akka.stream.testkit.TestSubscriber Java Examples

The following examples show how to use akka.stream.testkit.TestSubscriber. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AkkaStreamsDemoTest.java    From reactive-streams-in-java with Apache License 2.0 5 votes vote down vote up
@Test
public void test_a_source() {
    Sink<Object, TestSubscriber.Probe<Object>> sink = TestSink.probe(system);
    Source<Object, NotUsed> sourceUnderTest = Source.single("test");

    sourceUnderTest.runWith(sink, materializer)
            .request(1)
            .expectNext("test")
            .expectComplete();
}
 
Example #2
Source File: MergeSortedAsPairTest.java    From ditto with Eclipse Public License 2.0 5 votes vote down vote up
private void materializeTestProbes() {
    final Pair<Pair<TestPublisher.Probe<Integer>, TestPublisher.Probe<Integer>>,
            TestSubscriber.Probe<Pair<Integer, Integer>>>
            probes =
            mergeSortedAsPairWithMat(TestSource.probe(system), TestSource.probe(system))
                    .toMat(TestSink.probe(system), Keep.both())
                    .run(mat);
    source1Probe = probes.first().first();
    source2Probe = probes.first().second();
    sinkProbe = probes.second();
}
 
Example #3
Source File: ResumeSourceTest.java    From ditto with Eclipse Public License 2.0 5 votes vote down vote up
@Before
public void init() {
    system = ActorSystem.create();
    mat = ActorMaterializer.create(system);

    rematerializeSource();

    // materialize sink once - it never fails.
    final Sink<Integer, TestSubscriber.Probe<Integer>> sink = TestSink.probe(system);
    final Pair<TestSubscriber.Probe<Integer>, Sink<Integer, NotUsed>> sinkPair = sink.preMaterialize(mat);
    sinkProbe = sinkPair.first();
    testSink = sinkPair.second();
}
 
Example #4
Source File: TransistorTest.java    From ditto with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Connect a transistor to 2 test collectors and a test sink.
 */
@Before
public void init() {
    system = ActorSystem.create();
    final Source<Integer, TestPublisher.Probe<Integer>> collectorSource = TestSource.probe(system);
    final Source<Integer, TestPublisher.Probe<Integer>> baseSource = TestSource.probe(system);
    final Sink<Integer, TestSubscriber.Probe<Integer>> emitterSink = TestSink.probe(system);
    final Transistor<Integer> underTest = Transistor.of();

    final Graph<SourceShape<Integer>, Pair<TestPublisher.Probe<Integer>, TestPublisher.Probe<Integer>>>
            collectorGateTransistor =
            GraphDSL$.MODULE$.create3(
                    collectorSource, baseSource, underTest,
                    (collector, base, notUsed) -> Pair.create(collector, base),
                    (builder, collectorShape, baseShape, transistorShape) -> {
                        builder.from(collectorShape.out()).toInlet(transistorShape.in0());
                        builder.from(baseShape.out()).toInlet(transistorShape.in1());
                        return SourceShape.of(transistorShape.out());
                    });

    final Pair<Pair<TestPublisher.Probe<Integer>, TestPublisher.Probe<Integer>>, TestSubscriber.Probe<Integer>> m =
            Source.fromGraph(collectorGateTransistor)
                    .toMat(emitterSink, Keep.both())
                    .run(ActorMaterializer.create(system));

    collector = m.first().first();
    base = m.first().second();
    emitter = m.second();
}
 
Example #5
Source File: AmqpConnectorsTest.java    From rabbitmq-mock with Apache License 2.0 4 votes vote down vote up
@Test
public void publishAndConsumeRpcWithoutAutoAck() throws Exception {

    final String queueName = "amqp-conn-it-spec-rpc-queue-" + System.currentTimeMillis();
    final QueueDeclaration queueDeclaration = QueueDeclaration.create(queueName);

    final List<String> input = Arrays.asList("one", "two", "three", "four", "five");

    final Flow<WriteMessage, CommittableReadResult, CompletionStage<String>> ampqRpcFlow =
        AmqpRpcFlow.committableFlow(
            AmqpWriteSettings.create(connectionProvider)
                .withRoutingKey(queueName)
                .withDeclaration(queueDeclaration),
            10,
            1);
    Pair<CompletionStage<String>, TestSubscriber.Probe<ReadResult>> result =
        Source.from(input)
            .map(ByteString::fromString)
            .map(bytes -> WriteMessage.create(bytes))
            .viaMat(ampqRpcFlow, Keep.right())
            .mapAsync(1, cm -> cm.ack().thenApply(unused -> cm.message()))
            .toMat(TestSink.probe(system), Keep.both())
            .run(materializer);

    result.first().toCompletableFuture().get(5, TimeUnit.SECONDS);

    Sink<WriteMessage, CompletionStage<Done>> amqpSink =
        AmqpSink.createReplyTo(AmqpReplyToSinkSettings.create(connectionProvider));

    final Source<ReadResult, NotUsed> amqpSource =
        AmqpSource.atMostOnceSource(
            NamedQueueSourceSettings.create(connectionProvider, queueName)
                .withDeclaration(queueDeclaration),
            1);

    UniqueKillSwitch sourceToSink =
        amqpSource
            .viaMat(KillSwitches.single(), Keep.right())
            .map(b -> WriteMessage.create(b.bytes()).withProperties(b.properties()))
            .to(amqpSink)
            .run(materializer);

    List<ReadResult> probeResult =
        JavaConverters.seqAsJavaListConverter(
            result.second().toStrict(Duration.create(5, TimeUnit.SECONDS)))
            .asJava();
    assertEquals(
        probeResult.stream().map(s -> s.bytes().utf8String()).collect(Collectors.toList()), input);
    sourceToSink.shutdown();
}
 
Example #6
Source File: PortfolioServiceImplTest.java    From reactive-stock-trader with Apache License 2.0 4 votes vote down vote up
@Test
public void ignoreDuplicateTrades() throws Exception {
    PortfolioService service = server.client(PortfolioService.class);
    OpenPortfolioDetails details = new OpenPortfolioDetails("portfolioName");
    val portfolioId = service.openPortfolio().invoke(details).toCompletableFuture().get(5, SECONDS);
    Source<OrderPlaced, ?> source = service.orderPlaced().subscribe().atMostOnceSource();
    TestSubscriber.Probe<OrderPlaced> probe =
            source.runWith(TestSink.probe(server.system()), server.materializer());

    String symbol = "IBM";
    int sharesToBuy = 31;
    OrderDetails buyOrderDetails = OrderDetails.builder()
            .symbol(symbol)
            .shares(sharesToBuy)
            .tradeType(TradeType.BUY)
            .orderType(OrderType.Market.INSTANCE)
            .build();

    val orderId = service.placeOrder(portfolioId).invoke(buyOrderDetails).toCompletableFuture().get(5, SECONDS);

    BigDecimal price = new BigDecimal("123.45");
    OrderResult tradeResult = OrderResult.Fulfilled.builder()
            .portfolioId(portfolioId)
            .orderId(orderId)
            .trade(Trade.builder()
                    .tradeType(TradeType.BUY)
                    .symbol(symbol)
                    .sharePrice(price)
                    .shares(sharesToBuy)
                    .build()
            ).build();

    BrokerStub.orderResultProducerStub.send(tradeResult);
    BrokerStub.orderResultProducerStub.send(tradeResult);


    PortfolioView view = service.getPortfolio(portfolioId).invoke().toCompletableFuture().get(5, SECONDS);
    assertEquals(1, view.getHoldings().size());
    assertEquals(sharesToBuy, view.getHoldings().get(0).getShareCount());

}