Search in sources :

Example 6 with EnableIfReplicaSetAvailable

use of org.springframework.data.mongodb.test.util.EnableIfReplicaSetAvailable in project spring-data-mongodb by spring-projects.

the class DefaultMessageListenerContainerTests method shouldStartReceivingMessagesWhenContainerStarts.

// DATAMONGO-1803
@Test
@EnableIfReplicaSetAvailable
public void shouldStartReceivingMessagesWhenContainerStarts() throws InterruptedException {
    MessageListenerContainer container = new DefaultMessageListenerContainer(template);
    Subscription subscription = container.register(new ChangeStreamRequest(messageListener, options()), Document.class);
    collection.insertOne(new Document("_id", "id-1").append("value", "foo"));
    Thread.sleep(200);
    container.start();
    awaitSubscription(subscription);
    Document expected = new Document("_id", "id-2").append("value", "bar");
    collection.insertOne(expected);
    awaitMessages(messageListener);
    container.stop();
    assertThat(messageListener.getMessages().stream().map(Message::getBody).collect(Collectors.toList())).containsExactly(expected);
}
Also used : Document(org.bson.Document) ChangeStreamDocument(com.mongodb.client.model.changestream.ChangeStreamDocument) EnableIfReplicaSetAvailable(org.springframework.data.mongodb.test.util.EnableIfReplicaSetAvailable) Test(org.junit.jupiter.api.Test)

Example 7 with EnableIfReplicaSetAvailable

use of org.springframework.data.mongodb.test.util.EnableIfReplicaSetAvailable in project spring-data-mongodb by spring-projects.

the class DefaultMessageListenerContainerTests method shouldNotifyErrorHandlerOnErrorInListener.

// DATAMONGO-2322
@Test
@EnableIfReplicaSetAvailable
public void shouldNotifyErrorHandlerOnErrorInListener() throws InterruptedException {
    ErrorHandler errorHandler = mock(ErrorHandler.class);
    MessageListenerContainer container = new DefaultMessageListenerContainer(template);
    AtomicBoolean thrownException = new AtomicBoolean();
    Subscription subscription = container.register(new ChangeStreamRequest(message -> {
        try {
            if (thrownException.compareAndSet(false, true)) {
                throw new IllegalStateException("Boom!");
            }
        } finally {
            messageListener.onMessage(message);
        }
    }, options()), Person.class, errorHandler);
    container.start();
    awaitSubscription(subscription, TIMEOUT);
    collection.insertOne(new Document("_id", "id-1").append("firstname", "foo"));
    collection.insertOne(new Document("_id", "id-2").append("firstname", "bar"));
    awaitMessages(messageListener, 2, TIMEOUT);
    verify(errorHandler, atLeast(1)).handleError(any(IllegalStateException.class));
    assertThat(messageListener.getTotalNumberMessagesReceived()).isEqualTo(2);
}
Also used : Document(org.bson.Document) BeforeEach(org.junit.jupiter.api.BeforeEach) DataAccessException(org.springframework.dao.DataAccessException) ArgumentMatchers(org.mockito.ArgumentMatchers) MongoClient(com.mongodb.client.MongoClient) MongoCollection(com.mongodb.client.MongoCollection) ChangeStreamOptions(org.springframework.data.mongodb.core.ChangeStreamOptions) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ChangeStreamDocument(com.mongodb.client.model.changestream.ChangeStreamDocument) MongoDatabaseFactory(org.springframework.data.mongodb.MongoDatabaseFactory) ErrorHandler(org.springframework.util.ErrorHandler) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) CreateCollectionOptions(com.mongodb.client.model.CreateCollectionOptions) Duration(java.time.Duration) SimpleAsyncTaskExecutor(org.springframework.core.task.SimpleAsyncTaskExecutor) Assertions(org.assertj.core.api.Assertions) RequestOptions(org.springframework.data.mongodb.core.messaging.SubscriptionRequest.RequestOptions) MongoTemplate(org.springframework.data.mongodb.core.MongoTemplate) MongoTestUtils(org.springframework.data.mongodb.test.util.MongoTestUtils) Template(org.springframework.data.mongodb.test.util.Template) Collectors(java.util.stream.Collectors) Client(org.springframework.data.mongodb.test.util.Client) Test(org.junit.jupiter.api.Test) Mockito(org.mockito.Mockito) ChangeStreamRequestOptions(org.springframework.data.mongodb.core.messaging.ChangeStreamRequest.ChangeStreamRequestOptions) Data(lombok.Data) MongoTemplateExtension(org.springframework.data.mongodb.test.util.MongoTemplateExtension) Id(org.springframework.data.annotation.Id) EnableIfMongoServerVersion(org.springframework.data.mongodb.test.util.EnableIfMongoServerVersion) SubscriptionUtils(org.springframework.data.mongodb.core.messaging.SubscriptionUtils) MongoServerCondition(org.springframework.data.mongodb.test.util.MongoServerCondition) EnableIfReplicaSetAvailable(org.springframework.data.mongodb.test.util.EnableIfReplicaSetAvailable) ErrorHandler(org.springframework.util.ErrorHandler) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Document(org.bson.Document) ChangeStreamDocument(com.mongodb.client.model.changestream.ChangeStreamDocument) EnableIfReplicaSetAvailable(org.springframework.data.mongodb.test.util.EnableIfReplicaSetAvailable) Test(org.junit.jupiter.api.Test)

Example 8 with EnableIfReplicaSetAvailable

use of org.springframework.data.mongodb.test.util.EnableIfReplicaSetAvailable 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 9 with EnableIfReplicaSetAvailable

use of org.springframework.data.mongodb.test.util.EnableIfReplicaSetAvailable 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 10 with EnableIfReplicaSetAvailable

use of org.springframework.data.mongodb.test.util.EnableIfReplicaSetAvailable 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

Test (org.junit.jupiter.api.Test)13 EnableIfReplicaSetAvailable (org.springframework.data.mongodb.test.util.EnableIfReplicaSetAvailable)13 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)8 VersionedPerson (org.springframework.data.mongodb.core.MongoTemplateTests.VersionedPerson)8 Disposable (reactor.core.Disposable)8 StepVerifier (reactor.test.StepVerifier)8 EnableIfMongoServerVersion (org.springframework.data.mongodb.test.util.EnableIfMongoServerVersion)6 Document (org.bson.Document)4 Disabled (org.junit.jupiter.api.Disabled)4 ChangeStreamDocument (com.mongodb.client.model.changestream.ChangeStreamDocument)3 BsonDocument (org.bson.BsonDocument)2 MongoTransactionManager (org.springframework.data.mongodb.MongoTransactionManager)2 Person (org.springframework.data.mongodb.repository.Person)2 TransactionTemplate (org.springframework.transaction.support.TransactionTemplate)2 MongoClient (com.mongodb.client.MongoClient)1 MongoCollection (com.mongodb.client.MongoCollection)1 CreateCollectionOptions (com.mongodb.client.model.CreateCollectionOptions)1 Duration (java.time.Duration)1 Instant (java.time.Instant)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1