Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何编写python tornado测试来断言ioloop已关闭?_Python_Tornado_Python Asyncio_Application Shutdown - Fatal编程技术网

如何编写python tornado测试来断言ioloop已关闭?

如何编写python tornado测试来断言ioloop已关闭?,python,tornado,python-asyncio,application-shutdown,Python,Tornado,Python Asyncio,Application Shutdown,我有一些代码将拦截SIGTERM/SIGINT,并指示tornado ioloop(它是异步IO循环的包装器)在关闭tornado ioloop之前等待机上请求完成(见下文) 我不认为这是可以用AsyncHTTPTestCase测试的,因为事件循环的开始和停止对您是隐藏的(如果你不使用@gen\u test,IOLoop大部分时间已经停止。如果你使用@gen\u test,有一个IOLoop总是在运行,但是如果你停止它,你的测试就会挂起)。为了测试IOLoop是否停止,你的代码需要是调用IOLo

我有一些代码将拦截SIGTERM/SIGINT,并指示tornado ioloop(它是异步IO循环的包装器)在关闭tornado ioloop之前等待机上请求完成(见下文)


我不认为这是可以用AsyncHTTPTestCase测试的,因为事件循环的开始和停止对您是隐藏的(如果你不使用
@gen\u test
,IOLoop大部分时间已经停止。如果你使用
@gen\u test
,有一个IOLoop总是在运行,但是如果你停止它,你的测试就会挂起)。为了测试IOLoop是否停止,你的代码需要是调用
IOLoop.start()
的东西

测试这种启动和关闭问题的最全面的方法是将服务器作为子进程运行。通过这种方法,您可以向它发送HTTP请求和信号,并查看进程是否退出

在当前流程中,也有一些方法可以做到这一点,在真实性和性能之间进行各种权衡。例如,如下所示:

def test_graceful_shutdown(self):
    def trigger_signal():
        time.sleep(2)
        os.kill(os.getpid(), signal.SIGINT)
    thread = threading.Thread(target=trigger_signal)
    thread.start()

    io_loop = IOLoop()
    install_signal_handler(io_loop)
    def failure():
        self.fail("did not shut down within 5 seconds")
        io_loop.stop()
    io_loop.call_after(5, failure)
    io_loop.start()

感谢您的反馈Ben。我确实成功地测试了这一点,尽管需要使用unittest.TestCase tearDown函数:
def test_graceful_shutdown(self):
    def trigger_signal():
        time.sleep(2)
        os.kill(os.getpid(), signal.SIGINT)
    thread = threading.Thread(target=trigger_signal)
    thread.start()

    io_loop = IOLoop()
    install_signal_handler(io_loop)
    def failure():
        self.fail("did not shut down within 5 seconds")
        io_loop.stop()
    io_loop.call_after(5, failure)
    io_loop.start()