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);
}
Aggregations