Search in sources :

Example 66 with Disposable

use of reactor.core.Disposable in project reactor-core by reactor.

the class WorkQueueProcessorTest method cancelDoesNotHang.

@Test(timeout = 15000L)
public void cancelDoesNotHang() throws Exception {
    WorkQueueProcessor<String> wq = WorkQueueProcessor.create();
    Disposable d = wq.subscribe();
    Assert.assertTrue(wq.downstreamCount() == 1);
    d.dispose();
    while (wq.downstreamCount() != 0 && Thread.activeCount() > 2) {
    }
}
Also used : Disposable(reactor.core.Disposable) Test(org.junit.Test)

Example 67 with Disposable

use of reactor.core.Disposable in project spring-data-mongodb by spring-projects.

the class ReactiveMongoTemplateTests method resumesAtBsonTimestampCorrectly.

// DATAMONGO-2115
@Test
@EnableIfMongoServerVersion(isGreaterThanEqual = "4.0")
@EnableIfReplicaSetAvailable
void resumesAtBsonTimestampCorrectly() throws InterruptedException {
    template.createCollection(Person.class).as(StepVerifier::create).expectNextCount(1).verifyComplete();
    BlockingQueue<ChangeStreamEvent<Person>> documents = new LinkedBlockingQueue<>(100);
    Disposable disposable = template.changeStream("person", ChangeStreamOptions.empty(), Person.class).doOnNext(documents::add).subscribe();
    // just give it some time to link to the collection.
    Thread.sleep(500);
    Person person1 = new Person("Spring", 38);
    Person person2 = new Person("Data", 37);
    Person person3 = new Person("MongoDB", 39);
    // 
    template.save(person1).delayElement(Duration.ofSeconds(1)).as(// 
    StepVerifier::create).expectNextCount(// 
    1).verifyComplete();
    // 
    template.save(person2).as(// 
    StepVerifier::create).expectNextCount(// 
    1).verifyComplete();
    // skip first
    documents.take();
    // take 2nd
    BsonTimestamp resumeAt = documents.take().getBsonTimestamp();
    disposable.dispose();
    template.save(person3).as(StepVerifier::create).expectNextCount(1).verifyComplete();
    template.changeStream("person", ChangeStreamOptions.builder().resumeAt(resumeAt).build(), Person.class).map(// 
    ChangeStreamEvent::getBody).buffer(// 
    2).as(// 
    StepVerifier::create).consumeNextWith(actual -> {
        assertThat(actual).containsExactly(person2, person3);
    }).thenCancel().verify();
}
Also used : Disposable(reactor.core.Disposable) StepVerifier(reactor.test.StepVerifier) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) VersionedPerson(org.springframework.data.mongodb.core.MongoTemplateTests.VersionedPerson) BsonTimestamp(org.bson.BsonTimestamp) EnableIfMongoServerVersion(org.springframework.data.mongodb.test.util.EnableIfMongoServerVersion) EnableIfReplicaSetAvailable(org.springframework.data.mongodb.test.util.EnableIfReplicaSetAvailable) Test(org.junit.jupiter.api.Test)

Example 68 with Disposable

use of reactor.core.Disposable in project spring-data-mongodb by spring-projects.

the class ReactiveMongoTemplateTests method tailStreamsDataUntilCancellation.

// DATAMONGO-1444
@Test
void tailStreamsDataUntilCancellation() throws InterruptedException {
    template.dropCollection("capped").then(// 
    template.createCollection(// 
    "capped", CollectionOptions.empty().size(1000).maxDocuments(10).capped())).then(// 
    template.insert(// 
    new Document("random", Math.random()).append("key", "value"), // 
    "capped")).as(// 
    StepVerifier::create).expectNextCount(1).verifyComplete();
    BlockingQueue<Document> documents = new LinkedBlockingQueue<>(1000);
    Flux<Document> capped = template.tail(null, Document.class, "capped");
    Disposable disposable = capped.doOnNext(documents::add).subscribe();
    assertThat(documents.poll(5, TimeUnit.SECONDS)).isNotNull();
    assertThat(documents).isEmpty();
    // 
    template.insert(new Document("random", Math.random()).append("key", "value"), "capped").as(// 
    StepVerifier::create).expectNextCount(// 
    1).verifyComplete();
    assertThat(documents.poll(5, TimeUnit.SECONDS)).isNotNull();
    disposable.dispose();
    // 
    template.insert(new Document("random", Math.random()).append("key", "value"), "capped").as(// 
    StepVerifier::create).expectNextCount(// 
    1).verifyComplete();
    assertThat(documents.poll(1, TimeUnit.SECONDS)).isNull();
}
Also used : Disposable(reactor.core.Disposable) Document(org.bson.Document) BsonDocument(org.bson.BsonDocument) StepVerifier(reactor.test.StepVerifier) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Test(org.junit.jupiter.api.Test)

Example 69 with Disposable

use of reactor.core.Disposable in project spring-data-mongodb by spring-projects.

the class ReactiveMongoTemplateTests method changeStreamEventsShouldBeResumedCorrectly.

