2012-04-01 46 views
2

使用Tornado AsyncHTTPTestCase運行測試時,我得到的堆棧跟蹤與測試無關。測試正在通過,所以這可能發生在測試清理?運行Tornado時獲取錯誤的文件描述符AsyncHTTPTestCase

我使用Python 2.7.2,Tornado 2.2。

測試代碼是:

class AllServersHandlerTest(AsyncHTTPTestCase): 

    def get_app(self): 
     return Application([('/rest/test/', AllServersHandler)]) 

    def test_server_status_with_advertiser(self): 
     on_new_host(None, '127.0.0.1') 
     response = self.fetch('/rest/test/', method='GET') 
     result = json.loads(response.body, 'utf8').get('data') 
     self.assertEquals(['127.0.0.1'], result) 

測試通過確定,但我從龍捲風服務器以下堆棧跟蹤。

OSError: [Errno 9] Bad file descriptor 
INFO:root:200 POST /rest/serverStatuses (127.0.0.1) 0.00ms 
DEBUG:root:error closing fd 688 
Traceback (most recent call last): 
    File "C:\Python27\Lib\site-packages\tornado-2.2-py2.7.egg\tornado\ioloop.py", line 173, in close 
    os.close(fd) 
OSError: [Errno 9] Bad file descriptor 

任何想法如何幹淨地關閉測試用例?

回答

2

我在龍捲風代碼挖四周,發現此代碼設置()的all_fds爲True,然後導致堆棧跟蹤io_loop.close:

def tearDown(self): 
    if (not IOLoop.initialized() or 
     self.io_loop is not IOLoop.instance()): 
     # Try to clean up any file descriptors left open in the ioloop. 
     # This avoids leaks, especially when tests are run repeatedly 
     # in the same process with autoreload (because curl does not 
     # set FD_CLOEXEC on its file descriptors) 
     self.io_loop.close(all_fds=True) 
    super(AsyncTestCase, self).tearDown() 

因此,在重寫拆解()測試類會停止堆棧跟蹤。

class AllServersHandlerTest(AsyncHTTPTestCase): 

    def tearDown(self): 
     pass 

    def get_app(self): 
     return Application([('/rest/test/', AllServersHandler)]) 

    def test_server_status_with_advertiser(self): 
     on_new_host(None, '127.0.0.1') 
     response = self.fetch('/rest/test/', method='GET') 
     result = json.loads(response.body, 'utf8').get('data') 
     self.assertEquals(['127.0.0.1'], result) 

我不確定這種方法可能引入什麼樣的損害,所以如果別人有更好的建議,讓我知道!

相關問題