-
Notifications
You must be signed in to change notification settings - Fork 628
/
NettyOutboundTest.java
343 lines (299 loc) · 10.9 KB
/
NettyOutboundTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/*
* Copyright (c) 2017-2022 VMware, Inc. or its affiliates, All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package reactor.netty5;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.channels.Channels;
import java.nio.channels.WritableByteChannel;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.cert.CertificateException;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Predicate;
import javax.net.ssl.SSLException;
import io.netty5.buffer.api.Buffer;
import io.netty5.buffer.api.BufferAllocator;
import io.netty5.channel.ChannelHandlerContext;
import io.netty5.channel.DefaultFileRegion;
import io.netty5.channel.FileRegion;
import io.netty5.channel.embedded.EmbeddedChannel;
import io.netty5.handler.codec.MessageToMessageEncoder;
import io.netty5.handler.ssl.SslContext;
import io.netty5.handler.ssl.SslContextBuilder;
import io.netty5.handler.ssl.SslHandler;
import io.netty5.handler.ssl.util.SelfSignedCertificate;
import io.netty5.handler.stream.ChunkedNioFile;
import io.netty5.handler.stream.ChunkedWriteHandler;
import io.netty5.util.concurrent.Future;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.reactivestreams.Publisher;
import reactor.core.publisher.Mono;
import static io.netty5.buffer.api.DefaultBufferAllocators.preferredAllocator;
import static org.assertj.core.api.Assertions.assertThat;
class NettyOutboundTest {
static SelfSignedCertificate ssc;
@BeforeAll
static void createSelfSignedCertificate() throws CertificateException {
ssc = new SelfSignedCertificate();
}
@Test
void sendFileWithoutTlsUsesFileRegion() throws URISyntaxException {
List<Class<?>> messageClasses = new ArrayList<>(2);
EmbeddedChannel channel = new EmbeddedChannel(
new MessageToMessageEncoder<FileRegion>() {
@Override
protected void encodeAndClose(ChannelHandlerContext ctx, FileRegion msg,
List<Object> out) throws Exception {
ByteArrayOutputStream bais = new ByteArrayOutputStream();
WritableByteChannel wbc = Channels.newChannel(bais);
msg.transferTo(wbc, msg.position());
out.add(bais.toString(StandardCharsets.UTF_8));
}
},
new MessageToMessageEncoder<>() {
@Override
protected void encodeAndClose(ChannelHandlerContext ctx, Object msg,
List<Object> out) {
messageClasses.add(msg.getClass());
out.add(msg);
}
});
Connection mockContext = () -> channel;
NettyOutbound outbound = new NettyOutbound() {
@Override
public NettyOutbound sendObject(Publisher<?> dataStream, Predicate<Object> predicate) {
return this;
}
@Override
public NettyOutbound sendObject(Object message) {
return this;
}
@Override
public NettyOutbound send(Publisher<? extends Buffer> dataStream, Predicate<Buffer> predicate) {
return this;
}
@Override
public BufferAllocator alloc() {
return preferredAllocator();
}
@Override
public <S> NettyOutbound sendUsing(Callable<? extends S> sourceInput,
BiFunction<? super Connection, ? super S, ?> mappedInput,
Consumer<? super S> sourceCleanup) {
return then(mockSendUsing(mockContext, sourceInput, mappedInput, sourceCleanup));
}
@Override
public NettyOutbound withConnection(Consumer<? super Connection> withConnection) {
withConnection.accept(mockContext);
return this;
}
};
Future<Void> f = channel.writeOneOutbound(1);
outbound.sendFile(Paths.get(getClass().getResource("/largeFile.txt").toURI()))
.then().block();
assertThat(channel.inboundMessages()).isEmpty();
assertThat(channel.outboundMessages()).hasSize(2);
assertThat(messageClasses).containsExactly(Integer.class, DefaultFileRegion.class);
assertThat(channel.outboundMessages())
.element(1)
.asString()
.startsWith("This is an UTF-8 file that is larger than 1024 bytes. It contains accents like é. GARBAGE")
.endsWith("GARBAGE End of File");
assertThat(f.isSuccess()).isTrue();
assertThat(channel.finishAndReleaseAll()).isTrue();
}
@Test
void sendFileWithTlsUsesChunkedFile() throws URISyntaxException, SSLException {
SslContext sslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build();
final SslHandler sslHandler = sslCtx.newHandler(preferredAllocator());
List<Class<?>> messageWritten = new ArrayList<>(2);
List<Object> clearMessages = new ArrayList<>(2);
EmbeddedChannel channel = new EmbeddedChannel(
//outbound: pipeline reads inverted
//bytes are encrypted
sslHandler,
//capture the chunks unencrypted, transform as Strings:
new MessageToMessageEncoder<Buffer>() {
@Override
protected void encodeAndClose(ChannelHandlerContext ctx, Buffer msg,
List<Object> out) {
clearMessages.add(msg.readCharSequence(msg.readableBytes(), StandardCharsets.UTF_8));
out.add(msg.split());
msg.close();
}
},
//transform the ChunkedFile into Buffer chunks:
new ChunkedWriteHandler(),
//helps to ensure a ChunkedFile was written outs
new MessageToMessageEncoder<>() {
@Override
protected void encodeAndClose(ChannelHandlerContext ctx, Object msg, List<Object> out) {
messageWritten.add(msg.getClass());
out.add(msg);
}
});
Connection mockContext = () -> channel;
NettyOutbound outbound = new NettyOutbound() {
@Override
public NettyOutbound sendObject(Publisher<?> dataStream, Predicate<Object> predicate) {
return this;
}
@Override
public NettyOutbound sendObject(Object message) {
return this;
}
@Override
public NettyOutbound send(Publisher<? extends Buffer> dataStream, Predicate<Buffer> predicate) {
return this;
}
@Override
public BufferAllocator alloc() {
return preferredAllocator();
}
@Override
public <S> NettyOutbound sendUsing(Callable<? extends S> sourceInput,
BiFunction<? super Connection, ? super S, ?> mappedInput,
Consumer<? super S> sourceCleanup) {
return then(mockSendUsing(mockContext, sourceInput, mappedInput, sourceCleanup));
}
@Override
public NettyOutbound withConnection(Consumer<? super Connection> withConnection) {
withConnection.accept(mockContext);
return this;
}
};
Future<Void> f = channel.writeOneOutbound(1);
try {
outbound.sendFile(Paths.get(getClass().getResource("/largeFile.txt").toURI()))
.then()
.block(Duration.ofSeconds(1)); //TODO investigate why this hangs
}
catch (IllegalStateException e) {
if (!"Timeout on blocking read for 1000 MILLISECONDS".equals(e.getMessage())) {
throw e;
}
e.printStackTrace();
}
assertThat(messageWritten).containsExactly(Integer.class, ChunkedNioFile.class);
assertThat(clearMessages)
.hasSize(2)
.element(0)
.asString()
.startsWith("This is an UTF-8 file that is larger than 1024 bytes. It contains accents like é. GARBAGE")
.endsWith("1024 mark here ->");
assertThat(clearMessages)
.element(1)
.asString()
.startsWith("<- 1024 mark here")
.endsWith("End of File");
assertThat(f.isSuccess()).isFalse();
assertThat(channel.finishAndReleaseAll()).isTrue();
}
@Test
void sendFileWithForceChunkedFileUsesStrategyChunks()
throws URISyntaxException, IOException {
List<Class<?>> messageWritten = new ArrayList<>(2);
EmbeddedChannel channel = new EmbeddedChannel(
//outbound: pipeline reads inverted
//transform the Buffer chunks into Strings:
new MessageToMessageEncoder<Buffer>() {
@Override
protected void encodeAndClose(ChannelHandlerContext ctx, Buffer msg,
List<Object> out) {
out.add(msg.readCharSequence(msg.readableBytes(), StandardCharsets.UTF_8));
msg.close();
}
},
//transform the ChunkedFile into Buffer chunks:
new ChunkedWriteHandler(),
//helps to ensure a ChunkedFile was written outs
new MessageToMessageEncoder<>() {
@Override
protected void encodeAndClose(ChannelHandlerContext ctx, Object msg, List<Object> out) {
messageWritten.add(msg.getClass());
out.add(msg);
}
});
Connection mockContext = () -> channel;
NettyOutbound outbound = new NettyOutbound() {
@Override
public NettyOutbound sendObject(Publisher<?> dataStream, Predicate<Object> predicate) {
return this;
}
@Override
public NettyOutbound sendObject(Object message) {
return this;
}
@Override
public NettyOutbound send(Publisher<? extends Buffer> dataStream, Predicate<Buffer> predicate) {
return this;
}
@Override
public BufferAllocator alloc() {
return preferredAllocator();
}
@Override
public <S> NettyOutbound sendUsing(Callable<? extends S> sourceInput,
BiFunction<? super Connection, ? super S, ?> mappedInput,
Consumer<? super S> sourceCleanup) {
return then(mockSendUsing(mockContext, sourceInput, mappedInput, sourceCleanup));
}
@Override
public NettyOutbound withConnection(Consumer<? super Connection> withConnection) {
withConnection.accept(mockContext);
return this;
}
};
Path path = Paths.get(getClass().getResource("/largeFile.txt").toURI());
Future<Void> f = channel.writeOneOutbound(1);
outbound.sendFileChunked(path, 0, Files.size(path))
.then().block();
assertThat(channel.inboundMessages()).isEmpty();
assertThat(messageWritten).containsExactly(Integer.class, ChunkedNioFile.class);
assertThat(channel.outboundMessages())
.hasSize(3)
.element(1)
.asString()
.startsWith("This is an UTF-8 file that is larger than 1024 bytes. It contains accents like é. GARBAGE")
.endsWith("1024 mark here ->");
assertThat(channel.outboundMessages())
.last()
.asString()
.startsWith("<- 1024 mark here")
.endsWith("End of File");
assertThat(f.isSuccess()).isTrue();
assertThat(channel.finishAndReleaseAll()).isTrue();
}
static <S> Mono<Void> mockSendUsing(Connection c, Callable<? extends S> sourceInput,
BiFunction<? super Connection, ? super S, ?> mappedInput,
Consumer<? super S> sourceCleanup) {
return Mono.using(
sourceInput,
s -> Mono.fromCompletionStage(c.channel()
.writeAndFlush(mappedInput.apply(c, s)).asStage()),
sourceCleanup
);
}
}