Search in sources :

Example 1 with SubmissionPublisher

use of java.util.concurrent.SubmissionPublisher in project reactive-streams-jvm by reactive-streams.

the class FlowAdaptersTest method flowToReactiveError.

@Test
public void flowToReactiveError() {
    SubmissionPublisher<Integer> p = new SubmissionPublisher<Integer>(new Executor() {

        @Override
        public void execute(Runnable command) {
            command.run();
        }
    }, Flow.defaultBufferSize());
    TestEitherConsumer<Integer> tc = new TestEitherConsumer<Integer>();
    FlowAdapters.toPublisher(p).subscribe(tc);
    p.submit(1);
    p.submit(2);
    p.submit(3);
    p.submit(4);
    p.submit(5);
    p.closeExceptionally(new IOException());
    tc.assertFailure(IOException.class, 1, 2, 3, 4, 5);
}
Also used : Executor(java.util.concurrent.Executor) SubmissionPublisher(java.util.concurrent.SubmissionPublisher) IOException(java.io.IOException) Test(org.testng.annotations.Test)

Example 2 with SubmissionPublisher

use of java.util.concurrent.SubmissionPublisher in project reactive-streams-jvm by reactive-streams.

the class FlowAdaptersTest method flowToReactiveNormal.

@Test
public void flowToReactiveNormal() {
    SubmissionPublisher<Integer> p = new SubmissionPublisher<Integer>(new Executor() {

        @Override
        public void execute(Runnable command) {
            command.run();
        }
    }, Flow.defaultBufferSize());
    TestEitherConsumer<Integer> tc = new TestEitherConsumer<Integer>();
    FlowAdapters.toPublisher(p).subscribe(tc);
    p.submit(1);
    p.submit(2);
    p.submit(3);
    p.submit(4);
    p.submit(5);
    p.close();
    tc.assertRange(1, 5);
}
Also used : Executor(java.util.concurrent.Executor) SubmissionPublisher(java.util.concurrent.SubmissionPublisher) Test(org.testng.annotations.Test)

Example 3 with SubmissionPublisher

use of java.util.concurrent.SubmissionPublisher in project Java-9-Spring-Webflux by kkTranslation.

the class TestStockMaintain method teststockRemoval.

@Test
public void teststockRemoval() throws InterruptedException {
    Stock stock = new Stock();
    SubmissionPublisher<Order> p = new SubmissionPublisher<>();
    p.subscribe(new StockMaintain(stock));
    Product product = new Product();
    stock.store(product, 40);
    OrderItem item = new OrderItem();
    item.setProduct(product);
    item.setAmount(10);
    Order order = new Order();
    List<OrderItem> items = new LinkedList<>();
    items.add(item);
    order.setItems(items);
    for (int i = 0; i < 10; i++) p.submit(order);
    log.info("所有订单已经提交完毕");
    for (int j = 0; j < 10; j++) {
        log.info("Sleeping a bit...");
        Thread.sleep(50);
    }
    p.close();
    log.info("Publisher已关闭");
}
Also used : Order(com.dockerx.reactive.orderstock.product.Order) SubmissionPublisher(java.util.concurrent.SubmissionPublisher) OrderItem(com.dockerx.reactive.orderstock.product.OrderItem) Product(com.dockerx.reactive.orderstock.product.Product) StockMaintain(com.dockerx.reactive.orderstock.stock.StockMaintain) Stock(com.dockerx.reactive.orderstock.stock.Stock) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 4 with SubmissionPublisher

use of java.util.concurrent.SubmissionPublisher in project javaBook-src by huifer.

the class ProcessorTest method main.

public static void main(String[] args) throws InterruptedException {
    SubmissionPublisher<Integer> publisher = new SubmissionPublisher<>();
    Processor processor = new Processor();
    SubscrioberForString subscrioberForString = new SubscrioberForString();
    publisher.subscribe(processor);
    processor.subscribe(subscrioberForString);
    for (int i = 0; i < 300; i++) {
        int i1 = new Random().nextInt(100);
        publisher.submit(i1);
    }
    TimeUnit.SECONDS.sleep(500);
    System.out.println("main work end");
}
Also used : Random(java.util.Random) SubmissionPublisher(java.util.concurrent.SubmissionPublisher)

Example 5 with SubmissionPublisher

use of java.util.concurrent.SubmissionPublisher in project helidon by oracle.

the class SubscriberInputStreamTest method testSingle.

@Test
public void testSingle() throws IOException {
    String value = "the long value I want to push through";
    SubmissionPublisher<ByteBuffer> publisher = new SubmissionPublisher<>();
    SubscriberInputStream is = new SubscriberInputStream();
    publisher.subscribe(is);
    // asynchronously publish
    new Thread(() -> {
        publisher.submit(ByteBuffer.wrap(value.getBytes(StandardCharsets.UTF_8)));
        publisher.close();
    }).start();
    byte[] buffer = new byte[16];
    int len;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    while ((len = is.read(buffer)) != -1) {
        baos.write(buffer, 0, len);
    }
    String result = new String(baos.toByteArray(), StandardCharsets.UTF_8);
    assertThat(result, is(value));
}
Also used : SubmissionPublisher(java.util.concurrent.SubmissionPublisher) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.jupiter.api.Test)

Aggregations

SubmissionPublisher (java.util.concurrent.SubmissionPublisher)12 CountDownLatch (java.util.concurrent.CountDownLatch)3 Test (org.junit.jupiter.api.Test)3 DataChunk (io.helidon.common.http.DataChunk)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 IOException (java.io.IOException)2 ByteBuffer (java.nio.ByteBuffer)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 Executor (java.util.concurrent.Executor)2 Subscription (java.util.concurrent.Flow.Subscription)2 Test (org.testng.annotations.Test)2 Order (com.dockerx.reactive.orderstock.product.Order)1 OrderItem (com.dockerx.reactive.orderstock.product.OrderItem)1 Product (com.dockerx.reactive.orderstock.product.Product)1 Stock (com.dockerx.reactive.orderstock.stock.Stock)1 StockMaintain (com.dockerx.reactive.orderstock.stock.StockMaintain)1 TestUtils.requestChunkAsString (io.helidon.webserver.utils.TestUtils.requestChunkAsString)1 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1