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.

Community
  • 1
  • 1
Kevin Meredith
  • 41,036
  • 63
  • 209
  • 384

2 Answers2

1

Try to change mongo write concern:

collection.setWriteConcern(WriteConcern.FsyncSafe)
Sergey Passichenko
  • 6,920
  • 1
  • 28
  • 29
  • 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? – Kevin Meredith Oct 01 '13 at 11:52
  • 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 http://docs.mongodb.org/manual/core/write-concern/ and experiment with you specific cases (it's general advice for everything connected with mongodb). – Sergey Passichenko Oct 01 '13 at 12:15
  • 1
    @Kevin, I would use the same WC as in production and add a getLastError in your test before the assert. – Vinicius Miana Oct 01 '13 at 12:46
  • 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 - http://stackoverflow.com/questions/12262454/fsync-sync-does-it-really-do-what-its-supposed-to. Actually, maybe it writes within 60 seconds - http://docs.mongodb.org/manual/reference/command/fsync/ – Kevin Meredith Oct 11 '13 at 21:27
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

Vinicius Miana
  • 2,047
  • 17
  • 27