Search in sources :

Example 1 with ChangeStreamEventMessage

use of org.springframework.data.mongodb.core.messaging.ChangeStreamTask.ChangeStreamEventMessage in project spring-data-mongodb by spring-projects.

the class ChangeStreamTests method resumeAtTimestampCorrectly.

// DATAMONGO-2012, DATAMONGO-2113
@Test
@MongoVersion(asOf = "4.0")
void resumeAtTimestampCorrectly() throws InterruptedException {
    CollectingMessageListener<ChangeStreamDocument<Document>, User> messageListener1 = new CollectingMessageListener<>();
    Subscription subscription1 = container.register(new ChangeStreamRequest<>(messageListener1, new ChangeStreamRequestOptions(null, "user", Duration.ofMillis(10), ChangeStreamOptions.builder().build())), User.class);
    awaitSubscription(subscription1);
    template.save(jellyBelly);
    // cluster timestamp is in seconds, so we need to wait at least one.
    Thread.sleep(1000);
    template.save(sugarSplashy);
    awaitMessages(messageListener1, 12);
    Instant resumeAt = ((ChangeStreamEventMessage) messageListener1.getLastMessage()).getTimestamp();
    template.save(huffyFluffy);
    awaitMessages(messageListener1, 3);
    CollectingMessageListener<ChangeStreamDocument<Document>, User> messageListener2 = new CollectingMessageListener<>();
    ChangeStreamRequest<User> subSequentRequest = // 
    ChangeStreamRequest.builder().collection(// 
    "user").resumeAt(// 
    resumeAt).publishTo(// 
    messageListener2).maxAwaitTime(// 
    Duration.ofMillis(10)).build();
    Subscription subscription2 = container.register(subSequentRequest, User.class);
    awaitSubscription(subscription2);
    awaitMessages(messageListener2, 2);
    List<User> messageBodies = messageListener2.getMessages().stream().map(Message::getBody).collect(Collectors.toList());
    assertThat(messageBodies).hasSize(2).doesNotContain(jellyBelly);
}
Also used : Instant(java.time.Instant) ChangeStreamDocument(com.mongodb.client.model.changestream.ChangeStreamDocument) ChangeStreamRequestOptions(org.springframework.data.mongodb.core.messaging.ChangeStreamRequest.ChangeStreamRequestOptions) ChangeStreamEventMessage(org.springframework.data.mongodb.core.messaging.ChangeStreamTask.ChangeStreamEventMessage) RepeatFailedTest(org.junitpioneer.jupiter.RepeatFailedTest) Test(org.junit.jupiter.api.Test) MongoVersion(org.springframework.data.mongodb.test.util.MongoVersion)

Aggregations

ChangeStreamDocument (com.mongodb.client.model.changestream.ChangeStreamDocument)1 Instant (java.time.Instant)1 Test (org.junit.jupiter.api.Test)1 RepeatFailedTest (org.junitpioneer.jupiter.RepeatFailedTest)1 ChangeStreamRequestOptions (org.springframework.data.mongodb.core.messaging.ChangeStreamRequest.ChangeStreamRequestOptions)1 ChangeStreamEventMessage (org.springframework.data.mongodb.core.messaging.ChangeStreamTask.ChangeStreamEventMessage)1 MongoVersion (org.springframework.data.mongodb.test.util.MongoVersion)1