use of io.helidon.common.reactive.BufferedEmittingPublisher in project helidon by oracle.
the class DataChunkInputStreamTest method differentThreads.
@Test
public void differentThreads() throws Exception {
List<String> test_data = List.of("test0", "test1", "test2", "test3");
List<String> result = new ArrayList<>();
BufferedEmittingPublisher<String> pub = BufferedEmittingPublisher.create();
ExecutorService executorService = Executors.newFixedThreadPool(2);
Future<?> submitFuture = executorService.submit(() -> {
for (int i = 0; i < test_data.size(); i++) {
pub.emit(test_data.get(i));
sleep();
}
pub.complete();
});
Future<?> receiveFuture = executorService.submit(() -> {
DataChunkInputStream chunkInputStream = new DataChunkInputStream(Multi.create(pub).map(s -> DataChunk.create(s.getBytes())));
for (int i = 0; i < test_data.size(); i++) {
try {
String token = new String(chunkInputStream.readNBytes(test_data.get(0).length()));
System.out.println(">>> " + token);
result.add(token);
} catch (IOException e) {
fail(e);
}
}
});
submitFuture.get(500, TimeUnit.MILLISECONDS);
receiveFuture.get(500, TimeUnit.MILLISECONDS);
assertEquals(test_data, result);
}
Aggregations