Java Code Examples for org.reactivestreams.Processor#onNext()

The following examples show how to use org.reactivestreams.Processor#onNext() . 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: SerializedProcessorTest.java    From smallrye-mutiny with Apache License 2.0 6 votes vote down vote up
@Test(invocationCount = 20)
public void verifyOnNextOnCompleteThreadSafety() {
    final Processor<Integer, Integer> processor = UnicastProcessor.<Integer> create().serialized();
    MultiAssertSubscriber<Integer> subscriber = MultiAssertSubscriber.create(100);
    processor.subscribe(subscriber);

    Runnable r1 = () -> {
        processor.onNext(1);
        processor.onComplete();
    };
    Runnable r2 = processor::onComplete;

    new Thread(r1).start();
    new Thread(r2).start();

    subscriber.await();
    subscriber
            .assertSubscribed()
            .assertCompletedSuccessfully();

    if (subscriber.items().size() != 0) {
        assertThat(subscriber.items()).containsExactly(1);
    }
}
 
Example 2
Source File: SerializedProcessorTest.java    From smallrye-mutiny with Apache License 2.0 6 votes vote down vote up
@Test(invocationCount = 20)
public void verifyOnSubscribeOnCompleteThreadSafety() {
    final Processor<Integer, Integer> processor = UnicastProcessor.<Integer> create().serialized();
    MultiAssertSubscriber<Integer> subscriber = MultiAssertSubscriber.create(100);
    processor.subscribe(subscriber);

    Runnable r1 = () -> {
        processor.onNext(1);
        processor.onComplete();
    };
    Runnable r2 = () -> processor.onSubscribe(new Subscriptions.EmptySubscription());

    new Thread(r1).start();
    new Thread(r2).start();

    subscriber.await();
    subscriber
            .assertSubscribed()
            .assertCompletedSuccessfully();

    if (subscriber.items().size() != 0) {
        assertThat(subscriber.items()).containsExactly(1);
    }
}
 
Example 3
Source File: SerializedProcessorTest.java    From smallrye-mutiny with Apache License 2.0 6 votes vote down vote up
@Test(invocationCount = 50)
public void verifyOnFailureOnCompleteThreadSafety() {
    final Processor<Integer, Integer> processor = UnicastProcessor.<Integer> create().serialized();
    MultiAssertSubscriber<Integer> subscriber = MultiAssertSubscriber.create(100);
    processor.subscribe(subscriber);

    Runnable r1 = () -> {
        processor.onNext(1);
        processor.onComplete();
    };
    Runnable r2 = () -> processor.onError(new Exception("boom"));

    new Thread(r1).start();
    new Thread(r2).start();

    subscriber.await();
    subscriber
            .assertSubscribed()
            .assertTerminated();

    if (subscriber.items().size() != 0) {
        assertThat(subscriber.items()).containsExactly(1);
    }
}
 
Example 4
Source File: GSetTest.java    From wurmloch-crdt with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldHandleAddCommands() {
    // given:
    final Processor<GSet.AddCommand<String>, GSet.AddCommand<String>> inputStream = ReplayProcessor.create();
    final TestSubscriber<CrdtCommand> subscriber = TestSubscriber.create();
    final GSet<String> set = new GSet<>("ID_1");
    set.subscribeTo(inputStream);
    set.subscribe(subscriber);

    final GSet.AddCommand<String> command1 = new GSet.AddCommand<>(set.getCrdtId(), "1");
    final GSet.AddCommand<String> command2 = new GSet.AddCommand<>(set.getCrdtId(), "2");
    final GSet.AddCommand<String> command3 = new GSet.AddCommand<>(set.getCrdtId(), "1");

    // when:
    inputStream.onNext(command1);
    inputStream.onNext(command2);
    inputStream.onNext(command3);

    // then:
    assertThat(set, hasSize(2));
    assertThat(subscriber.valueCount(), is(2));
    subscriber.assertNotComplete();
    subscriber.assertNoErrors();
}
 
