Search in sources :

Example 1 with BufferedEmittingPublisher

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);
}
Also used : BufferedEmittingPublisher(io.helidon.common.reactive.BufferedEmittingPublisher) Assertions.fail(org.junit.jupiter.api.Assertions.fail) Arrays(java.util.Arrays) DataChunk(io.helidon.common.http.DataChunk) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) Executors(java.util.concurrent.Executors) ArrayList(java.util.ArrayList) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) List(java.util.List) Future(java.util.concurrent.Future) Publisher(java.util.concurrent.Flow.Publisher) Flow(java.util.concurrent.Flow) Single(io.helidon.common.reactive.Single) Matchers.is(org.hamcrest.Matchers.is) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) ExecutorService(java.util.concurrent.ExecutorService) Multi(io.helidon.common.reactive.Multi) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) ExecutorService(java.util.concurrent.ExecutorService) IOException(java.io.IOException) Test(org.junit.jupiter.api.Test)

Aggregations

DataChunk (io.helidon.common.http.DataChunk)1 BufferedEmittingPublisher (io.helidon.common.reactive.BufferedEmittingPublisher)1 Multi (io.helidon.common.reactive.Multi)1 Single (io.helidon.common.reactive.Single)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 ByteBuffer (java.nio.ByteBuffer)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 List (java.util.List)1 ExecutorService (java.util.concurrent.ExecutorService)1 Executors (java.util.concurrent.Executors)1 Flow (java.util.concurrent.Flow)1 Publisher (java.util.concurrent.Flow.Publisher)1 Future (java.util.concurrent.Future)1 TimeUnit (java.util.concurrent.TimeUnit)1 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)1 Matchers.is (org.hamcrest.Matchers.is)1 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)1 Assertions.fail (org.junit.jupiter.api.Assertions.fail)1