973
976
def external_url(self):
975
def get_bytes(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())
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()
986
smart_server.stop_background_thread()
981
class FlakyServer(test_server.SmartTCPServer_for_testing):
982
def get_backing_transport(self, backing_transport_server):
983
return FlakyTransport()
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,
992
self.assertContainsRe(str(err), 'some random exception')
989
995
class SmartTCPTests(tests.TestCase):
990
996
"""Tests for connection/end to end behaviour using the TCP server.
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.
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())
1025
def tearDownServer(self):
1032
def stop_server(self):
1033
"""Disconnect the client and stop the server.
1035
This must be re-entrant as some tests will call it explicitly in
1036
addition to the normal cleanup.
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
1035
1046
class TestServerSocketUsage(SmartTCPTests):
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())
1053
self.assertRaises(errors.ConnectionError, t.has, '.')
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()
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, '.')
1057
1067
class WritableEndToEndTests(SmartTCPTests):