Example 5
Source File: ORSetTest.java    From wurmloch-crdt with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldHandleDuplicateCommands() {
    // given:
    final Processor<ORSet.ORSetCommand<String>, ORSet.ORSetCommand<String>> inputStream = ReplayProcessor.create();
    final TestSubscriber<CrdtCommand> subscriber = TestSubscriber.create();
    final ORSet<String> set = new ORSet<>("ID_1");
    set.subscribeTo(inputStream);
    set.subscribe(subscriber);

    final ORSet.AddCommand<String> command = new ORSet.AddCommand<>(set.getCrdtId(), new ORSet.Element<>("1", UUID.randomUUID()));

    // when:
    inputStream.onNext(command);
    inputStream.onNext(command);

    // then:
    assertThat(set, hasSize(1));
    assertThat(subscriber.valueCount(), is(1));
    subscriber.assertNotComplete();
    subscriber.assertNoErrors();
}
 
Example 6
Source File: USetTest.java    From wurmloch-crdt with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldHandleAddCommands() {
    // given:
    final UUID uuid1 = UUID.randomUUID();
    final UUID uuid2 = UUID.randomUUID();
    final Processor<USet.USetCommand<UUID>, USet.USetCommand<UUID>> inputStream = ReplayProcessor.create();
    final TestSubscriber<CrdtCommand> subscriber = TestSubscriber.create();
    final USet<UUID> set = new USet<>("ID_1");
    set.subscribeTo(inputStream);
    set.subscribe(subscriber);

    final USet.AddCommand<UUID> command1 = new USet.AddCommand<>(set.getCrdtId(), uuid1);
    final USet.AddCommand<UUID> command2 = new USet.AddCommand<>(set.getCrdtId(), uuid2);

    // when:
    inputStream.onNext(command1);
    inputStream.onNext(command2);

    // then:
    assertThat(set, hasSize(2));
    assertThat(subscriber.valueCount(), is(2));
    subscriber.assertNotComplete();
    subscriber.assertNoErrors();
}
 
Example 7
Source File: USetTest.java    From wurmloch-crdt with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldHandleRemoveCommands() {
    // given:
    final UUID uuid1 = UUID.randomUUID();
    final Processor<USet.USetCommand<UUID>, USet.USetCommand<UUID>> inputStream = ReplayProcessor.create();
    final TestSubscriber<CrdtCommand> subscriber = TestSubscriber.create();
    final USet<UUID> set = new USet<>("ID_1");
    set.subscribeTo(inputStream);
    set.subscribe(subscriber);

    final USet.AddCommand<UUID> command1 = new USet.AddCommand<>(set.getCrdtId(), uuid1);
    final USet.RemoveCommand<UUID> command2 = new USet.RemoveCommand<>(set.getCrdtId(), uuid1);

    // when:
    inputStream.onNext(command1);
    inputStream.onNext(command2);

    // then:
    assertThat(set, empty());
    assertThat(subscriber.valueCount(), is(2));
    subscriber.assertNotComplete();
    subscriber.assertNoErrors();
}
 
Example 8
Source File: ORSetTest.java    From wurmloch-crdt with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldHandleAddCommands() {
    // given:
    final Processor<ORSet.ORSetCommand<String>, ORSet.ORSetCommand<String>> inputStream = ReplayProcessor.create();
    final TestSubscriber<CrdtCommand> subscriber = TestSubscriber.create();
    final ORSet<String> set = new ORSet<>("ID_1");
    set.subscribeTo(inputStream);
    set.subscribe(subscriber);

    final ORSet.AddCommand<String> command1 = new ORSet.AddCommand<>(set.getCrdtId(), new ORSet.Element<>("1", UUID.randomUUID()));
    final ORSet.AddCommand<String> command2 = new ORSet.AddCommand<>(set.getCrdtId(), new ORSet.Element<>("2", UUID.randomUUID()));
    final ORSet.AddCommand<String> command3 = new ORSet.AddCommand<>(set.getCrdtId(), new ORSet.Element<>("1", UUID.randomUUID()));

    // when:
    inputStream.onNext(command1);
    inputStream.onNext(command2);
    inputStream.onNext(command3);

    // then:
    assertThat(set, hasSize(2));
    assertThat(subscriber.valueCount(), is(3));
    subscriber.assertNotComplete();
    subscriber.assertNoErrors();
}
 
