我正在编写一个简单的集成测试,其中包含以下内容:
I know #2 works and I have no problem with this.
但是,#1仅适用于前几个浏览器,然后挂起,导致后续浏览器测试失败。我真的不明白为什么。
在我的代码中,我正在做的事情:
class Test:
def setup_class(cls):
ready_signal = threading.Event()
cls.port = 9000
cls.docroot = /tmp
cls.http_wrapper = SimpleHTTPWrap(port=cls.port, docroot=cls.docroot, ready_signal)
background_thread = threading.Thread(target=http_wrapper.start)
background_thread.daemon = True
background_thread.start()
ready_signal.wait(10) # Wait for thread to be ready (or proceed after timeout)
def test(self):
ip = get_my_ip_address()
self.browser = webdriver.Remote(_some_parameters_go_here)
self.browser.get('http://' + ip + ":" + cls.port + cls.docroot + '/test.html')
# This hangs after similar tests have already run and succeeded
def teardown_class(cls):
cls.http_wrapper.stop()
class SimpleHTTPWrap(object):
def __init__(self, port, docroot, ready_signal):
self.port = port
self.docroot = docroot
self.request_handler = SimpleHTTPServer.SimpleHTTPRequestHandler
self.server = None
self.ready_signal = ready_signal
def start(self):
os.chdir(self.docroot)
try:
log.info("Attempting to bind to port: " + str(self.port))
self.server = SocketServer.TCPServer(("", self.port), self.request_handler)
self.server.allow_reuse_address = True
log.info("Success.")
self.ready_signal.set() # Signal the main thread that we're ready
self.server.serve_forever()
except socket.error:
log.error("Could not bind to address (port already in use)" + str(self.port))
我认为它处于某种死锁状态,因为当我尝试调用cls.http_wrapper.server.shutdown()时它只是挂起。
然而,我不明白为什么。在后台只运行一个线程,其他测试似乎没问题。但是经过几次,一切都会挂起 从浏览器的角度来看,它尝试加载页面一段时间,直到最后由于连接被拒绝而超时。
有人会碰巧知道可能导致这种情况的原因或我可以做些什么来进一步调查?在这一点上,我不知所措,并考虑尝试别的东西。