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

The following examples show how to use org.reactivestreams.Processor#subscribe() . 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 = 50)
public void verifyOnErrorThreadSafety() {
    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.onError(failure);
    Runnable r2 = () -> processor.onError(failure);

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

    subscriber
            .await()
            .assertSubscribed()
            .assertHasFailedWith(Exception.class, "boom");
}
 
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 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 3
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 4
Source File: SerializedProcessorTest.java    From smallrye-mutiny with Apache License 2.0 6 votes vote down vote up
@Test(invocationCount = 50)
public void verifyOnSubscribeOnSubscribeThreadSafety() throws InterruptedException {
    final Processor<Integer, Integer> processor = UnicastProcessor.<Integer> create().serialized();
    MultiAssertSubscriber<Integer> subscriber = MultiAssertSubscriber.create(100);
    processor.subscribe(subscriber);

    CountDownLatch latch = new CountDownLatch(2);
    Runnable r1 = () -> {
        processor.onSubscribe(new Subscriptions.EmptySubscription());
        latch.countDown();
    };
    Runnable r2 = () -> {
        processor.onSubscribe(new Subscriptions.EmptySubscription());
        latch.countDown();
    };

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

    latch.await();

    subscriber
            .assertSubscribed();
}
 
Example 5
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 6
Source File: SerializedProcessorTest.java    From smallrye-mutiny with Apache License 2.0 6 votes vote down vote up
@Test(invocationCount = 50)
public void verifyOnFailureOnFailureThreadSafety() {
    final Processor<Integer, Integer> processor = UnicastProcessor.<Integer> create().serialized();
    MultiAssertSubscriber<Integer> subscriber = MultiAssertSubscriber.create(100);
    processor.subscribe(subscriber);

    Runnable r1 = () -> processor.onError(new Exception("boom"));
    Runnable r2 = () -> processor.onError(new Exception("boom"));

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

    subscriber.await();
    subscriber
            .assertSubscribed()
            .assertTerminated()
            .assertHasFailedWith(Exception.class, "boom");
}
 
Example 7
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 8
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 9
Source File: AbstractListenerWriteFlushProcessor.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public <T> void onNext(AbstractListenerWriteFlushProcessor<T> processor,
		Publisher<? extends T> currentPublisher) {

	if (processor.changeState(this, RECEIVED)) {
		Processor<? super T, Void> currentProcessor = processor.createWriteProcessor();
		currentPublisher.subscribe(currentProcessor);
		currentProcessor.subscribe(new WriteResultSubscriber(processor));
	}
}
 
Example 10
Source File: AbstractListenerWriteFlushProcessor.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public <T> void onNext(AbstractListenerWriteFlushProcessor<T> processor,
		Publisher<? extends T> currentPublisher) {

	if (processor.changeState(this, RECEIVED)) {
		Processor<? super T, Void> currentProcessor = processor.createWriteProcessor();
		currentPublisher.subscribe(currentProcessor);
		currentProcessor.subscribe(new WriteResultSubscriber(processor));
	}
}
 
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: 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 13
Source File: EmitterProcessorTest.java    From reactor-core with Apache License 2.0 4 votes vote down vote up
@Test
public void testHotIdentityProcessor() throws InterruptedException {
	final int elements = 10000;
	CountDownLatch latch = new CountDownLatch(elements);

	Processor<Integer, Integer> processor = EmitterProcessor.create(1024);

	EmitterProcessor<Integer> stream = EmitterProcessor.create();
	FluxSink<Integer> session = stream.sink();
	stream.subscribe(processor);

	processor.subscribe(new CoreSubscriber<Integer>() {
		@Override
		public void onSubscribe(Subscription s) {
			s.request(elements);
		}

		@Override
		public void onNext(Integer integer) {
			latch.countDown();
		}

		@Override
		public void onError(Throwable t) {
			System.out.println("error! " + t);
		}

		@Override
		public void onComplete() {
			System.out.println("completed!");
			//latch.countDown();
		}
	});

	for (int i = 0; i < elements; i++) {
		session.next(i);
	}
	//stream.then();

	latch.await(8, TimeUnit.SECONDS);

	long count = latch.getCount();
	org.junit.Assert.assertTrue("Count > 0 : " + count + "  , Running on " + Schedulers.DEFAULT_POOL_SIZE + " CPU",
			latch.getCount() == 0);

	stream.onComplete();

}