我有ThreadedSimpleServer。现在我想用unittest测试它。在test.py文件中,我有类Test(unittest.TestCase)。
if __name__ == '__main__':
# create service
service = Service()
# run service in a separate thread
thread = Thread(target=service.start, name='ServiceThread')
thread.start()
# start testing
unittest.main()
当我运行代码时,其中一个测试发生了管道错误:
File "/usr/lib/python2.7/SocketServer.py", line 323, in finish_request
self.RequestHandlerClass(request, client_address, self)
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
如果我从一个地方运行服务器,并从另一个地方执行unittest,则没有错误。在整个时间内双方都打开连接。问题在哪里?