简体   繁体   English

嘲笑“睡觉”

[英]Mocking “sleep”

I am writing an application that would asynchronously trigger some events. 我正在编写一个将异步触发某些事件的应用程序。 The test looks like this: set everything up, sleep for sometime, check that event has triggered. 测试如下所示:设置所有内容,休眠一段时间,检查事件是否已触发。

However because of that waiting the test takes quite a time to run - I'm waiting for about 10 seconds on every test. 但是由于这样的等待,测试需要相当长的时间才能运行-我在每个测试上都等待大约10秒钟。 I feel that my tests are slow - there are other places where I can speed them up, but this sleeping seems the most obvious place to speed it up. 我觉得我的测试很慢-在其他地方我可以加快它们的速度,但是这种睡觉似乎是最明显的加快速度的地方。

What would be the correct way to eliminate that sleep? 消除睡眠的正确方法是什么? Is there some way to cheat datetime or something like that? 有什么办法可以欺骗日期时间或类似的东西吗?

The application is a tornado-based web app and async events are triggered with IOLoop, so I don't have a way to directly trigger it myself. 该应用程序是基于龙卷风的Web应用程序,异步事件是使用IOLoop触发的,所以我没有办法直接自己触发它。

Edit: more details. 编辑:更多详细信息。

The test is a kind of integration test, where I am willing to mock the 3rd party code, but don't want to directly trigger my own code. 该测试是一种集成测试,在该测试中,我愿意模拟第3方代码,但不想直接触发我自己的代码。

The test is to verify that a certain message is sent using websocket and is processed correctly in the browser. 该测试是为了验证是否使用websocket发送了某些消息,并在浏览器中正确处理了该消息。 Message is sent after a certain timeout which is started at the moment the client connects to the websocket handler. 在客户端连接到Websocket处理程序时开始的特定超时之后,将发送消息。 The timeout value is taken as a difference between datetime.now() at the moment of connection and a value in database. 超时值被视为连接时datetime.now()与数据库中的值之间的差。 The value is artificially set to be datetime.now() - 5 seconds before using selenium to request the page. 该值被人为设置为datetime.now() - 5 seconds在使用硒请求页面之前的datetime.now() - 5 seconds Since loading the page requires some time and could be a bit random on different machines I don't think reducing the 5 seconds time gap would be wise. 由于加载页面需要一些时间,并且在不同的机器上可能有点随机,所以我认为减小5秒的时间间隔不是明智的。 Loading the page after timeout will produce a different result (no websocket message should be sent). 超时后加载页面将产生不同的结果(不应发送任何websocket消息)。

So the problem is to somehow force tornado's IOLoop to send the message at any moment after the websocket is connected - if that happened in 0.5 seconds after setting the database value, 4.5 seconds left to wait and I want to try and eliminate that delay. 因此,问题在于以某种方式迫使龙卷风的IOLoop在连接websocket之后的任何时候发送消息-如果在设置数据库值后0.5秒内发生了这种情况,还需要等待4.5秒,我想尝试消除这种延迟。

Two obvious places to mock are IOLoop itself and datetime.now(). 两个明显的模拟地方是IOLoop本身和datetime.now()。 the question is now which one I should monkey-patch and how. 现在的问题是,我应该选择哪一种以及如何修补。

I you want to mock sleep then you must not use it directly in your application's code. 我想模拟sleep那么您一定不能在应用程序的代码中直接使用它。 I would create a class method like System.sleep() and use this in your application. 我将创建一个类似System.sleep()的类方法,并在您的应用程序中使用它。 System.sleep() can be mocked then. 然后可以System.sleep()

Use the built in tornado testing tools . 使用内置的龙卷风测试工具 Each test gets it's own IOLoop, and you use self.stop and self.wait to get results from it, eg (from the docs): 每个测试都有自己的IOLoop,您可以使用self.stopself.wait从中获取结果,例如(从文档中):

    client = AsyncHTTPClient(self.io_loop)
    # call self.stop on fetch completion
    client.fetch("http://www.tornadoweb.org/", self.stop)
    response = self.wait()

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

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