38
38
hooks: An instance of SmartServerHooks.
41
def __init__(self, backing_transport, host='127.0.0.1', port=0):
41
def __init__(self, backing_transport, host='127.0.0.1', port=0,
42
root_client_path='/'):
42
43
"""Construct a new server.
44
45
To actually start it running, call either start_background_thread or
48
:param backing_transport: The transport to serve.
47
49
:param host: Name of the interface to listen on.
48
50
:param port: TCP port to listen on, or 0 to allocate a transient port.
51
:param root_client_path: The client path that will correspond to root
50
54
# let connections timeout so that we get a chance to terminate
51
55
# Keep a reference to the exceptions we want to catch because the socket
63
67
self.backing_transport = backing_transport
64
68
self._started = threading.Event()
65
69
self._stopped = threading.Event()
70
self.root_client_path = root_client_path
68
73
self._should_terminate = False
134
139
# propogates to the newly accepted socket.
135
140
conn.setblocking(True)
136
141
conn.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
137
handler = SmartServerSocketStreamMedium(conn, self.backing_transport)
142
handler = SmartServerSocketStreamMedium(
143
conn, self.backing_transport, self.root_client_path)
138
144
connection_thread = threading.Thread(None, handler.serve, name='smart-server-child')
139
145
connection_thread.setDaemon(True)
140
146
connection_thread.start()
205
211
def __init__(self):
206
212
SmartTCPServer.__init__(self, None)
213
self.client_path_extra = None
208
215
def get_backing_transport(self, backing_transport_server):
209
216
"""Get a backing transport from a server we are decorating."""
210
217
return transport.get_transport(backing_transport_server.get_url())
212
def setUp(self, backing_transport_server=None):
213
"""Set up server for testing"""
219
def setUp(self, backing_transport_server=None,
220
client_path_extra='/extra/'):
221
"""Set up server for testing.
223
:param backing_transport_server: backing server to use. If not
224
specified, a LocalURLServer at the current working directory will
226
:param client_path_extra: a path segment starting with '/' to append to
227
the root URL for this server. For instance, a value of '/foo/bar/'
228
will mean the root of the backing transport will be published at a
229
URL like `bzr://127.0.0.1:nnnn/foo/bar/`, rather than
230
`bzr://127.0.0.1:nnnn/`. Default value is `extra`, so that tests
231
by default will fail unless they do the necessary path translation.
233
assert client_path_extra.startswith('/')
214
234
from bzrlib.transport.chroot import ChrootServer
215
235
if backing_transport_server is None:
216
236
from bzrlib.transport.local import LocalURLServer
220
240
self.chroot_server.setUp()
221
241
self.backing_transport = transport.get_transport(
222
242
self.chroot_server.get_url())
243
self.root_client_path = self.client_path_extra = client_path_extra
223
244
self.start_background_thread()
225
246
def tearDown(self):
226
247
self.stop_background_thread()
227
248
self.chroot_server.tearDown()
251
url = super(SmartTCPServer_for_testing, self).get_url()
252
assert url.endswith('/')
253
return url[:-1] + self.client_path_extra
229
255
def get_bogus_url(self):
230
256
"""Return a URL which will fail to connect"""
231
257
return 'bzr://127.0.0.1:1/'