// DATAMONGO-1803
@Test
@Disabled("Heavily relying on timing assumptions. Cannot test message resumption properly. Too much race for too little time in between.")
@EnableIfReplicaSetAvailable
void changeStreamEventsShouldBeResumedCorrectly() throws InterruptedException {
    template.createCollection(Person.class).as(StepVerifier::create).expectNextCount(1).verifyComplete();
    BlockingQueue<ChangeStreamEvent<Person>> documents = new LinkedBlockingQueue<>(100);
    Disposable disposable = template.changeStream("person", ChangeStreamOptions.empty(), Person.class).doOnNext(documents::add).subscribe();
    // just give it some time to link to the collection.
    Thread.sleep(500);
    Person person1 = new Person("Spring", 38);
    Person person2 = new Person("Data", 37);
    Person person3 = new Person("MongoDB", 39);
    // 
    Flux.merge(template.insert(person1), template.insert(person2), template.insert(person3)).as(// 
    StepVerifier::create).expectNextCount(// 
    3).verifyComplete();
    // just give it some time to link receive all events
    Thread.sleep(500);
    disposable.dispose();
    BsonDocument resumeToken = documents.take().getRaw().getResumeToken();
    BlockingQueue<ChangeStreamEvent<Person>> resumeDocuments = new LinkedBlockingQueue<>(100);
    template.changeStream("person", ChangeStreamOptions.builder().resumeToken(resumeToken).build(), Person.class).doOnNext(resumeDocuments::add).subscribe();
    // just give it some time to link receive all events
    Thread.sleep(500);
    try {
        assertThat(resumeDocuments.stream().map(ChangeStreamEvent::getBody).collect(Collectors.toList())).containsExactly(person2, person3);
    } finally {
        disposable.dispose();
    }
}
Also used : Disposable(reactor.core.Disposable) BsonDocument(org.bson.BsonDocument) StepVerifier(reactor.test.StepVerifier) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) VersionedPerson(org.springframework.data.mongodb.core.MongoTemplateTests.VersionedPerson) EnableIfReplicaSetAvailable(org.springframework.data.mongodb.test.util.EnableIfReplicaSetAvailable) Test(org.junit.jupiter.api.Test) Disabled(org.junit.jupiter.api.Disabled)

Example 70 with Disposable

use of reactor.core.Disposable in project spring-data-mongodb by spring-projects.

the class ReactiveMongoTemplateTests method mapsReservedWordsCorrectly.

// DATAMONGO-1803
@Test
@EnableIfReplicaSetAvailable
void mapsReservedWordsCorrectly() throws InterruptedException {
    template.dropCollection(Person.class).onErrorResume(it -> Mono.empty()).as(StepVerifier::create).verifyComplete();
    template.createCollection(Person.class).as(StepVerifier::create).expectNextCount(1).verifyComplete();
    BlockingQueue<ChangeStreamEvent<Person>> documents = new LinkedBlockingQueue<>(100);
    Disposable disposable = template.changeStream("person", ChangeStreamOptions.builder().filter(newAggregation(Person.class, match(where("operationType").is("replace")))).build(), Person.class).doOnNext(documents::add).subscribe();
    // just give it some time to link to the collection.
    Thread.sleep(500);
    Person person1 = new Person("Spring", 38);
    Person person2 = new Person("Data", 37);
    // 
    Flux.merge(template.insert(person1), template.insert(person2)).as(// 
    StepVerifier::create).expectNextCount(// 
    2).verifyComplete();
    Person replacement = new Person(person2.getId(), "BDognoM");
    replacement.setAge(person2.getAge());
    // 
    template.save(replacement).as(// 
    StepVerifier::create).expectNextCount(// 
    1).verifyComplete();
    // just give it some time to link receive all events
    Thread.sleep(500);
    try {
        assertThat(documents.stream().map(ChangeStreamEvent::getBody).collect(Collectors.toList())).containsExactly(replacement);
    } finally {
        disposable.dispose();
    }
}
Also used : Disposable(reactor.core.Disposable) StepVerifier(reactor.test.StepVerifier) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) VersionedPerson(org.springframework.data.mongodb.core.MongoTemplateTests.VersionedPerson) EnableIfReplicaSetAvailable(org.springframework.data.mongodb.test.util.EnableIfReplicaSetAvailable) Test(org.junit.jupiter.api.Test)

Aggregations

Disposable (reactor.core.Disposable)118 Test (org.junit.jupiter.api.Test)99 CountDownLatch (java.util.concurrent.CountDownLatch)33 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)32 StepVerifier (reactor.test.StepVerifier)29 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)25 AtomicReference (java.util.concurrent.atomic.AtomicReference)18 Duration (java.time.Duration)15 List (java.util.List)15 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)15 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)15 Subscription (org.reactivestreams.Subscription)15 TimeUnit (java.util.concurrent.TimeUnit)14 Timeout (org.junit.jupiter.api.Timeout)13 CoreSubscriber (reactor.core.CoreSubscriber)12 ArrayList (java.util.ArrayList)11 Arrays (java.util.Arrays)11 Disabled (org.junit.jupiter.api.Disabled)11 Scannable (reactor.core.Scannable)11 Fuseable (reactor.core.Fuseable)10