1
# Copyright (C) 2006 Canonical Ltd
1
# Copyright (C) 2006, 2007 Canonical Ltd
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
823
823
class TestServerSocketUsage(SmartTCPTests):
825
def test_server_closes_listening_sock_on_shutdown(self):
825
def test_server_setup_teardown(self):
826
"""It should be safe to teardown the server with no requests."""
829
transport = smart.SmartTCPTransport(self.server.get_url())
830
self.tearDownServer()
831
self.assertRaises(errors.ConnectionError, transport.has, '.')
833
def test_server_closes_listening_sock_on_shutdown_after_request(self):
826
834
"""The server should close its listening socket when it's stopped."""
827
835
self.setUpServer()
828
# clean up the server and initial transport (which wont have connected):
829
# force a connection, which uses the listening socket to synchronise
830
# with the server thread, so that when we shut it down it has already
831
# executed the 'self._should_terminate = False' line in the server
833
836
server = self.server
834
837
self.transport.has('.')
835
838
self.tearDownServer()
836
# make a new connection to break out the inner loop in the server.
837
transport = smart.SmartTCPTransport(server.get_url())
838
# force the connection
841
transport.disconnect()
842
# this del probably is not needed, but I wanted to be clear about what
843
# we are testing: having objects hanging around is not part of the test.
845
while server._server_thread.isAlive():
846
# this is fugly: we should have an event for the server we can
848
import time; time.sleep(0.001)
849
839
# if the listening socket has closed, we should get a BADFD error
850
840
# when connecting, rather than a hang.
851
841
transport = smart.SmartTCPTransport(server.get_url())
963
953
self.capture_server_call)
964
954
self.setUpServer()
965
955
result = [(self.backing_transport.base, self.transport.base)]
966
# check the stopping message isn't emitted up front, this also
967
# has the effect of synchronising with the server, so that
968
# when we shut it down it has already executed the
969
# 'self._should_terminate = False' line in the server method.
956
# check the stopping message isn't emitted up front.
957
self.assertEqual([], self.hook_calls)
958
# nor after a single message
970
959
self.transport.has('.')
971
960
self.assertEqual([], self.hook_calls)
972
961
# clean up the server
973
962
server = self.server
974
963
self.tearDownServer()
975
# make a new connection to break out the inner loop in the server.
976
transport = smart.SmartTCPTransport(result[0][1])
978
transport.disconnect()
980
while server._server_thread.isAlive():
981
# this is fugly: we should have an event for the server we can
983
import time; time.sleep(0.001)
964
# now it should have fired.
984
965
self.assertEqual(result, self.hook_calls)
986
967
# TODO: test that when the server suffers an exception that it calls the