/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_smart_transport.py

  • Committer: Robert Collins
  • Date: 2007-04-05 00:39:03 UTC
  • mto: This revision was merged to the branch mainline in revision 2401.
  • Revision ID: robertc@robertcollins.net-20070405003903-u1ys8t2lo5gs6b35
Overhaul the SmartTCPServer connect-thread logic to synchronise on startup and shutdown and notify the server if it is in accept.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006 Canonical Ltd
 
1
# Copyright (C) 2006, 2007 Canonical Ltd
2
2
#
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
822
822
 
823
823
class TestServerSocketUsage(SmartTCPTests):
824
824
 
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."""
 
827
        self.setUpServer()
 
828
        server = self.server
 
829
        transport = smart.SmartTCPTransport(self.server.get_url())
 
830
        self.tearDownServer()
 
831
        self.assertRaises(errors.ConnectionError, transport.has, '.')
 
832
 
 
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
832
 
        # method.
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
839
 
        transport.has('.')
840
 
        # and close it.
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.
844
 
        del transport
845
 
        while server._server_thread.isAlive():
846
 
            # this is fugly: we should have an event for the server we can
847
 
            # wait for.
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])
977
 
        transport.has('.')
978
 
        transport.disconnect()
979
 
        del transport
980
 
        while server._server_thread.isAlive():
981
 
            # this is fugly: we should have an event for the server we can
982
 
            # wait for.
983
 
            import time; time.sleep(0.001)
 
964
        # now it should have fired.
984
965
        self.assertEqual(result, self.hook_calls)
985
966
 
986
967
# TODO: test that when the server suffers an exception that it calls the