windows - Why does my Python unittest test case with threads hang? -
i have following test case. note following test case not trying test trying demonstrate hanging issue encountering.
import http.server import urllib.request import threading import unittest class footest(unittest.testcase): def setup(self): print('---- setup start') self.httpd = http.server.httpserver(('', 8080), http.server.simplehttprequesthandler) thread = threading.thread(target=self.httpd.serve_forever) thread.start() print('---- setup complete') def teardown(self): print('---- teardown start') self.httpd.shutdown() print('---- teardown complete') def test1(self): print('---- test1 start') print(threading.current_thread()) urllib.request.urlopen('http://127.0.0.1:8080/foo') print('---- test1 complete') def test2(self): print('---- test2 start') print(threading.current_thread()) urllib.request.urlopen('http://127.0.0.1:8080/foo') print('---- test2 complete') if __name__ == '__main__': unittest.main()
i expect 2 errors when try execute test case. instead, test case hangs after following output.
c:\lab>python foo.py -v test1 (__main__.footest) ... ---- setup start ---- setup complete ---- test1 start <_mainthread(mainthread, started 12980)> 127.0.0.1 - - [24/mar/2014 21:53:57] code 404, message file not found 127.0.0.1 - - [24/mar/2014 21:53:57] "get /foo http/1.1" 404 - ---- teardown start ---- teardown complete error test2 (__main__.footest) ... ---- setup start ---- setup complete ---- test2 start <_mainthread(mainthread, started 12980)>
if remove test2
code, expect 1 error , sure enough see it.
c:\lab>python foo.py -v test1 (__main__.footest) ... ---- setup start ---- setup complete ---- test1 start <_mainthread(mainthread, started 15720)> 127.0.0.1 - - [24/mar/2014 21:55:12] code 404, message file not found 127.0.0.1 - - [24/mar/2014 21:55:12] "get /foo http/1.1" 404 - ---- teardown start ---- teardown complete error ====================================================================== error: test1 (__main__.footest) ---------------------------------------------------------------------- traceback (most recent call last): file "foo.py", line 22, in test1 urllib.request.urlopen('http://127.0.0.1:8080/foo') file "c:\python34\lib\urllib\request.py", line 153, in urlopen return opener.open(url, data, timeout) file "c:\python34\lib\urllib\request.py", line 461, in open response = meth(req, response) file "c:\python34\lib\urllib\request.py", line 574, in http_response 'http', request, response, code, msg, hdrs) file "c:\python34\lib\urllib\request.py", line 499, in error return self._call_chain(*args) file "c:\python34\lib\urllib\request.py", line 433, in _call_chain result = func(*args) file "c:\python34\lib\urllib\request.py", line 582, in http_error_default raise httperror(req.full_url, code, msg, hdrs, fp) urllib.error.httperror: http error 404: file not found ---------------------------------------------------------------------- ran 1 test in 0.032s failed (errors=1)
why test case hang if there 2 tests errors?
add call server_close
close socket:
def setup(self): print('---- setup start') handler = http.server.simplehttprequesthandler self.httpd = http.server.httpserver(('', 8080), handler) threading.thread(target=self.serve).start() print('---- setup complete') def serve(self): try: self.httpd.serve_forever() finally: self.httpd.server_close()
Comments
Post a Comment