龙卷风测试" ioloop正在关闭"运行时错误

时间:2015-08-19 09:05:14

标签: tornado

我有这样的考试。问题出在评论中。

@gen_test(timeout=10)
def test_handshake(self):
    print "+++++++++ Test create_stream ++++++++++"
    self.io_loop.current().spawn_callback(self.pool.process_base_channel)
    strcmd = "{'cmd': 'crt_stream', 'p_id':'test_stream','redis_chan':'test_chan'}"
    cmd = ast.literal_eval(strcmd)
    yield self.pool.q.put(cmd)
    yield self.pool.q.join() 
    self.assertIsInstance(self.pool.streams['test_stream'], Stream)

    print "++++++++++ Test subscription ++++++++++++"
    ''' 
       If this blocks run in this function, it OK, but if I move 
       it to separate function, runtime error occurs.
    '''
    subscr = "{'cmd': 'sub2stream', 'stream_id':'test_stream','redis_chan':'test_broadcast', 'cols':'all' }"
    cmd = ast.literal_eval(subscr)
    #self.io_loop.current().spawn_callback(self.pool.process_base_channel)
    yield self.pool.q.put(cmd)

@gen_test(timeout=10)
def test_next(self):
    print "++++++++++ Test subscription ++++++++++++"
    print "Test stream %s" % self.pool.streams['test_stream'].publish_list
    #self.io_loop.current().spawn_callback(self.pool.process_base_channel)
    # tried respawn callbac
    subscr = "{'cmd': 'sub2stream', 'stream_id':'test_stream','redis_chan':'test_broadcast', 'cols':'all' }"
    cmd = ast.literal_eval(subscr)

    yield self.pool.q.put(cmd)

由于某些原因,在test_handshake运行后,self.io_loop将关闭。无法理解原因。

1 个答案:

答案 0 :(得分:1)

使用AsyncTestCase,每个测试都会创建一个新的IOLoop,并在测试完成后关闭它。看起来你有一些东西(另一个线程?一个析构函数?你的tearDown函数中的某些东西?)试图在IOLoop关闭后与IOLoop进行交互,但它无法分辨出这是怎么回事不完整的例子(什么是self.pool?)。

此外,您在此代码中使用self.io_loop.current(),这是多余的。 current()是一个类方法,而不是实例方法,因此通常称为tornado.ioloop.IOLoop.current()。在测试中,IOLoop.current()会返回self.io_loop,因此您只需使用self.io_loop代替self.io_loop.current()