use of io.smallrye.mutiny.subscription.MultiEmitter in project smallrye-mutiny by smallrye.
the class MultiCreateFromEmitterTest method testWithDropBackPressure.
@Test
public void testWithDropBackPressure() {
AssertSubscriber<Integer> subscriber = Multi.createFrom().<Integer>emitter(emitter -> {
IntStream.range(0, 1000).forEach(emitter::emit);
emitter.complete();
}, BackPressureStrategy.DROP).subscribe().withSubscriber(AssertSubscriber.create(20)).request(Long.MAX_VALUE).assertCompleted();
// 20 because the 20 first are consumed, others are dropped
assertThat(subscriber.getItems()).hasSize(20);
subscriber = Multi.createFrom().<Integer>emitter(emitter -> {
IntStream.range(0, 1000).forEach(emitter::emit);
emitter.complete();
}, BackPressureStrategy.DROP).subscribe().withSubscriber(AssertSubscriber.create()).request(20).request(Long.MAX_VALUE).assertCompleted();
assertThat(subscriber.getItems()).isEmpty();
Multi.createFrom().<Integer>emitter(MultiEmitter::complete, BackPressureStrategy.DROP).subscribe().withSubscriber(AssertSubscriber.create(20)).assertCompleted().assertHasNotReceivedAnyItem();
Multi.createFrom().<Integer>emitter(emitter -> {
IntStream.range(0, 1000).forEach(emitter::emit);
emitter.fail(new IOException("boom"));
}, BackPressureStrategy.DROP).subscribe().withSubscriber(AssertSubscriber.create()).request(20).request(Long.MAX_VALUE).assertFailedWith(IOException.class, "boom").assertHasNotReceivedAnyItem();
}
use of io.smallrye.mutiny.subscription.MultiEmitter in project smallrye-mutiny by smallrye.
the class MultiCreateFromEmitterTest method testWithMoreRequestsThanValue.
@Test
public void testWithMoreRequestsThanValue() {
AtomicInteger terminated = new AtomicInteger();
AtomicReference<MultiEmitter<? super Integer>> reference = new AtomicReference<>();
Multi.createFrom().<Integer>emitter(emitter -> {
reference.set(emitter);
emitter.onTermination(terminated::incrementAndGet);
emitter.emit(1);
emitter.emit(2);
emitter.emit(3);
emitter.complete();
}).subscribe().withSubscriber(AssertSubscriber.create()).assertSubscribed().run(() -> {
assertThat(reference.get()).isNotNull();
assertThat(reference.get().requested()).isEqualTo(0);
}).request(2).run(() -> {
// Already emitted
assertThat(reference.get().requested()).isEqualTo(0);
}).assertNotTerminated().assertItems(1, 2).request(10).assertItems(1, 2, 3).run(() -> {
assertThat(reference.get().requested()).isEqualTo(10);
}).assertCompleted();
assertThat(terminated).hasValue(1);
}
use of io.smallrye.mutiny.subscription.MultiEmitter in project smallrye-mutiny by smallrye.
the class MultiCreateFromEmitterTest method testWithRequests.
@Test
public void testWithRequests() {
AtomicInteger terminated = new AtomicInteger();
AtomicReference<MultiEmitter<? super Integer>> reference = new AtomicReference<>();
Multi.createFrom().<Integer>emitter(emitter -> {
reference.set(emitter);
emitter.onTermination(terminated::incrementAndGet);
emitter.emit(1);
emitter.emit(2);
emitter.emit(3);
emitter.complete();
}).subscribe().withSubscriber(AssertSubscriber.create()).assertSubscribed().run(() -> {
assertThat(reference.get()).isNotNull();
assertThat(reference.get().requested()).isEqualTo(0);
}).request(2).run(() -> {
// Already emitted
assertThat(reference.get().requested()).isEqualTo(0);
}).assertNotTerminated().assertItems(1, 2).request(2).assertItems(1, 2, 3).assertCompleted();
assertThat(terminated).hasValue(1);
}
use of io.smallrye.mutiny.subscription.MultiEmitter in project smallrye-mutiny by smallrye.
the class MultiOnOverflowTest method testDropCallFailedUni.
@Test
public void testDropCallFailedUni() {
AssertSubscriber<Integer> sub = AssertSubscriber.create();
AtomicReference<MultiEmitter<? super Integer>> emitter = new AtomicReference<>();
Multi<Integer> multi = Multi.createFrom().emitter((Consumer<MultiEmitter<? super Integer>>) emitter::set).onOverflow().call(item -> Uni.createFrom().failure(new IOException("boom :: " + item))).drop();
multi.subscribe(sub);
emitter.get().emit(1);
sub.assertFailedWith(IOException.class, "boom :: 1");
}
use of io.smallrye.mutiny.subscription.MultiEmitter in project smallrye-mutiny by smallrye.
the class MultiCreateFromEmitterTest method testWithLatestBackPressure.
@Test
public void testWithLatestBackPressure() {
AssertSubscriber<Integer> subscriber = Multi.createFrom().<Integer>emitter(emitter -> {
IntStream.range(0, 1000).forEach(emitter::emit);
emitter.complete();
}, BackPressureStrategy.LATEST).subscribe().withSubscriber(AssertSubscriber.create(20)).request(Long.MAX_VALUE).assertCompleted();
// 21 because the 20 first are consumed, and then only the latest is kept.
assertThat(subscriber.getItems()).hasSize(21);
subscriber = Multi.createFrom().<Integer>emitter(emitter -> {
IntStream.range(0, 1000).forEach(emitter::emit);
emitter.complete();
}, BackPressureStrategy.LATEST).subscribe().withSubscriber(AssertSubscriber.create()).request(20).request(Long.MAX_VALUE).assertCompleted();
assertThat(subscriber.getItems()).hasSize(1).containsExactly(999);
Multi.createFrom().<Integer>emitter(MultiEmitter::complete, BackPressureStrategy.LATEST).subscribe().withSubscriber(AssertSubscriber.create(20)).assertCompleted().assertHasNotReceivedAnyItem();
subscriber = Multi.createFrom().<Integer>emitter(emitter -> {
IntStream.range(0, 1000).forEach(emitter::emit);
emitter.fail(new IOException("boom"));
}, BackPressureStrategy.LATEST).subscribe().withSubscriber(AssertSubscriber.create()).request(20).request(Long.MAX_VALUE).assertFailedWith(IOException.class, "boom");
assertThat(subscriber.getItems()).hasSize(1).containsExactly(999);
}
Aggregations