sách gpt4 ăn đã đi

python - 带有 phantomjs 间歇性挂起/超时的 Django LiveServerTestCase

In lại 作者:太空狗 更新时间:2023-10-30 00:14:39 25 4
mua khóa gpt4 giày nike

我正在使用 Django (1.5.5)、selenium (2.41.0)、splinter (0.6.0) 和 phantomjs (1.9.7) 来运行实时测试。

虽然测试大部分都有效,但时不时地(在 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 之后,这是我得到的堆栈跟踪:

Theo dõi (cuộc gọi gần đây nhất là cuộc gọi cuối cùng):
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
Theo dõi (cuộc gọi gần đây nhất là cuộc gọi cuối cùng):
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 启动/停止放在类设置/拆卸中,我在这里做了,但它没有解决问题。

有没有人遇到过类似的问题,如果遇到过,解决方法是什么?

câu trả lời hay nhất

为了确定问题并使测试更快失败,您可能需要在 setUpClass/setUp 中配置套接字超时:

import socket

...
socket.setdefaulttimeout(10)

关于python - 带有 phantomjs 间歇性挂起/超时的 Django LiveServerTestCase,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23008256/

25 4 0
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress