1
# Copyright (C) 2010, 2011 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26
from bzrlib.tests import test_server
27
from bzrlib.tests.scenarios import load_tests_apply_scenarios
30
load_tests = load_tests_apply_scenarios
33
class TCPClient(object):
38
def connect(self, addr):
39
if self.sock is not None:
40
raise AssertionError('Already connected to %r'
41
% (self.sock.getsockname(),))
42
self.sock = osutils.connect_socket(addr)
45
if self.sock is not None:
47
self.sock.shutdown(socket.SHUT_RDWR)
49
except socket.error, e:
50
if e[0] in (errno.EBADF, errno.ENOTCONN):
51
# Right, the socket is already down
58
return self.sock.sendall(s)
60
def read(self, bufsize=4096):
61
return self.sock.recv(bufsize)
64
class TCPConnectionHandler(SocketServer.StreamRequestHandler):
68
self.handle_connection()
70
self.handle_connection()
72
def handle_connection(self):
73
req = self.rfile.readline()
77
self.wfile.write('pong\n')
79
raise ValueError('[%s] not understood' % req)
82
class TestTCPServerInAThread(tests.TestCase):
85
(name, {'server_class': getattr(test_server, name)})
87
('TestingTCPServer', 'TestingThreadingTCPServer')]
92
def get_server(self, server_class=None, connection_handler_class=None):
93
if server_class is not None:
94
self.server_class = server_class
95
if connection_handler_class is None:
96
connection_handler_class = TCPConnectionHandler
97
server = test_server.TestingTCPServerInAThread(
98
('localhost', 0), self.server_class, connection_handler_class)
100
self.addCleanup(server.stop_server)
103
def get_client(self):
105
self.addCleanup(client.disconnect)
108
def get_server_connection(self, server, conn_rank):
109
return server.server.clients[conn_rank]
111
def assertClientAddr(self, client, server, conn_rank):
112
conn = self.get_server_connection(server, conn_rank)
113
self.assertEquals(client.sock.getsockname(), conn[1])
115
def test_start_stop(self):
116
server = self.get_server()
117
client = self.get_client()
119
# since the server doesn't accept connections anymore attempting to
120
# connect should fail
121
client = self.get_client()
122
self.assertRaises(socket.error,
123
client.connect, (server.host, server.port))
125
def test_client_talks_server_respond(self):
126
server = self.get_server()
127
client = self.get_client()
128
client.connect((server.host, server.port))
129
self.assertIs(None, client.write('ping\n'))
131
self.assertClientAddr(client, server, 0)
132
self.assertEquals('pong\n', resp)
134
def test_server_fails_to_start(self):
135
class CantStart(Exception):
138
class CantStartServer(test_server.TestingTCPServer):
140
def server_bind(self):
143
# The exception is raised in the main thread
144
self.assertRaises(CantStart,
145
self.get_server, server_class=CantStartServer)
147
def test_server_fails_while_serving_or_stopping(self):
148
class CantConnect(Exception):
151
class FailingConnectionHandler(TCPConnectionHandler):
156
server = self.get_server(
157
connection_handler_class=FailingConnectionHandler)
158
# The server won't fail until a client connect
159
client = self.get_client()
160
client.connect((server.host, server.port))
162
# Now we must force the server to answer by sending the request and
163
# waiting for some answer. But since we don't control when the
164
# server thread will be given cycles, we don't control either
165
# whether our reads or writes may hang.
166
client.sock.settimeout(0.1)
167
client.write('ping\n')
171
# Now the server has raised the exception in its own thread
172
self.assertRaises(CantConnect, server.stop_server)
174
def test_server_crash_while_responding(self):
175
sync = threading.Event()
177
class FailToRespond(Exception):
180
class FailingDuringResponseHandler(TCPConnectionHandler):
182
def handle_connection(self):
183
req = self.rfile.readline()
184
threading.currentThread().set_sync_event(sync)
185
raise FailToRespond()
187
server = self.get_server(
188
connection_handler_class=FailingDuringResponseHandler)
189
client = self.get_client()
190
client.connect((server.host, server.port))
191
client.write('ping\n')
193
self.assertRaises(FailToRespond, server.pending_exception)
195
def test_exception_swallowed_while_serving(self):
196
sync = threading.Event()
198
class CantServe(Exception):
201
class FailingWhileServingConnectionHandler(TCPConnectionHandler):
204
# We want to sync with the thread that is serving the
206
threading.currentThread().set_sync_event(sync)
209
server = self.get_server(
210
connection_handler_class=FailingWhileServingConnectionHandler)
211
# Install the exception swallower
212
server.set_ignored_exceptions(CantServe)
213
client = self.get_client()
214
# Connect to the server so the exception is raised there
215
client.connect((server.host, server.port))
216
# Wait for the exception to propagate.
218
# The connection wasn't served properly but the exception should have
220
server.pending_exception()