17
17
package org .springframework .http .client ;
18
18
19
19
import java .io .IOException ;
20
- import java .io .InputStream ;
21
20
import java .io .OutputStreamWriter ;
22
21
import java .util .ArrayList ;
23
22
import java .util .List ;
28
27
29
28
import org .junit .jupiter .api .Test ;
30
29
import org .reactivestreams .FlowAdapters ;
31
- import reactor .core .publisher .Flux ;
32
30
import reactor .test .StepVerifier ;
33
31
34
32
import static java .nio .charset .StandardCharsets .UTF_8 ;
@@ -79,8 +77,8 @@ void basic() {
79
77
},
80
78
this .byteMapper , this .executor , null );
81
79
82
- StepVerifier .create (toStringFlux (publisher ))
83
- .assertNext (s -> assertThat (s ).isEqualTo ("foobarbaz" ))
80
+ StepVerifier .create (FlowAdapters . toPublisher (publisher ))
81
+ .assertNext (s -> assertThat (s ).containsExactly ("foobarbaz" . getBytes ( UTF_8 ) ))
84
82
.verifyComplete ();
85
83
}
86
84
@@ -98,17 +96,20 @@ void flush() throws IOException {
98
96
this .byteMapper , this .executor , null );
99
97
100
98
101
- try (InputStream is = SubscriberInputStream . subscribeTo (
102
- toStringPublisher ( publisher ), s -> s . getBytes ( UTF_8 ), s -> {}, 1 )) {
99
+ try (SubscriberInputStream < byte []> is = new SubscriberInputStream <>( s -> s , s -> {}, 1 )) {
100
+ publisher . subscribe ( is );
103
101
104
102
byte [] chunk = new byte [3 ];
105
103
106
104
assertThat (is .read (chunk )).isEqualTo (3 );
107
105
assertThat (chunk ).containsExactly (FOO );
106
+
108
107
assertThat (is .read (chunk )).isEqualTo (3 );
109
108
assertThat (chunk ).containsExactly (BAR );
109
+
110
110
assertThat (is .read (chunk )).isEqualTo (3 );
111
111
assertThat (chunk ).containsExactly (BAZ );
112
+
112
113
assertThat (is .read (chunk )).isEqualTo (-1 );
113
114
}
114
115
}
@@ -123,8 +124,8 @@ void chunkSize() {
123
124
},
124
125
this .byteMapper , this .executor , 2 );
125
126
126
- try (InputStream is = SubscriberInputStream . subscribeTo (
127
- toStringPublisher ( publisher ), s -> s . getBytes ( UTF_8 ), s -> {}, 1 )) {
127
+ try (SubscriberInputStream < byte []> is = new SubscriberInputStream <>( s -> s , s -> {}, 1 )) {
128
+ publisher . subscribe ( is );
128
129
129
130
StringBuilder stringBuilder = new StringBuilder ();
130
131
byte [] chunk = new byte [3 ];
@@ -148,7 +149,7 @@ void chunkSize() {
148
149
}
149
150
150
151
@ Test
151
- void cancel () throws InterruptedException {
152
+ void cancel () throws InterruptedException , IOException {
152
153
CountDownLatch latch = new CountDownLatch (1 );
153
154
154
155
Flow .Publisher <byte []> publisher = new OutputStreamPublisher <>(
@@ -165,27 +166,23 @@ void cancel() throws InterruptedException {
165
166
166
167
}, this .byteMapper , this .executor , null );
167
168
168
- List <String > discarded = new ArrayList <>();
169
-
170
- try (InputStream is = SubscriberInputStream .subscribeTo (
171
- toStringPublisher (publisher ), s -> s .getBytes (UTF_8 ), discarded ::add , 1 )) {
169
+ List <byte []> discarded = new ArrayList <>();
172
170
171
+ try (SubscriberInputStream <byte []> is = new SubscriberInputStream <>(s -> s , discarded ::add , 1 )) {
172
+ publisher .subscribe (is );
173
173
byte [] chunk = new byte [3 ];
174
174
175
175
assertThat (is .read (chunk )).isEqualTo (3 );
176
176
assertThat (chunk ).containsExactly (FOO );
177
177
}
178
- catch (IOException e ) {
179
- throw new RuntimeException (e );
180
- }
181
178
182
179
latch .await ();
183
180
184
- assertThat (discarded ).containsExactly ("bar" );
181
+ assertThat (discarded ).containsExactly ("bar" . getBytes ( UTF_8 ) );
185
182
}
186
183
187
184
@ Test
188
- void closed () throws InterruptedException {
185
+ void closed () throws InterruptedException , IOException {
189
186
CountDownLatch latch = new CountDownLatch (1 );
190
187
191
188
Flow .Publisher <byte []> publisher = new OutputStreamPublisher <>(
@@ -198,18 +195,14 @@ void closed() throws InterruptedException {
198
195
},
199
196
this .byteMapper , this .executor , null );
200
197
201
- try (InputStream is = SubscriberInputStream .subscribeTo (
202
- toStringPublisher (publisher ), s -> s .getBytes (UTF_8 ), s -> {}, 1 )) {
203
-
198
+ try (SubscriberInputStream <byte []> is = new SubscriberInputStream <>(s -> s , s -> {}, 1 )) {
199
+ publisher .subscribe (is );
204
200
byte [] chunk = new byte [3 ];
205
201
206
202
assertThat (is .read (chunk )).isEqualTo (3 );
207
203
assertThat (chunk ).containsExactly (FOO );
208
204
assertThat (is .read (chunk )).isEqualTo (-1 );
209
205
}
210
- catch (IOException e ) {
211
- throw new RuntimeException (e );
212
- }
213
206
214
207
latch .await ();
215
208
}
@@ -234,19 +227,11 @@ void mapperThrowsException() throws InterruptedException {
234
227
Throwable savedEx = null ;
235
228
236
229
StringBuilder sb = new StringBuilder ();
237
- try (InputStream is = SubscriberInputStream . subscribeTo (
238
- publisher , s -> { throw new NullPointerException ("boom" ); }, s -> {}, 1 )) {
230
+ try (SubscriberInputStream < byte []> is = new SubscriberInputStream <> (
231
+ s -> { throw new NullPointerException ("boom" ); }, s -> {}, 1 )) {
239
232
240
- byte [] chunk = new byte [3 ];
241
-
242
- sb .append (new String (new byte []{(byte )is .read ()}, UTF_8 ));
243
- assertThat (is .read (chunk )).isEqualTo (3 );
244
- sb .append (new String (chunk , UTF_8 ));
245
- assertThat (is .read (chunk )).isEqualTo (3 );
246
- sb .append (new String (chunk , UTF_8 ));
247
- assertThat (is .read (chunk )).isEqualTo (2 );
248
- sb .append (new String (chunk ,0 , 2 , UTF_8 ));
249
- assertThat (is .read ()).isEqualTo (-1 );
233
+ publisher .subscribe (is );
234
+ sb .append (new String (new byte [] {(byte ) is .read ()}, UTF_8 ));
250
235
}
251
236
catch (Throwable ex ) {
252
237
savedEx = ex ;
@@ -258,12 +243,4 @@ void mapperThrowsException() throws InterruptedException {
258
243
assertThat (savedEx ).hasMessage ("boom" );
259
244
}
260
245
261
- private static Flow .Publisher <String > toStringPublisher (Flow .Publisher <byte []> publisher ) {
262
- return FlowAdapters .toFlowPublisher (toStringFlux (publisher ));
263
- }
264
-
265
- private static Flux <String > toStringFlux (Flow .Publisher <byte []> publisher ) {
266
- return Flux .from (FlowAdapters .toPublisher (publisher )).map (bytes -> new String (bytes , UTF_8 ));
267
- }
268
-
269
246
}
0 commit comments