Example 9
Source File: MVRegisterTest.java    From wurmloch-crdt with Apache License 2.0 6 votes vote down vote up
@Test
public void itShouldOverwriteOnlyPartialCommandsFromReceivedCommand() {
    // given
    final TestSubscriber<MVRegister.SetCommand<String>> outCommands1 = TestSubscriber.create();
    final Processor<MVRegister.SetCommand<String>, MVRegister.SetCommand<String>> inCommands2 = ReplayProcessor.create();
    final MVRegister<String> register1 = new MVRegister<>(NODE_ID_1, CRDT_ID);
    register1.subscribe(outCommands1);
    final MVRegister<String> register2 = new MVRegister<>(NODE_ID_2, CRDT_ID);
    register2.subscribeTo(inCommands2);

    register1.set("Hello World");
    register2.set("Goodbye World");
    inCommands2.onNext(outCommands1.values().get(0));

    // when
    register1.set("42");
    inCommands2.onNext(outCommands1.values().get(1));

    // then
    assertThat(register1.get(), containsInAnyOrder("42"));
    assertThat(register2.get(), containsInAnyOrder("42", "Goodbye World"));
}
 
Example 10
Source File: SerializedProcessorTest.java    From smallrye-mutiny with Apache License 2.0 5 votes vote down vote up
@Test(invocationCount = 20)
public void verifyOnNextOnErrorThreadSafety() {
    Exception failure = new Exception("boom");
    final Processor<Integer, Integer> processor = UnicastProcessor.<Integer> create().serialized();
    MultiAssertSubscriber<Integer> subscriber = MultiAssertSubscriber.create(100);
    processor.subscribe(subscriber);

    Runnable r1 = () -> {
        processor.onNext(1);
        processor.onComplete();
    };
    Runnable r2 = () -> processor.onError(failure);

    new Thread(r1).start();
    new Thread(r2).start();

    await().until(() -> !subscriber.items().isEmpty() || !subscriber.failures().isEmpty());

    subscriber
            .assertSubscribed()
            .assertTerminated();

    if (subscriber.items().size() != 0) {
        assertThat(subscriber.items()).containsExactly(1);
    } else {
        assertThat(subscriber.failures()).containsExactly(failure);
    }
}
 
Example 11
Source File: SubjectPerf.java    From akarnokd-misc with Apache License 2.0 5 votes vote down vote up
final void run(Processor<Integer, Integer> subject, Blackhole bh) {
    subject.subscribe(new PerfConsumer(bh));
    int e = count;
    for (int i = 0; i < e; i++) {
        subject.onNext(1);
    }
    subject.onComplete();
    bh.consume(subject);
}
 
Example 12
Source File: ORSetTest.java    From wurmloch-crdt with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldHandleRemoveCommands() {
    // given:
    final Processor<ORSet.ORSetCommand<String>, ORSet.ORSetCommand<String>> inputStream = ReplayProcessor.create();
    final TestSubscriber<CrdtCommand> subscriber = TestSubscriber.create();
    final ORSet<String> set = new ORSet<>("ID_1");
    set.subscribeTo(inputStream);
    set.subscribe(subscriber);

    final ORSet.Element<String> elem1 = new ORSet.Element<>("1", UUID.randomUUID());
    final ORSet.Element<String> elem2 = new ORSet.Element<>("1", UUID.randomUUID());
    final Set<ORSet.Element<String>> elements = new HashSet<>(Arrays.asList(elem1, elem2));
    final ORSet.AddCommand<String> command1 = new ORSet.AddCommand<>(set.getCrdtId(), elem1);
    final ORSet.AddCommand<String> command2 = new ORSet.AddCommand<>(set.getCrdtId(), elem2);
    final ORSet.RemoveCommand<String> command3 = new ORSet.RemoveCommand<>(set.getCrdtId(), elements);

    // when:
    inputStream.onNext(command1);
    inputStream.onNext(command2);
    inputStream.onNext(command3);

    // then:
    assertThat(set, empty());
    assertThat(subscriber.valueCount(), is(3));
    subscriber.assertNotComplete();
    subscriber.assertNoErrors();
}
 
