2014-04-11 43 views
6

我正在使用Django(1.5.5),硒(2.41.0),分裂(0.6.0)和phantomjs(1.9.7)運行實時測試。Django LiveServerTestCase與phantomjs間歇性掛起/超時

儘管測試大多數都是正常工作,但偶爾(經常在CircleCI上,在本地VM中不太常見),它們會掛起,直到CircleCI出現超時或手動殺死跑步者(Ctrl-C即KeyboardInterrupt工作)。

這是我的基礎測試類的樣子:

class SplinterTestCase(LiveServerTestCase): 
    @classmethod 
    def setUpClass(cls): 
     super(SplinterTestCase, cls).setUpClass() 
     # start phantom just once per class, to speed up tests 
     cls.phantom = splinter.Browser('phantomjs', load_images=False) 

    @classmethod 
    def tearDownClass(cls): 
     cls.phantom.quit() 
     super(SplinterTestCase, cls).tearDownClass() 

    def login(self, *args, **kwargs): 
     # perform a login using Django builtin "client", steal the session 
     # cookie and inject it to phantomjs, avoiding the need to do the 
     # login dance for each test 
     from django.conf import settings 
     cn = settings.SESSION_COOKIE_NAME 

     self.django_client.login(*args, **kwargs) 
     if cn in self.django_client.cookies: 
      self.client.driver.add_cookie({ 
       'name': cn, 
       'value': self.django_client.cookies[cn].value, 
       'path': '/', 
       'domain': 'localhost' 
      }) 

    def setUp(self): 
     # use phantom as the test client instead of Django's 
     super(SplinterTestCase, self).setUp() 
     self.django_client = self.client 
     self.client = self.phantom 

    def tearDown(self): 
     # this seems to help somewhat (decreases the number of timeouts), but 
     # doesn't solve it completely 
     self.client.visit('about:config') 
     super(SplinterTestCase, self).tearDown() 

按Ctrl-C之後,這是堆棧跟蹤我得到:

Traceback (most recent call last): 
    File "/usr/lib/python2.7/wsgiref/handlers.py", line 86, in run 
    self.finish_response() 
    File "/usr/lib/python2.7/wsgiref/handlers.py", line 127, in finish_response 
    self.write(data) 
    File "/usr/lib/python2.7/wsgiref/handlers.py", line 215, in write 
    self._write(data) 
    File "/usr/lib/python2.7/socket.py", line 324, in write 
    self.flush() 
    File "/usr/lib/python2.7/socket.py", line 303, in flush 
    self._sock.sendall(view[write_offset:write_offset+buffer_size]) 
error: [Errno 104] Connection reset by peer 
Traceback (most recent call last): 
    File "/home/ubuntu/memo-angel/venv/local/lib/python2.7/site-packages/django/test/testcases.py", line 998, in _handle_request_noblock 
    self.process_request(request, client_address) 
    File "/usr/lib/python2.7/SocketServer.py", line 310, in process_request 
    self.finish_request(request, client_address) 
    File "/usr/lib/python2.7/SocketServer.py", line 323, in finish_request 
    self.RequestHandlerClass(request, client_address, self) 
    File "/home/ubuntu/memo-angel/venv/local/lib/python2.7/site-packages/django/core/servers/basehttp.py", line 150, in __init__ 
    super(WSGIRequestHandler, self).__init__(*args, **kwargs) 
    File "/usr/lib/python2.7/SocketServer.py", line 640, in __init__ 
    self.finish() 
    File "/usr/lib/python2.7/SocketServer.py", line 693, in finish 
    self.wfile.flush() 
    File "/usr/lib/python2.7/socket.py", line 303, in flush 
    self._sock.sendall(view[write_offset:write_offset+buffer_size]) 
error: [Errno 32] Broken pipe 

類似的問題可能已經在Django with splinter and phantomjs is painfully slow所討論原來的海報還提到「它只是凍結,直到我沒有耐心等待它完成」。那裏提到的答案是試圖把phantomjs開始/停止放在課堂設置/拆解中,我在這裏做了,但它不能解決問題。

有沒有人遇到類似的問題,如果您有,您的解決方法是什麼?

+0

我有所有這些組件的最新版本,類似的問題(如2017年1月)。我最好的解決方法是MechanTOurS答案和我對它的評論。 –

回答

2

要識別問題,使您的測試失敗更快,您可能需要配置您的setUpClass /設置內部套接字超時:

import socket 

... 
socket.setdefaulttimeout(10) 
+0

我設置了默認的套接字超時時間,並且在'try:... socket.timeout:pass'之外單擊了我的掛起按鈕,我的測試通過。點擊似乎正在發生,並且掛起僅在結果的非必要通信中通過堆棧發生。 –