2015-08-19 48 views
0

我有這樣的測試。這個問題在評論中。龍捲風測試「ioloop正在關閉」運行時錯誤

@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

隨着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()

+0

在後臺運行的永遠是async-while(consumer)(self.pool.process_base_channel)。池在測試類的setup()中放置。所以,當一個測試關閉ioloop時,那會一直持續下去,並可能導致一些「uhoh」? – user3003873