use of reactor.test.StepVerifier in project spring-cloud-gcp by spring-cloud.
the class ReactiveFirestoreTransactionManagerTest method writeTransaction.
@Test
public void writeTransaction() {
FirestoreTemplate template = getFirestoreTemplate();
ReactiveFirestoreTransactionManager txManager = new ReactiveFirestoreTransactionManager(this.firestoreStub, this.parent);
TransactionalOperator operator = TransactionalOperator.create(txManager);
doAnswer(invocation -> {
CommitRequest commitRequest = invocation.getArgument(0);
StreamObserver<CommitResponse> streamObserver = invocation.getArgument(1);
assertThat(commitRequest.getTransaction()).isEqualTo(ByteString.copyFromUtf8("transaction1"));
assertThat(commitRequest.getWritesList().get(0).getUpdate().getName()).isEqualTo(this.parent + "/testEntities/" + "e2");
assertThat(commitRequest.getWritesList().get(1).getUpdate().getName()).isEqualTo(this.parent + "/testEntities/" + "e3");
assertThat(commitRequest.getWritesList().get(2).getDelete()).isEqualTo(this.parent + "/testEntities/" + "e3");
streamObserver.onNext(CommitResponse.newBuilder().build());
streamObserver.onCompleted();
return null;
}).when(this.firestoreStub).commit(any(), any());
template.findById(Mono.just("e1"), FirestoreTemplateTests.TestEntity.class).flatMap(testEntity -> template.save(new FirestoreTemplateTests.TestEntity("e2", 100L))).flatMap(testEntity -> template.save(new FirestoreTemplateTests.TestEntity("e3", 100L))).flatMap(testEntity -> template.delete(Mono.just(testEntity))).then().as(operator::transactional).as(StepVerifier::create).verifyComplete();
verify(this.firestoreStub).beginTransaction(any(), any());
verify(this.firestoreStub).commit(any(), any());
GetDocumentRequest request1 = GetDocumentRequest.newBuilder().setName(this.parent + "/testEntities/" + "e1").setTransaction(ByteString.copyFromUtf8("transaction1")).build();
verify(this.firestoreStub, times(1)).getDocument(eq(request1), any());
}
use of reactor.test.StepVerifier in project reactor-core by reactor.
the class FluxPublishTest method prematureOnComplete.
@Test
public void prematureOnComplete() {
Sinks.Many<Flux<String>> sink = Sinks.unsafe().many().multicast().onBackpressureBuffer(Queues.SMALL_BUFFER_SIZE, false);
Flux.just("ALPHA", "BRAVO", "CHARLIE", "DELTA", "ALPHA", "BRAVO", "CHARLIE", "DELTA", "ALPHA", "BRAVO", "CHARLIE", "DELTA").log("stream.incoming").windowWhile(s -> !"DELTA".equals(s), 1).subscribe(v -> sink.emitNext(v, FAIL_FAST), e -> sink.emitError(e, FAIL_FAST), () -> sink.emitComplete(FAIL_FAST));
AtomicInteger windowIndex = new AtomicInteger(0);
AtomicInteger nextIndex = new AtomicInteger(0);
sink.asFlux().next().flatMapMany(flux -> flux.takeWhile(s -> !"CHARLIE".equals(s)).log(String.format("stream.window.%d", windowIndex.getAndIncrement()))).log(String.format("stream.next.%d", nextIndex.getAndIncrement())).as(StepVerifier::create).expectNextCount(2).verifyComplete();
sink.asFlux().next().flatMapMany(flux -> flux.takeWhile(s -> !"CHARLIE".equals(s)).log(String.format("stream.window.%d", windowIndex.getAndIncrement()))).log(String.format("stream.next.%d", nextIndex.getAndIncrement())).as(StepVerifier::create).expectNextCount(2).verifyComplete();
sink.asFlux().next().flatMapMany(flux -> flux.takeWhile(s -> !"CHARLIE".equals(s)).log(String.format("stream.window.%d", windowIndex.getAndIncrement()))).log(String.format("stream.next.%d", nextIndex.getAndIncrement())).as(StepVerifier::create).expectNextCount(2).verifyComplete();
}
use of reactor.test.StepVerifier in project reactor-core by reactor.
the class ParallelMergeOrderedTest method dealsWithPrefetchLargerThanSmallBufferSize.
// see https://github.com/reactor/reactor-core/issues/1958
@Test
void dealsWithPrefetchLargerThanSmallBufferSize() {
// parallelism must be > 1 to expose issue
int parallelism = 2;
int bufferS = Queues.SMALL_BUFFER_SIZE;
// if orderedPrefetch > bufferS then operator used to drop elements and eventually hang
int orderedPrefetch = bufferS + 128;
final AtomicInteger prev = new AtomicInteger(-1);
Flux.range(0, 200_000).subscribeOn(Schedulers.newSingle("init", true)).parallel(parallelism, bufferS).runOn(Schedulers.newParallel("process", parallelism, true), bufferS).map(i -> i).ordered(Comparator.comparing(i -> i), orderedPrefetch).as(StepVerifier::create).thenConsumeWhile(current -> {
int previous = prev.getAndSet(current);
try {
assertThat(current).withFailMessage("elements dropped: prev: %d, next: %d, lost: %d\n", previous, current, current - previous).isEqualTo(previous + 1);
} catch (AssertionError ae) {
ae.printStackTrace();
}
return true;
}).expectComplete().verify(// should run in 3s
Duration.ofSeconds(5));
}
use of reactor.test.StepVerifier in project reactor-core by reactor.
the class BoundedElasticSchedulerTest method testGh1973.
// gh-1973 smoke test
@Test
@Tag("slow")
public void testGh1973() throws InterruptedException {
Scheduler scheduler = afterTest.autoDispose(Schedulers.newBoundedElastic(3, 100000, "subscriberElastic", 600, true));
LinkedList<MonoSink<String>> listeners = new LinkedList<>();
List<Disposable> scheduled = new LinkedList<>();
ExecutorService producer = startProducer(listeners);
Consumer<MonoSink<String>> addListener = sink -> {
try {
Thread.sleep(200);
} catch (InterruptedException e) {
LOGGER.trace("listener cancelled");
}
listeners.add(sink);
sink.onDispose(() -> listeners.remove(sink));
};
for (int i = 0; i < 50; i++) {
scheduled.add(Mono.create(addListener).subscribeOn(scheduler).subscribe(LOGGER::info));
}
Thread.sleep(1000);
scheduled.forEach(Disposable::dispose);
Thread.sleep(1000);
Mono.<String>create(sink -> {
try {
Thread.sleep(200);
} catch (InterruptedException e) {
LOGGER.warn("last listener improperly cancelled");
}
listeners.add(sink);
sink.onDispose(() -> listeners.remove(sink));
}).subscribeOn(scheduler).map(res -> res + " the end").doOnNext(LOGGER::info).as(StepVerifier::create).assertNext(n -> assertThat(n).endsWith(" the end")).expectComplete().verify(Duration.ofSeconds(5));
}
use of reactor.test.StepVerifier in project reactor-core by reactor.
the class SchedulersTest method handleErrorWithJvmFatalForwardsToUncaughtHandlerFusedCallable.
@Test
public void handleErrorWithJvmFatalForwardsToUncaughtHandlerFusedCallable() {
AtomicBoolean handlerCaught = new AtomicBoolean();
Scheduler scheduler = Schedulers.fromExecutorService(Executors.newSingleThreadExecutor(r -> {
Thread thread = new Thread(r);
thread.setUncaughtExceptionHandler((t, ex) -> {
handlerCaught.set(true);
System.err.println("from uncaught handler: " + ex.toString());
});
return thread;
}));
final StepVerifier stepVerifier = StepVerifier.create(Mono.<String>fromCallable(() -> {
throw new StackOverflowError("boom");
}).subscribeOn(scheduler)).expectFusion().expectErrorMessage("boom");
// the exception is still fatal, so the StepVerifier should time out.
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> stepVerifier.verify(Duration.ofMillis(100))).withMessageStartingWith("VerifySubscriber timed out on ");
// nonetheless, the uncaught exception handler should have been invoked
assertThat(handlerCaught).as("uncaughtExceptionHandler used").isTrue();
}
Aggregations