~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_smart_transport.py

Merge bzr.dev into cleanup

Show diffs side-by-side

added added

removed removed

Lines of Context:
40
40
        server,
41
41
        vfs,
42
42
)
43
 
from bzrlib.tests import test_smart
 
43
from bzrlib.tests import (
 
44
    test_smart,
 
45
    test_server,
 
46
    )
44
47
from bzrlib.transport import (
45
48
        http,
46
49
        local,
972
975
            base = 'a_url'
973
976
            def external_url(self):
974
977
                return self.base
975
 
            def get_bytes(self, path):
 
978
            def get(self, path):
976
979
                raise Exception("some random exception from inside server")
977
 
        smart_server = server.SmartTCPServer(backing_transport=FlakyTransport())
978
 
        smart_server.start_background_thread('-' + self.id())
979
 
        try:
980
 
            transport = remote.RemoteTCPTransport(smart_server.get_url())
981
 
            err = self.assertRaises(errors.UnknownErrorFromSmartServer,
982
 
                transport.get, 'something')
983
 
            self.assertContainsRe(str(err), 'some random exception')
984
 
            transport.disconnect()
985
 
        finally:
986
 
            smart_server.stop_background_thread()
 
980
 
 
981
        class FlakyServer(test_server.SmartTCPServer_for_testing):
 
982
            def get_backing_transport(self, backing_transport_server):
 
983
                return FlakyTransport()
 
984
 
 
985
        smart_server = FlakyServer()
 
986
        smart_server.start_server()
 
987
        self.addCleanup(smart_server.stop_server)
 
988
        t = remote.RemoteTCPTransport(smart_server.get_url())
 
989
        self.addCleanup(t.disconnect)
 
990
        err = self.assertRaises(errors.UnknownErrorFromSmartServer,
 
991
                                t.get, 'something')
 
992
        self.assertContainsRe(str(err), 'some random exception')
987
993
 
988
994
 
989
995
class SmartTCPTests(tests.TestCase):
990
996
    """Tests for connection/end to end behaviour using the TCP server.
991
997
 
992
 
    All of these tests are run with a server running on another thread serving
 
998
    All of these tests are run with a server running in another thread serving
993
999
    a MemoryTransport, and a connection to it already open.
994
1000
 
995
1001
    the server is obtained by calling self.start_server(readonly=False).
1003
1009
        # NB: Tests using this fall into two categories: tests of the server,
1004
1010
        # tests wanting a server. The latter should be updated to use
1005
1011
        # self.vfs_transport_factory etc.
1006
 
        if not backing_transport:
 
1012
        if backing_transport is None:
1007
1013
            mem_server = memory.MemoryServer()
1008
1014
            mem_server.start_server()
1009
1015
            self.addCleanup(mem_server.stop_server)
1017
1023
            self.backing_transport = transport.get_transport(
1018
1024
                "readonly+" + self.backing_transport.abspath('.'))
1019
1025
        self.server = server.SmartTCPServer(self.backing_transport)
 
1026
        self.server.start_server('127.0.0.1', 0)
1020
1027
        self.server.start_background_thread('-' + self.id())
1021
1028
        self.transport = remote.RemoteTCPTransport(self.server.get_url())
1022
 
        self.addCleanup(self.tearDownServer)
 
1029
        self.addCleanup(self.stop_server)
1023
1030
        self.permit_url(self.server.get_url())
1024
1031
 
1025
 
    def tearDownServer(self):
 
1032
    def stop_server(self):
 
1033
        """Disconnect the client and stop the server.
 
1034
 
 
1035
        This must be re-entrant as some tests will call it explicitly in
 
1036
        addition to the normal cleanup.
 
1037
        """
1026
1038
        if getattr(self, 'transport', None):
1027
1039
            self.transport.disconnect()
1028
1040
            del self.transport
1029
1041
        if getattr(self, 'server', None):
1030
1042
            self.server.stop_background_thread()
1031
 
            # XXX: why not .stop_server() -- mbp 20100106
1032
1043
            del self.server
1033
1044
 
1034
1045
 
1035
1046
class TestServerSocketUsage(SmartTCPTests):
1036
1047
 
1037
 
    def test_server_setup_teardown(self):
1038
 
        """It should be safe to teardown the server with no requests."""
 
1048
    def test_server_start_stop(self):
 
1049
        """It should be safe to stop the server with no requests."""
1039
1050
        self.start_server()
1040
 
        server = self.server
1041
 
        transport = remote.RemoteTCPTransport(self.server.get_url())
1042
 
        self.tearDownServer()
1043
 
        self.assertRaises(errors.ConnectionError, transport.has, '.')
 
1051
        t = remote.RemoteTCPTransport(self.server.get_url())
 
1052
        self.stop_server()
 
1053
        self.assertRaises(errors.ConnectionError, t.has, '.')
1044
1054
 
1045
1055
    def test_server_closes_listening_sock_on_shutdown_after_request(self):
1046
1056
        """The server should close its listening socket when it's stopped."""
1047
1057
        self.start_server()
1048
 
        server = self.server
 
1058
        server_url = self.server.get_url()
1049
1059
        self.transport.has('.')
1050
 
        self.tearDownServer()
 
1060
        self.stop_server()
1051
1061
        # if the listening socket has closed, we should get a BADFD error
1052
1062
        # when connecting, rather than a hang.
1053
 
        transport = remote.RemoteTCPTransport(server.get_url())
1054
 
        self.assertRaises(errors.ConnectionError, transport.has, '.')
 
1063
        t = remote.RemoteTCPTransport(server_url)
 
1064
        self.assertRaises(errors.ConnectionError, t.has, '.')
1055
1065
 
1056
1066
 
1057
1067
class WritableEndToEndTests(SmartTCPTests):
1192
1202
        self.transport.has('.')
1193
1203
        self.assertEqual([], self.hook_calls)
1194
1204
        # clean up the server
1195
 
        self.tearDownServer()
 
1205
        self.stop_server()
1196
1206
        # now it should have fired.
1197
1207
        self.assertEqual(result, self.hook_calls)
1198
1208
 
1211
1221
        self.transport.has('.')
1212
1222
        self.assertEqual([], self.hook_calls)
1213
1223
        # clean up the server
1214
 
        self.tearDownServer()
 
1224
        self.stop_server()
1215
1225
        # now it should have fired.
1216
1226
        self.assertEqual(result, self.hook_calls)
1217
1227