Example 13
Source File: LWWRegisterTest.java    From wurmloch-crdt with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void itShouldIgnoreOlderValueFromReceivedCommands() {
    // given
    final TestSubscriber<LWWRegister.SetCommand<String>> outCommands1 = TestSubscriber.create();
    final TestSubscriber<LWWRegister.SetCommand<String>> outCommands2 = TestSubscriber.create();
    final Processor<LWWRegister.SetCommand<String>, LWWRegister.SetCommand<String>> inCommands3 = ReplayProcessor.create();
    final LWWRegister<String> register1 = new LWWRegister<>(NODE_ID_1, CRDT_ID);
    register1.subscribe(outCommands1);
    final LWWRegister<String> register2 = new LWWRegister<>(NODE_ID_2, CRDT_ID);
    register2.subscribe(outCommands2);
    register1.subscribeTo(register2);
    register2.subscribeTo(register1);
    final LWWRegister<String> register3 = new LWWRegister<>(NODE_ID_3, CRDT_ID);
    register3.subscribeTo(inCommands3);

    // when
    register1.set("Hello World");
    register2.set("Goodbye World");
    final LWWRegister.SetCommand<String> oldCommand = outCommands1.values().get(0);
    final LWWRegister.SetCommand<String> newCommand = outCommands2.values().get(1);
    inCommands3.onNext(newCommand);
    inCommands3.onNext(oldCommand);

    // then
    assertThat(register3.get(), is("Goodbye World"));
}
 
Example 14
Source File: MVRegisterTest.java    From wurmloch-crdt with Apache License 2.0 5 votes vote down vote up
@Test
public void itShouldIgnoreOlderValueFromReceivedCommands() {
    // given
    final TestSubscriber<MVRegister.SetCommand<String>> outCommands1 = TestSubscriber.create();
    final TestSubscriber<MVRegister.SetCommand<String>> outCommands2 = TestSubscriber.create();
    final Processor<MVRegister.SetCommand<String>, MVRegister.SetCommand<String>> inCommands3 = ReplayProcessor.create();
    final MVRegister<String> register1 = new MVRegister<>(NODE_ID_1, CRDT_ID);
    register1.subscribe(outCommands1);
    final MVRegister<String> register2 = new MVRegister<>(NODE_ID_2, CRDT_ID);
    register2.subscribe(outCommands2);
    register1.subscribeTo(register2);
    register2.subscribeTo(register1);
    final MVRegister<String> register3 = new MVRegister<>(NODE_ID_3, CRDT_ID);
    register3.subscribeTo(inCommands3);


    // when
    register1.set("Hello World");
    register2.set("Goodbye World");
    final MVRegister.SetCommand<String> oldCommand = outCommands1.values().get(0);
    final MVRegister.SetCommand<String> newCommand = outCommands2.values().get(1);
    inCommands3.onNext(newCommand);
    inCommands3.onNext(oldCommand);

    // then
    assertThat(register3.get(), contains("Goodbye World"));
}
 
Example 15
Source File: StatsdMeterRegistryTest.java    From micrometer with Apache License 2.0 5 votes vote down vote up
private Consumer<String> toSink(Processor<String, String> lines, int numLines) {
    AtomicInteger latch = new AtomicInteger(numLines);
    return l -> {
        lines.onNext(l);
        if (latch.decrementAndGet() == 0) {
            lines.onComplete();
        }
    };
}
 
Example 16
Source File: SubjectPerf.java    From akarnokd-misc with Apache License 2.0 5 votes vote down vote up
final void run(Processor<Integer, Integer> subject, Blackhole bh) {
    subject.subscribe(new PerfConsumer(bh));
    int e = count;
    for (int i = 0; i < e; i++) {
        subject.onNext(1);
    }
    subject.onComplete();
    bh.consume(subject);
}
 
Example 17
Source File: RxBusSenderBuilder.java    From RxBus2 with Apache License 2.0 5 votes vote down vote up
private boolean sendToUnboundBus(RxQueueKey key, Object event)
{
    boolean send = false;
    Processor processor = RxBus.getInstance().getProcessor(key, false);
    // only send event, if processor exists => this means someone has at least once subscribed to it
    if (processor != null)
    {
        if (mCast == null)
            processor.onNext(event);
        else
            processor.onNext(mCast.cast(event));
        send = true;
    }
    return send;
}
 
