Commit 4a7154c6 authored by Likun Zhang's avatar Likun Zhang
Browse files

code cleanup and fix the error in the test case.

git-svn-id: svn://bind10.isc.org/svn/bind10/branches/trac352@3346 e5f2f494-b856-4b98-b285-d166d9295462
parent 4eefe424
...@@ -54,7 +54,7 @@ class ServeMixIn: ...@@ -54,7 +54,7 @@ class ServeMixIn:
''' '''
def __init__(self): def __init__(self):
self.__read_sock, self.__write_sock = socket.socketpair() self.__read_sock, self.__write_sock = socket.socketpair()
self.__is_shut_down = threading.Event() self._is_shut_down = threading.Event()
def serve_forever(self, poll_interval=None): def serve_forever(self, poll_interval=None):
''' Overrides the serve_forever([poll_interval]) in class ''' Overrides the serve_forever([poll_interval]) in class
...@@ -73,7 +73,6 @@ class ServeMixIn: ...@@ -73,7 +73,6 @@ class ServeMixIn:
if err.args[0] == EINTR: if err.args[0] == EINTR:
continue continue
else: else:
sys.stderr.write("Error with select(), %s\n", err)
break break
if self.__read_sock in r: if self.__read_sock in r:
...@@ -81,7 +80,7 @@ class ServeMixIn: ...@@ -81,7 +80,7 @@ class ServeMixIn:
else: else:
self._handle_request_noblock() self._handle_request_noblock()
self.__is_shut_down.set() self._is_shut_down.set()
def shutdown(self): def shutdown(self):
'''Stops the serve_forever loop. '''Stops the serve_forever loop.
...@@ -90,4 +89,4 @@ class ServeMixIn: ...@@ -90,4 +89,4 @@ class ServeMixIn:
in another thread when serve_forever is running, or it will block. in another thread when serve_forever is running, or it will block.
''' '''
self.__write_sock.send(SOCK_DATA) # make self.__read_sock readable. self.__write_sock.send(SOCK_DATA) # make self.__read_sock readable.
self.__is_shut_down.wait() # wait until the serve thread terminate self._is_shut_down.wait() # wait until the serve thread terminate
...@@ -47,18 +47,15 @@ class TestServeMixIn(unittest.TestCase): ...@@ -47,18 +47,15 @@ class TestServeMixIn(unittest.TestCase):
server = MyServer(('127.0.0.1', 0), MyHandler) server = MyServer(('127.0.0.1', 0), MyHandler)
ip, port = server.server_address ip, port = server.server_address
server_thread = threading.Thread(target=server.serve_forever) server_thread = threading.Thread(target=server.serve_forever)
server_thread.setDaemon(True)
server_thread.start() server_thread.start()
msg = b'senddata' msg = b'senddata'
self.assertEqual(msg, send_and_get_reply(ip, port, msg)) self.assertEqual(msg, send_and_get_reply(ip, port, msg))
self.assertTrue(server_thread.is_alive()) self.assertTrue(server_thread.is_alive())
# Now shutdown the server self.assertFalse(server._is_shut_down.is_set())
server.shutdown() server.shutdown() # Now shutdown the server
# Sleep a while, make sure the thread has finished. self.assertTrue(server._is_shut_down.is_set())
time.sleep(0.1)
self.assertFalse(server_thread.is_alive())
if __name__== "__main__": if __name__== "__main__":
unittest.main() unittest.main()
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment