Possible sequences and test cases

What is the best practice for writing test cases when working with a stable data warehouse such as MongoDB?

My current setup is Mongodb with a setting of 3 / w630> Master / Slave / Slave with slave-ok set to true. This means that the Node wizard is used for writing only, and the two Node slaves are used for reading only.

The time required to reconcile data on the slaves is relatively small and depends on the size of the operation and the data. For example, ~ 3 ms for delete operations and ~ 200 ms for batch insertion of 1000 objects.

My goal is to check the actions on my Dao. They can be simple, such as getById, delete, insert, or complex, such as findByExample. I need to make sure that they work correctly, with a possible sequence, when the allowable time limit expires.

This is what I have to check the delete operation, for example:

@Test public void deleteTest() throws InstantiationException, IllegalAccessException { MyObject obj = new MyObject(); obj.setName("test object"); obj.save(obj); MyObject found = dao.findById(obj.getId()); logger.info ("before: " + found); Assert.assertEquals(obj, found); dao.delete(obj.getId()); MyObject deleted = null; long start = System.nanoTime(); do { //TBD: need to add escape condition/timeout, else may be infinite loop.... deleted = dao.findById(obj.getId()); logger.info ("While: " + deleted); } while (deleted!=null); logger.info("It took " + ((System.nanoTime()-start)/1000000.00) + " ms for delete to be consistent"); Assert.assertEquals(null, d1); } 
+4
source share
2 answers

A couple of thoughts come to mind

  • In production, if you are ready from a subordinate, you will never know if you are getting the latest data. This is a compromise of readable slave in MongoDB. My experience is that under normal working conditions the slave is updated. If you need to get the latest data, request a wizard.
  • I would definitely start using mms to track replica lag. This will show you how far behind your slaves so you can feel how fast the data will be available.
  • As for the initial test question, it depends on your goals. Your DAO should be able to read and write the same thing, whether it's a replica or a standalone one. You just need to make sure that your application understands that the requested data may not be the latest data.
+1
source

For what you do, you can rely on the fact that with a set of replicas, the mongo will always write to the master. Therefore, I would change the removal test to something like this:

 /* * get this from the DAO, * or use the instance injected into the DAO, etc. */ DBCollection collection = something(); DBCursor itemsRemaining = collection.find(); //find everything itemsRemaining.setReadPreference(ReadPreference.PRIMARY); //force query to the master Assert.assertEquals(0, itemsRemaining.count()); 

Running a test through DBCollection directly allows you to force a test request to use the wizard. I would check that findById (anyOldId) will return null when the item is not in the collection in a separate test.

+1
source

Source: https://habr.com/ru/post/1382435/


All Articles