use of org.springframework.data.mongodb.test.util.EnableIfReplicaSetAvailable in project spring-data-mongodb by spring-projects.
the class ReactiveMongoTemplateTests method watchesDatabaseCorrectly.
// DATAMONGO-2012
@Test
@EnableIfMongoServerVersion(isGreaterThanEqual = "4.0")
@EnableIfReplicaSetAvailable
void watchesDatabaseCorrectly() throws InterruptedException {
template.dropCollection(Person.class).onErrorResume(it -> Mono.empty()).as(StepVerifier::create).verifyComplete();
template.dropCollection("personX").onErrorResume(it -> Mono.empty()).as(StepVerifier::create).verifyComplete();
template.createCollection(Person.class).as(StepVerifier::create).expectNextCount(1).verifyComplete();
template.createCollection("personX").as(StepVerifier::create).expectNextCount(1).verifyComplete();
BlockingQueue<ChangeStreamEvent<Person>> documents = new LinkedBlockingQueue<>(100);
Disposable disposable = template.changeStream(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).as(//
StepVerifier::create).expectNextCount(//
1).verifyComplete();
//
template.save(person2).as(//
StepVerifier::create).expectNextCount(//
1).verifyComplete();
//
template.save(person3, "personX").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(person1, person2, person3);
} finally {
disposable.dispose();
}
}
use of org.springframework.data.mongodb.test.util.EnableIfReplicaSetAvailable in project spring-data-mongodb by spring-projects.
the class ReactiveMongoTemplateTests method changeStreamEventsShouldBeEmittedCorrectly.
// 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 changeStreamEventsShouldBeEmittedCorrectly() throws InterruptedException {
template.createCollection(Person.class).as(StepVerifier::create).expectNextCount(1).verifyComplete();
BlockingQueue<ChangeStreamEvent<Document>> documents = new LinkedBlockingQueue<>(100);
Disposable disposable = template.changeStream("person", ChangeStreamOptions.empty(), Document.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", 39);
Person person3 = new Person("MongoDB", 37);
//
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);
try {
assertThat(documents.stream().map(ChangeStreamEvent::getBody).collect(Collectors.toList())).hasSize(3).allMatch(val -> val instanceof Document);
} finally {
disposable.dispose();
}
}
use of org.springframework.data.mongodb.test.util.EnableIfReplicaSetAvailable in project spring-data-mongodb by spring-projects.
the class ReactiveMongoTemplateTests method changeStreamEventsShouldBeConvertedCorrectly.
// 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 changeStreamEventsShouldBeConvertedCorrectly() 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", 39);
Person person3 = new Person("MongoDB", 37);
//
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);
try {
assertThat(documents.stream().map(ChangeStreamEvent::getBody).collect(Collectors.toList())).containsExactly(person1, person2, person3);
} finally {
disposable.dispose();
}
}
use of org.springframework.data.mongodb.test.util.EnableIfReplicaSetAvailable in project spring-data-mongodb by spring-projects.
the class ReactiveMongoTemplateTests method changeStreamEventsShouldBeFilteredCorrectly.
// 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 changeStreamEventsShouldBeFilteredCorrectly() 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.builder().filter(newAggregation(Person.class, match(where("age").gte(38)))).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);
Person person3 = new Person("MongoDB", 39);
//
Flux.merge(template.save(person1), template.save(person2), template.save(person3)).as(//
StepVerifier::create).expectNextCount(//
3).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(person1, person3);
} finally {
disposable.dispose();
}
}
use of org.springframework.data.mongodb.test.util.EnableIfReplicaSetAvailable in project spring-data-mongodb by spring-projects.
the class ReactiveMongoTemplateTests method resumesAtTimestampCorrectly.
// DATAMONGO-2012, DATAMONGO-2113
@Test
@EnableIfMongoServerVersion(isGreaterThanEqual = "4.0")
@EnableIfReplicaSetAvailable
void resumesAtTimestampCorrectly() 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.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();
// just give it some time to link receive all events
Thread.sleep(500);
disposable.dispose();
// skip first
documents.take();
// take 2nd
Instant resumeAt = documents.take().getTimestamp();
template.save(person3).as(StepVerifier::create).expectNextCount(1).verifyComplete();
BlockingQueue<ChangeStreamEvent<Person>> resumeDocuments = new LinkedBlockingQueue<>(100);
template.changeStream("person", ChangeStreamOptions.builder().resumeAt(resumeAt).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();
}
}
Aggregations