使用 rhino 模拟测试后台工作者
假设我在一个类中有一个后台工作者,在后台线程中执行数据库查询。
我想测试这个类,
所以我模拟我的数据库并返回一些集合,到目前为止一切都很好, 确保我的后台工作人员调用了 do work 我想确保结局也发生了。
我注意到测试随机通过和失败(我认为这与线程有关)
任何建议
lets say i have a background worker in a class that perform db query in a background thread.
i wanna test this class
so i mock my db and return some collection so far so good,
make sure my background worker called the do work
and than i wanna make sure that the finish also happened.
I've noticed that the test pass and fail randomly (i think it has something to do with threads)
any suggestions
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能在后台线程和断言/验证之间存在竞争条件。
例如:
线程不一定在断言之前结束,有时会,有时不会。这种情况的解决方案是加入后台线程:
检查是否存在竞争条件的一种方法是延迟测试线程(在断言之前长时间调用 Thread.Sleep),如果测试停止失败,则这是一个很好的指示竞赛条件。
You may have a race condition between the background thread and the asserts/verifies.
For example:
The thread doesn't necessarily end before the asserts, sometimes it will and sometimes it won't. A solution to this case is to join the background thread:
One way to check if it's a race condition is to delay the test thread (call Thread.Sleep for long time just before the assert) and if the test stops failing that's good indication for race condition.