use of java.util.concurrent.Flow.Subscriber in project helidon by oracle.
the class SingleTest method testToFutureDoubleOnNext.
@Test
public void testToFutureDoubleOnNext() throws InterruptedException, ExecutionException {
Single<String> single = new CompletionSingle<String>() {
@Override
public void subscribe(Subscriber<? super String> subscriber) {
subscriber.onSubscribe(new Subscription() {
@Override
public void request(long n) {
subscriber.onNext("foo");
subscriber.onNext("bar");
subscriber.onComplete();
}
@Override
public void cancel() {
}
});
}
};
Future<String> future = single.toStage().toCompletableFuture();
assertThat(future.isDone(), is(equalTo(true)));
assertThat(future.get(), is(equalTo("foo")));
}
use of java.util.concurrent.Flow.Subscriber in project helidon by oracle.
the class MultiPartDecoderTest method testContentAcrossChunks.
@Test
public void testContentAcrossChunks() {
String boundary = "boundary";
final byte[] chunk1 = ("--" + boundary + "\n" + "Content-Id: part1\n" + "\n" + "this-is-the-1st-slice-of-the-body\n").getBytes();
final byte[] chunk2 = ("this-is-the-2nd-slice-of-the-body\n" + "--" + boundary + "--").getBytes();
final CountDownLatch latch = new CountDownLatch(2);
Consumer<BodyPart> consumer = (part) -> {
latch.countDown();
assertThat(part.headers().values("Content-Id"), hasItems("part1"));
DataChunkSubscriber subscriber = new DataChunkSubscriber();
part.content().subscribe(subscriber);
subscriber.content().thenAccept(body -> {
latch.countDown();
assertThat(body, is(equalTo("this-is-the-1st-slice-of-the-body\n" + "this-is-the-2nd-slice-of-the-body")));
});
};
BodyPartSubscriber testSubscriber = new BodyPartSubscriber(SUBSCRIBER_TYPE.INFINITE, consumer);
partsPublisher(boundary, List.of(chunk1, chunk2)).subscribe(testSubscriber);
try {
boolean b = testSubscriber.complete.orTimeout(200, TimeUnit.MILLISECONDS).join();
assertThat(b, is(equalTo(true)));
} catch (CompletionException error) {
assertThat(error, is(nullValue()));
}
waitOnLatch(latch);
}
use of java.util.concurrent.Flow.Subscriber in project helidon by oracle.
the class MultiPartDecoderTest method testOnePartInOneChunk.
@Test
public void testOnePartInOneChunk() {
String boundary = "boundary";
final byte[] chunk1 = ("--" + boundary + "\n" + "Content-Id: part1\n" + "\n" + "body 1\n" + "--" + boundary + "--").getBytes();
final CountDownLatch latch = new CountDownLatch(2);
final CompletableFuture<Void> testDone = new CompletableFuture<>();
Consumer<BodyPart> consumer = (part) -> {
latch.countDown();
assertThat(part.headers().values("Content-Id"), hasItems("part1"));
DataChunkSubscriber subscriber = new DataChunkSubscriber();
part.content().subscribe(subscriber);
subscriber.content().thenAccept(body -> {
assertThat(body, is(equalTo("body 1")));
latch.countDown();
if (latch.getCount() == 0) {
testDone.complete(null);
}
}).exceptionally((ex) -> {
testDone.completeExceptionally(ex);
return null;
});
};
BodyPartSubscriber testSubscriber = new BodyPartSubscriber(SUBSCRIBER_TYPE.INFINITE, consumer);
partsPublisher(boundary, chunk1).subscribe(testSubscriber);
testDone.orTimeout(5, TimeUnit.SECONDS).join();
try {
boolean b = testSubscriber.complete.orTimeout(200, TimeUnit.MILLISECONDS).join();
assertThat(b, is(equalTo(true)));
} catch (CompletionException error) {
assertThat(error, is(nullValue()));
}
}
use of java.util.concurrent.Flow.Subscriber in project helidon by oracle.
the class MultiPartDecoderTest method testTwoPartsInOneChunk.
@Test
public void testTwoPartsInOneChunk() {
String boundary = "boundary";
final byte[] chunk1 = ("--" + boundary + "\n" + "Content-Id: part1\n" + "\n" + "body 1\n" + "--" + boundary + "\n" + "Content-Id: part2\n" + "\n" + "body 2\n" + "--" + boundary + "--").getBytes();
final CountDownLatch latch = new CountDownLatch(4);
Consumer<BodyPart> consumer = (part) -> {
latch.countDown();
if (latch.getCount() == 3) {
assertThat(part.headers().values("Content-Id"), hasItems("part1"));
DataChunkSubscriber subscriber = new DataChunkSubscriber();
part.content().subscribe(subscriber);
subscriber.content().thenAccept(body -> {
latch.countDown();
assertThat(body, is(equalTo("body 1")));
});
} else {
assertThat(part.headers().values("Content-Id"), hasItems("part2"));
DataChunkSubscriber subscriber = new DataChunkSubscriber();
part.content().subscribe(subscriber);
subscriber.content().thenAccept(body -> {
latch.countDown();
assertThat(body, is(equalTo("body 2")));
});
}
};
BodyPartSubscriber testSubscriber = new BodyPartSubscriber(SUBSCRIBER_TYPE.INFINITE, consumer);
partsPublisher(boundary, chunk1).subscribe(testSubscriber);
try {
boolean b = testSubscriber.complete.orTimeout(200, TimeUnit.MILLISECONDS).join();
assertThat(b, is(equalTo(true)));
} catch (CompletionException error) {
assertThat(error, is(nullValue()));
}
waitOnLatch(latch);
}
use of java.util.concurrent.Flow.Subscriber in project helidon by oracle.
the class MultiPartDecoderTest method testMulitiplePartsWithOneByOneSubscriber.
@Test
public void testMulitiplePartsWithOneByOneSubscriber() {
String boundary = "boundary";
final byte[] chunk1 = ("--" + boundary + "\n" + "Content-Id: part1\n" + "\n" + "body 1\n" + "--" + boundary + "\n" + "Content-Id: part2\n" + "\n" + "body 2\n" + "--" + boundary + "--").getBytes();
final CountDownLatch latch = new CountDownLatch(4);
Consumer<BodyPart> consumer = (part) -> {
latch.countDown();
if (latch.getCount() == 3) {
assertThat(part.headers().values("Content-Id"), hasItems("part1"));
DataChunkSubscriber subscriber = new DataChunkSubscriber();
part.content().subscribe(subscriber);
subscriber.content().thenAccept(body -> {
latch.countDown();
assertThat(body, is(equalTo("body 1")));
});
} else {
assertThat(part.headers().values("Content-Id"), hasItems("part2"));
DataChunkSubscriber subscriber = new DataChunkSubscriber();
part.content().subscribe(subscriber);
subscriber.content().thenAccept(body -> {
latch.countDown();
assertThat(body, is(equalTo("body 2")));
});
}
};
BodyPartSubscriber testSubscriber = new BodyPartSubscriber(SUBSCRIBER_TYPE.ONE_BY_ONE, consumer);
partsPublisher(boundary, chunk1).subscribe(testSubscriber);
try {
boolean b = testSubscriber.complete.orTimeout(200, TimeUnit.MILLISECONDS).join();
assertThat(b, is(equalTo(true)));
} catch (CompletionException error) {
assertThat(error, is(nullValue()));
}
waitOnLatch(latch);
}
Aggregations