2

Using scalatest and Casbah, I created a test to load a bunch of documents into Mongo, and then assert that collection.count() > 0.

val collection = MongoConnection()(MY_DB)(MY_COLLECTION) 
collection.dropCollection // clear out any docs from previous test run

insert200DocumentsIntoMongo() // inserts 200 docs into the same DB and collection

assert(collection.size > 0) 

For multiple tests, scalatest throws an exception that the assert is not true.

However, after the test fails, I can clearly see in the Mongo shell that 200 documents were added to the Mongo database's collection as per the above "MY_DB" and "MY_COLLECTION."

>db.test.count()
200

I'm confused as to why this assert is failing since the Mongo shell demonstrates that there are 200 documents in the collection.

Also, I've tried to drop the entire database using this post, but still the assert fails.

2 Answers 2

1

Try to change mongo write concern:

collection.setWriteConcern(WriteConcern.FsyncSafe)
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, Sergey. So, my testing and production Write Concerns (WC) may differ. As a result, should I use a configuration value to specify which WC to use? What's the usual pattern?
I think it depends on your needs. Usually it's not good to block process and wait for db operation in production. I recommend to start from docs.mongodb.org/manual/core/write-concern and experiment with you specific cases (it's general advice for everything connected with mongodb).
@Kevin, I would use the same WC as in production and add a getLastError in your test before the assert.
Actually, after further testing, I question this answer. Due to code re-structuring, I set my WriteConcern inside of the Object service performing the write. Based on configuration, I set the WriteConcern. When using FsyncSafe, my tests still fail when checking the collection's size. I think this SO post shows that fsync will not necessarily write to disk - stackoverflow.com/questions/12262454/…. Actually, maybe it writes within 60 seconds - docs.mongodb.org/manual/reference/command/fsync
1

There are a few options:

One to change mongo write concern, as well pointed out by Sergey.

Mongo writes assynchronously by default, this means that when you fire your insert, it will not wait for the data to be inserted and move on. Changing the write concern will make your test work, but may mask problems if you are not using this option in production environment, depending on what you are testing.

The other option would be to put a wait before doing your assert, which could be trickier.

And last you could use getLastError, which will block your execution until the last command is executed.

Read more here

2 Comments

Thanks, Vicinius. I used your recommended approach - use async WC in Production code, but getLastError in test.
I think that is the way to go. I glad I could help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.