Example 18
Source File: SerializedProcessorTest.java    From smallrye-mutiny with Apache License 2.0 5 votes vote down vote up
@Test
public void testWithMultipleItems() {
    Processor<Integer, Integer> processor = UnicastProcessor.<Integer> create().serialized();
    MultiAssertSubscriber<Integer> subscriber = MultiAssertSubscriber.create(10);
    processor.subscribe(subscriber);

    Multi.createFrom().range(1, 11).subscribe(processor);

    subscriber
            .assertReceived(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
            .assertCompletedSuccessfully();

    processor.onNext(11);
    processor.onComplete();
}
 
Example 19
Source File: RGATest.java    From wurmloch-crdt with Apache License 2.0 4 votes vote down vote up
@Test
public void itShouldAddAndRemoveSingleElementConcurrently() {
    int i1 = 0;

    // given
    final Processor<RGA.RGACommand<String>, RGA.RGACommand<String>> inCommands1 = ReplayProcessor.create();
    final TestSubscriber<RGA.RGACommand<String>> outCommands1 = TestSubscriber.create();
    final RGA<String> rga1 = new RGA<>(NODE_ID_1, CRDT_ID);
    rga1.subscribeTo(inCommands1);
    rga1.subscribe(outCommands1);

    final Processor<RGA.RGACommand<String>, RGA.RGACommand<String>> inCommands2 = ReplayProcessor.create();
    final TestSubscriber<RGA.RGACommand<String>> outCommands2 = TestSubscriber.create();
    final RGA<String> rga2 = new RGA<>(NODE_ID_2, CRDT_ID);
    rga2.subscribeTo(inCommands2);
    rga2.subscribe(outCommands2);

    rga1.add("A");
    inCommands2.onNext(outCommands1.values().get(i1));
    int i2 = i1;

    // when
    rga1.remove(0);
    rga2.add(0, "B");
    inCommands2.onNext(outCommands1.values().get(++i1));
    inCommands1.onNext(outCommands2.values().get(++i2));

    // then
    assertThat(rga1, contains("B"));
    assertThat(rga2, contains("B"));

    // when
    rga1.remove(0);
    rga2.add(1, "C");
    inCommands2.onNext(outCommands1.values().get(i1 + 2));
    inCommands1.onNext(outCommands2.values().get(i2 + 2));

    // then
    assertThat(rga1, contains("C"));
    assertThat(rga2, contains("C"));
}
 
Example 20
Source File: RGATest.java    From wurmloch-crdt with Apache License 2.0 4 votes vote down vote up
@Test
public void itShouldAddElementsConcurrently() {
    int i1 = 0;
    int i2 = 0;

    // given
    final Processor<RGA.RGACommand<String>, RGA.RGACommand<String>> inCommands1 = ReplayProcessor.create();
    final TestSubscriber<RGA.RGACommand<String>> outCommands1 = TestSubscriber.create();
    final RGA<String> rga1 = new RGA<>(NODE_ID_1, CRDT_ID);
    rga1.subscribeTo(inCommands1);
    rga1.subscribe(outCommands1);

    final Processor<RGA.RGACommand<String>, RGA.RGACommand<String>> inCommands2 = ReplayProcessor.create();
    final TestSubscriber<RGA.RGACommand<String>> outCommands2 = TestSubscriber.create();
    final RGA<String> rga2 = new RGA<>(NODE_ID_2, CRDT_ID);
    rga2.subscribeTo(inCommands2);
    rga2.subscribe(outCommands2);

    // when
    rga1.add(0, "A1");
    rga2.add(0, "A2");
    inCommands2.onNext(outCommands1.values().get(i1));
    inCommands1.onNext(outCommands2.values().get(i2));

    // then
    assertThat(rga1, contains("A2", "A1"));
    assertThat(rga2, contains("A2", "A1"));

    // when
    rga1.add(0, "B1");
    rga2.add(0, "B2");
    inCommands2.onNext(outCommands1.values().get(i1+=2));
    inCommands1.onNext(outCommands2.values().get(i2+=2));

    // then
    assertThat(rga1, contains("B2", "B1", "A2", "A1"));
    assertThat(rga2, contains("B2", "B1", "A2", "A1"));

    // when
    rga1.add(1, "C1");
    rga2.add(1, "C2");
    inCommands2.onNext(outCommands1.values().get(i1+=2));
    inCommands1.onNext(outCommands2.values().get(i2+=2));

    // then
    assertThat(rga1, contains("B2", "C2", "C1", "B1", "A2", "A1"));
    assertThat(rga2, contains("B2", "C2", "C1", "B1", "A2", "A1"));

    // when
    rga1.add(6, "D1");
    rga2.add(6, "D2");
    inCommands2.onNext(outCommands1.values().get(i1 + 2));
    inCommands1.onNext(outCommands2.values().get(i2 + 2));

    // then
    assertThat(rga1, contains("B2", "C2", "C1", "B1", "A2", "A1", "D2", "D1"));
    assertThat(rga2, contains("B2", "C2", "C1", "B1", "A2", "A1", "D2", "D1"));
}