Search in sources :

Example 6 with Disposable

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

the class ReactiveMongoRepositoryTests method shouldUseTailableCursorWithProjection.

// DATAMONGO-1444
@Test
public void shouldUseTailableCursorWithProjection() throws Exception {
    StepVerifier.create(// 
    template.dropCollection(Capped.class).then(// 
    template.createCollection(// 
    Capped.class, // 
    CollectionOptions.empty().size(1000).maxDocuments(100).capped()))).expectNextCount(// 
    1).verifyComplete();
    StepVerifier.create(template.insert(new Capped("value", Math.random()))).expectNextCount(1).verifyComplete();
    BlockingQueue<CappedProjection> documents = new LinkedBlockingDeque<>(100);
    Disposable disposable = cappedRepository.findProjectionByKey("value").doOnNext(documents::add).subscribe();
    CappedProjection projection1 = documents.poll(5, TimeUnit.SECONDS);
    assertThat(projection1, is(notNullValue()));
    assertThat(projection1.getRandom(), is(not(0)));
    StepVerifier.create(template.insert(new Capped("value", Math.random()))).expectNextCount(1).verifyComplete();
    CappedProjection projection2 = documents.poll(5, TimeUnit.SECONDS);
    assertThat(projection2, is(notNullValue()));
    assertThat(projection2.getRandom(), is(not(0)));
    assertThat(documents.isEmpty(), is(true));
    disposable.dispose();
}
Also used : Disposable(reactor.core.Disposable) LinkedBlockingDeque(java.util.concurrent.LinkedBlockingDeque) Test(org.junit.Test)

Example 7 with Disposable

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

the class ReactiveMongoTemplateTests method changeStreamEventsShouldBeResumedCorrectly.

// DATAMONGO-1803
@Test
public void changeStreamEventsShouldBeResumedCorrectly() throws InterruptedException {
    Assumptions.assumeThat(ReplicaSet.required().runsAsReplicaSet()).isTrue();
    StepVerifier.create(template.createCollection(Person.class)).expectNextCount(1).verifyComplete();
    BlockingQueue<ChangeStreamEvent<Person>> documents = new LinkedBlockingQueue<>(100);
    Disposable disposable = template.changeStream(Collections.emptyList(), Person.class, ChangeStreamOptions.empty(), "person").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);
    StepVerifier.create(template.save(person1)).expectNextCount(1).verifyComplete();
    StepVerifier.create(template.save(person2)).expectNextCount(1).verifyComplete();
    StepVerifier.create(template.save(person3)).expectNextCount(1).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(Collections.emptyList(), Person.class, ChangeStreamOptions.builder().resumeToken(resumeToken).build(), "person").doOnNext(resumeDocuments::add).subscribe();
    // just give it some time to link receive all events
    Thread.sleep(500);
    try {
        Assertions.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) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) VersionedPerson(org.springframework.data.mongodb.core.MongoTemplateTests.VersionedPerson) Test(org.junit.Test)

Example 8 with Disposable

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

the class ReactiveMongoTemplateTests method tailStreamsDataUntilCancellation.

// DATAMONGO-1444
@Test
public void tailStreamsDataUntilCancellation() throws InterruptedException {
    StepVerifier.create(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"))).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), is(notNullValue()));
    assertThat(documents.isEmpty(), is(true));
    // 
    StepVerifier.create(template.insert(new Document("random", Math.random()).append("key", "value"), "capped")).expectNextCount(// 
    1).verifyComplete();
    assertThat(documents.poll(5, TimeUnit.SECONDS), is(notNullValue()));
    disposable.dispose();
    // 
    StepVerifier.create(template.insert(new Document("random", Math.random()).append("key", "value"), "capped")).expectNextCount(// 
    1).verifyComplete();
    assertThat(documents.poll(1, TimeUnit.SECONDS), is(nullValue()));
}
Also used : Disposable(reactor.core.Disposable) Document(org.bson.Document) BsonDocument(org.bson.BsonDocument) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Test(org.junit.Test)

Example 9 with Disposable

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

the class ReactiveMongoTemplateTests method mapsReservedWordsCorrectly.

// DATAMONGO-1803
@Test
public void mapsReservedWordsCorrectly() throws InterruptedException {
    Assumptions.assumeThat(ReplicaSet.required().runsAsReplicaSet()).isTrue();
    StepVerifier.create(template.createCollection(Person.class)).expectNextCount(1).verifyComplete();
    BlockingQueue<ChangeStreamEvent<Person>> documents = new LinkedBlockingQueue<>(100);
    Disposable disposable = template.changeStream(newAggregation(Person.class, match(where("operationType").is("replace"))), Person.class, ChangeStreamOptions.empty(), "person").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);
    StepVerifier.create(template.save(person1)).expectNextCount(1).verifyComplete();
    StepVerifier.create(template.save(person2)).expectNextCount(1).verifyComplete();
    Person replacement = new Person(person2.getId(), "BDognoM");
    replacement.setAge(person2.getAge());
    StepVerifier.create(template.save(replacement)).expectNextCount(1).verifyComplete();
    // just give it some time to link receive all events
    Thread.sleep(500);
    try {
        Assertions.assertThat(documents.stream().map(ChangeStreamEvent::getBody).collect(Collectors.toList())).containsExactly(replacement);
    } finally {
        disposable.dispose();
    }
}
Also used : Disposable(reactor.core.Disposable) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) VersionedPerson(org.springframework.data.mongodb.core.MongoTemplateTests.VersionedPerson) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)9 Disposable (reactor.core.Disposable)9 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)7 VersionedPerson (org.springframework.data.mongodb.core.MongoTemplateTests.VersionedPerson)5 BsonDocument (org.bson.BsonDocument)4 Document (org.bson.Document)3 LinkedBlockingDeque (java.util.concurrent.LinkedBlockingDeque)2