简体   繁体   English

JUnit测试数据库失败?

[英]JUnit Test a Database Failure?

I'm trying to create a test that simulates a system failure to ensure the integrity of a Oracle Berkeley DB XML database. 我正在尝试创建一个模拟系统故障的测试,以确保Oracle Berkeley DB XML数据库的完整性。 Data loss is currently being experienced during an insert operation so I'd like to setup a test that starts inserting an arbitrary number of documents and sack the process along the way (akin to someone yanking the power cord). 在插入操作期间当前正在经历数据丢失,因此我想设置一个测试,开始插入任意数量的文档并在此过程中解决该过程(类似于某人扯断电源线)。 After the process dies I want to spawn a new process and open the database to ensure it opens properly. 在进程死后,我想生成一个新进程并打开数据库以确保它正常打开。

The unit test is one of many in a maven build and this test has to run in linux and windows XP environments. 单元测试是maven构建中的众多测试之一,此测试必须在linux和Windows XP环境中运行。 My current thought process is to hammer out a script for both operating systems since I can use the script to kill the process and start a new one in its place. 我目前的思考过程是为两个操作系统敲定一个脚本,因为我可以使用该脚本来终止进程并在其位置启动一个新进程。 Do I have any other options? 我还有其他选择吗? Can I create a separate process space / VM using JUnit? 我可以使用JUnit创建单独的进程空间/ VM吗?

I wouldn't consider this kind of test a unit test but nevertheless you may be able to do something like this. 我不认为这种测试是单元测试,但你可以做这样的事情。

  1. Use the ProcessBuilder class to construct and start the process, store the returned Process object. 使用ProcessBuilder类构造并启动进程,存储返回的Process对象。
  2. Start inserting records. 开始插入记录。
  3. At some point destroy() the process. 在某些时候destroy()过程。

Please bear in mind previous comments on the non-deterministic nature of this test. 请记住以前关于该测试的非确定性的评论。

I have come across the SQLite team also doing a simulated failure strategy as part of their automated test suite. 我遇到了SQLite团队 ,他们也在模拟故障策略中作为自动化测试套件的一部分。

This type of behavior is perfect for transactions. 这种行为非常适合交易。 If your code were to start a transaction, the database would know how to keep the data consistent when a transaction aborted due to a process dying. 如果您的代码要启动事务,那么当事务因流程死亡而中止时,数据库将知道如何保持数据一致。 You might be reinventing the wheel here. 你可能会在这里重新发明轮子。 The Daily WTF has a good example of how we fool ourselves into reinventing the wheel . 每日WTF有一个很好的例子,说明我们如何欺骗自己重新发明轮子

Take a good hard look at your first revision and say to yourself, "gloves." 仔细看看你的第一次修订并对自己说“手套”。

What about using just threads insead of whole process? 使用刚刚整个过程的线程怎么样? For example: 例如:

  • You can create a background worker thread and give it a few loads of work. 您可以创建后台工作线程并为其提供一些工作量。
  • Then in main test's thread wait a random number of milisecond. 然后在主测试的线程中等待一个随机数的毫秒。 Then kill the thread. 然后杀死线程。
  • Then maybe you want to wait again to have cached data saved (or not, depending on what you are testing) 那么也许你想再次等待保存缓存数据(或不,取决于你正在测试的内容)
  • Then run your sanity. 然后运行你的理智。 If it's throwing, the test will fail here as you need! 如果它正在投掷,测试将在这里失败,因为你需要!
  • Here you are done. 在这里你完成了。

I would rather call the above test an integration test. 我宁愿把上面的测试称为集成测试。 Anyway it will do what you needed. 无论如何它会做你需要的。 Running will multiple times will test different data corruption case on each run. 多次运行将在每次运行时测试不同的数据损坏情况。

I would not consider this suitable for a UNIT test. 我不认为这适合UNIT测试。 in unit tests you want to test small sections of code, for example your sanity code, possibly with a mock DB. 在单元测试中,您希望测试一小段代码,例如您的健全代码,可能还有一个模拟数据库。 or by calling only part of your code. 或只调用部分代码。 What you are referring to is a more complicated test that may be worth doing but not necessarily worth automating and running repeatedly as part of your unit tests which you presumably run fairly often(on build or nightly or whenever). 你所指的是一个更复杂的测试,可能值得做,但不一定值得自动化和重复运行作为单元测试的一部分,你可能经常运行(在构建或夜间或任何时候)。 Adding this kind of test will also probably slow down your unit testing and will impose restrictions on the environment your unit testing can run in. I suggest writing smaller unit tests, and testing the whole thing together separately. 添加这种测试也可能会降低单元测试速度,并对单元测试可以运行的环境施加限制。我建议编写较小的单元测试,并分别测试整个测试。

Me. 我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM