flapdoodle-oss / de.flapdoodle.embed.mongo

...will provide a platform neutral way for running mongodb in unittests.
Apache License 2.0
910 stars 160 forks source link

Is there a recommended solution on how to enable mongoDB "change streams" ? #497

Closed michaelmosmann closed 12 months ago

michaelmosmann commented 1 year ago

Discussed in https://github.com/flapdoodle-oss/de.flapdoodle.embed.mongo/discussions/496

Originally posted by **bodote** November 15, 2023 MongoDB has a feature called ["change streams"](https://www.mongodb.com/docs/v7.0/changeStreams). However, this feature is only available if there is a "replica set". The replica set in turn is NOT enabled by default for single instance MongoDBs. You can enable the replica set with de.flapdoodle.embed.mongo. But it seemes a little bit cumbersome though. Is there any recommendation on how to use this with de.flapdoodle.embed.mongo ? what we do is this: ``` @Override public Transition mongodArguments() { return Start.to(MongodArguments.class) .initializedWith(MongodArguments.defaults() .withReplication(Storage.of("testRepSet", 5000)) .withUseNoJournal(false)); } ``` plus : ``` mongoClient.getDatabase("admin").runCommand(new Document("replSetInitiate", new Document())); ``` immediately `@BeforeEach` test. especially the need for this admin - command above turned out to be somehow problematic and lead to unstable test until we wraped it part of the test code in some `await().atMost(10, SECONDS).until(() -> {...}` Is there any better way ?
XSpielinbox commented 1 year ago

Setting de.flapdoodle.mongodb.embedded.storage.repl-set-name=rs0 in the application.properties when using de.flapdoodle.embed.mongo.spring enables replicasets in a way that transactions seem to work. Maybe this does the trick here too?

michaelmosmann commented 1 year ago

@XSpielinbox you are right, if you use the spring integration: https://github.com/flapdoodle-oss/de.flapdoodle.embed.mongo.spring/blob/041421695626591599798602a39df8a61e3fdb42/src/main/java/de/flapdoodle/embed/mongo/spring/autoconfigure/AbstractServerFactory.java#L84

maybe it is time for an backport:)

bodote commented 12 months ago

@XSpielinbox you are right, if you use the spring integration: https://github.com/flapdoodle-oss/de.flapdoodle.embed.mongo.spring/blob/041421695626591599798602a39df8a61e3fdb42/src/main/java/de/flapdoodle/embed/mongo/spring/autoconfigure/AbstractServerFactory.java#L84

maybe it is time for an backport:)

If its already in the spring3.1.x branch of https://github.com/flapdoodle-oss/de.flapdoodle.embed.mongo.spring/ , which I use anyhow, it would be nice, if there would be an explationaion in the HowTo.md how to use this autoconfigure/AbstractServerFactory, because it is not clear to me. Is setting the de.flapdoodle.mongodb.embedded.storage.repl-set-name=rs0 really all you need to do ?

michaelmosmann commented 12 months ago

@bodote yes.. the setting is all you need, as everything else will be configured from that (using AbstractServerFactory etc).

bodote commented 12 months ago

de.flapdoodle.mongodb.embedded

I see, thank you. BTW using "de.flapdoodle.mongodb.embedded." and "de.flapdoodle.embed.mongo." for properties is really confusing, and can easily get mixed up which would lead to wrongly setting properties which are not working.

michaelmosmann commented 12 months ago

@bodote hmmm.. i may fix this with the next mayor release.. i think it was 'spring.mongodb.embedded' .. and i changed it to 'de.flapdoodle.mongodb.embedded' ..

michaelmosmann commented 12 months ago

moved it to an new issue.. so i close this.