English 中文(简体)
最终一致性和测试案例
原标题:Eventual Consistency and Test Cases

在与MongoDB等最终一致的数据库合作时,撰写测试案例的最佳做法是什么?

我的现任职务是Mongodb,由3-node Master/Slave/Slave组成,由奴隶制度确定。 这意味着,诺德硕士只用于书写,而两个奴隶节只用于阅读。

有关奴隶的数据必须一致,时间相对较少,取决于操作和数据规模。 例如,删除作业的面积约为3米,批量为1 000件。

我的目标是考验我的代索的行动。 它们可能很简单,例如获得ById,删除,插入,或像发现ByExample。 我需要核实他们的工作是正确的,最终在一定时间范围内可以接受。

这是我目前对删除行动进行测试的情况,例如:

  @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);
  } 
最佳回答

想到的有两点。

  1. In production, if you are ready from the slave, you ll never know if you are getting the most current data. That s the tradeoff of a read slave in MongoDB. My experience has been that under normal working conditions the slave is up to date. If you have to get the most current data, query the master.
  2. I would definitely start using mms to track your replica lag. This will tell you how far behind your slaves are so that you can get a feel for how quickly the data will be available
  3. As for the original testing question, it depends on your goals. Your DAO should be able to read and write the same whether it is a replica or standalone. You just need to make sure that your application understands that the data it queries might not be the most current data.
问题回答

如果你做些什么,你可以依赖这样的事实,即用一套复制机,万戈将永远写给主。 因此,我将把删除试验改为这样的东西:

/*
 * 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());

通过亚洲开发银行的“协作”进行测试,直接使你能够强迫测试点使用主人。 我要测试的是,如果项目在单独试验中排在收集中,那么发现ById(任何OldId)将无效。





相关问题
run unit tests and coverage in certain python structure

I have some funny noob problem. I try to run unit tests from commandline: H:PROpyEstimator>python src estpython est_power_estimator.py Traceback (most recent call last): File "src est...

How to unit-test an enterprise symfony project?

I´m working on a huge project at my work. We have about 200 database tables, accordingly a huge number of Models, Actions and so on. How should I begin to write tests for this? My biggest problem ...

Code Coverage Tools & Visual Studio 2008 Pro

Just wondering what people are using for code coverage tools when using MS Visual Studio 2008 Pro. We are using the built-in MS test project and unit testing tool (the one that come pre-installed ...

Unit testing. File structure

I have a C++ legacy codebase with 10-15 applications, all sharing several components. While setting up unittests for both shared components and for applications themselves, I was wondering if there ...

Unit Testing .NET 3.5 projects using MStest in VS2010

There s a bug/feature in Visual Studio 2010 where you can t create a unit test project with the 2.0 CLR. https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=483891&wa=...

Unit Test for Exceptions Message

Is there a simple (Attribute-driven) way to have the following test fail on the message of the exception. [TestMethod()] [ExpectedException(typeof(ArgumentException))] public void ExceptionTestTest() ...

热门标签