Skip to content

Commit 38c88e7

Browse files
committed
Polishing in SubscriberInputStreamTests
1 parent 4a0edc5 commit 38c88e7

File tree

1 file changed

+22
-36
lines changed

1 file changed

+22
-36
lines changed

spring-web/src/test/java/org/springframework/http/client/SubscriberInputStreamTests.java

Lines changed: 22 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@
2626
import java.util.concurrent.Flow;
2727

2828
import org.junit.jupiter.api.Test;
29-
import org.reactivestreams.FlowAdapters;
30-
import reactor.test.StepVerifier;
3129

3230
import static java.nio.charset.StandardCharsets.UTF_8;
3331
import static org.assertj.core.api.Assertions.assertThat;
@@ -68,22 +66,7 @@ public byte[] map(byte[] b, int off, int len) {
6866

6967

7068
@Test
71-
void basic() {
72-
Flow.Publisher<byte[]> publisher = new OutputStreamPublisher<>(
73-
out -> {
74-
out.write(FOO);
75-
out.write(BAR);
76-
out.write(BAZ);
77-
},
78-
this.byteMapper, this.executor, null);
79-
80-
StepVerifier.create(FlowAdapters.toPublisher(publisher))
81-
.assertNext(s -> assertThat(s).containsExactly("foobarbaz".getBytes(UTF_8)))
82-
.verifyComplete();
83-
}
84-
85-
@Test
86-
void flush() throws IOException {
69+
void basic() throws IOException {
8770
Flow.Publisher<byte[]> publisher = new OutputStreamPublisher<>(
8871
out -> {
8972
out.write(FOO);
@@ -115,7 +98,7 @@ void flush() throws IOException {
11598
}
11699

117100
@Test
118-
void chunkSize() {
101+
void chunkSize() throws Exception {
119102
Flow.Publisher<byte[]> publisher = new OutputStreamPublisher<>(
120103
out -> {
121104
out.write(FOO);
@@ -127,24 +110,25 @@ void chunkSize() {
127110
try (SubscriberInputStream<byte[]> is = new SubscriberInputStream<>(s -> s, s -> {}, 1)) {
128111
publisher.subscribe(is);
129112

130-
StringBuilder stringBuilder = new StringBuilder();
113+
StringBuilder sb = new StringBuilder();
131114
byte[] chunk = new byte[3];
132115

133-
stringBuilder.append(new String(new byte[]{(byte)is.read()}, UTF_8));
116+
sb.append((char) is.read());
117+
assertThat(sb).matches("f");
118+
134119
assertThat(is.read(chunk)).isEqualTo(3);
120+
sb.append(new String(chunk, UTF_8));
121+
assertThat(sb).matches("foob");
135122

136-
stringBuilder.append(new String(chunk, UTF_8));
137123
assertThat(is.read(chunk)).isEqualTo(3);
124+
sb.append(new String(chunk, UTF_8));
125+
assertThat(sb).matches("foobarb");
138126

139-
stringBuilder.append(new String(chunk, UTF_8));
140127
assertThat(is.read(chunk)).isEqualTo(2);
128+
sb.append(new String(chunk,0, 2, UTF_8));
129+
assertThat(sb).matches("foobarbaz");
141130

142-
stringBuilder.append(new String(chunk,0, 2, UTF_8));
143131
assertThat(is.read()).isEqualTo(-1);
144-
assertThat(stringBuilder.toString()).isEqualTo("foobarbaz");
145-
}
146-
catch (IOException e) {
147-
throw new RuntimeException(e);
148132
}
149133
}
150134

@@ -155,13 +139,13 @@ void cancel() throws InterruptedException, IOException {
155139
Flow.Publisher<byte[]> publisher = new OutputStreamPublisher<>(
156140
out -> {
157141
assertThatIOException().isThrownBy(() -> {
158-
out.write(FOO);
159-
out.flush();
160-
out.write(BAR);
161-
out.flush();
162-
out.write(BAZ);
163-
out.flush();
164-
}).withMessage("Subscription has been terminated");
142+
out.write(FOO);
143+
out.flush();
144+
out.write(BAR);
145+
out.flush();
146+
out.write(BAZ);
147+
out.flush();
148+
}).withMessage("Subscription has been terminated");
165149
latch.countDown();
166150

167151
}, this.byteMapper, this.executor, null);
@@ -201,6 +185,7 @@ void closed() throws InterruptedException, IOException {
201185

202186
assertThat(is.read(chunk)).isEqualTo(3);
203187
assertThat(chunk).containsExactly(FOO);
188+
204189
assertThat(is.read(chunk)).isEqualTo(-1);
205190
}
206191

@@ -215,7 +200,8 @@ void mapperThrowsException() throws InterruptedException {
215200
out -> {
216201
out.write(FOO);
217202
out.flush();
218-
assertThatIOException().isThrownBy(() -> {
203+
assertThatIOException()
204+
.isThrownBy(() -> {
219205
out.write(BAR);
220206
out.flush();
221207
})

0 commit comments

Comments
 (0)