~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_test_server.py

  • Committer: Vincent Ladeuil
  • Date: 2011-10-06 09:20:02 UTC
  • mto: This revision was merged to the branch mainline in revision 6195.
  • Revision ID: v.ladeuil+lp@free.fr-20111006092002-5s63tmvqter71tlc
Open trunk again as 2.5dev3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2010, 2011 Canonical Ltd
 
2
#
 
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.
 
7
#
 
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.
 
12
#
 
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
 
16
 
 
17
import errno
 
18
import socket
 
19
import SocketServer
 
20
import threading
 
21
 
 
22
 
 
23
from bzrlib import (
 
24
    osutils,
 
25
    tests,
 
26
    )
 
27
from bzrlib.tests import test_server
 
28
from bzrlib.tests.scenarios import load_tests_apply_scenarios
 
29
 
 
30
 
 
31
load_tests = load_tests_apply_scenarios
 
32
 
 
33
 
 
34
def portable_socket_pair():
 
35
    """Return a pair of TCP sockets connected to each other.
 
36
 
 
37
    Unlike socket.socketpair, this should work on Windows.
 
38
    """
 
39
    listen_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
 
40
    listen_sock.bind(('127.0.0.1', 0))
 
41
    listen_sock.listen(1)
 
42
    client_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
 
43
    client_sock.connect(listen_sock.getsockname())
 
44
    server_sock, addr = listen_sock.accept()
 
45
    listen_sock.close()
 
46
    return server_sock, client_sock
 
47
 
 
48
 
 
49
class TCPClient(object):
 
50
 
 
51
    def __init__(self):
 
52
        self.sock = None
 
53
 
 
54
    def connect(self, addr):
 
55
        if self.sock is not None:
 
56
            raise AssertionError('Already connected to %r'
 
57
                                 % (self.sock.getsockname(),))
 
58
        self.sock = osutils.connect_socket(addr)
 
59
 
 
60
    def disconnect(self):
 
61
        if self.sock is not None:
 
62
            try:
 
63
                self.sock.shutdown(socket.SHUT_RDWR)
 
64
                self.sock.close()
 
65
            except socket.error, e:
 
66
                if e[0] in (errno.EBADF, errno.ENOTCONN):
 
67
                    # Right, the socket is already down
 
68
                    pass
 
69
                else:
 
70
                    raise
 
71
            self.sock = None
 
72
 
 
73
    def write(self, s):
 
74
        return self.sock.sendall(s)
 
75
 
 
76
    def read(self, bufsize=4096):
 
77
        return self.sock.recv(bufsize)
 
78
 
 
79
 
 
80
class TCPConnectionHandler(SocketServer.BaseRequestHandler):
 
81
 
 
82
    def handle(self):
 
83
        self.done = False
 
84
        self.handle_connection()
 
85
        while not self.done:
 
86
            self.handle_connection()
 
87
 
 
88
    def readline(self):
 
89
        # TODO: We should be buffering any extra data sent, etc. However, in
 
90
        #       practice, we don't send extra content, so we haven't bothered
 
91
        #       to implement it yet.
 
92
        req = self.request.recv(4096)
 
93
        # An empty string is allowed, to indicate the end of the connection
 
94
        if not req or (req.endswith('\n') and req.count('\n') == 1):
 
95
            return req
 
96
        raise ValueError('[%r] not a simple line' % (req,))
 
97
 
 
98
    def handle_connection(self):
 
99
        req = self.readline()
 
100
        if not req:
 
101
            self.done = True
 
102
        elif req == 'ping\n':
 
103
            self.request.sendall('pong\n')
 
104
        else:
 
105
            raise ValueError('[%s] not understood' % req)
 
106
 
 
107
 
 
108
class TestTCPServerInAThread(tests.TestCase):
 
109
 
 
110
    scenarios = [
 
111
        (name, {'server_class': getattr(test_server, name)})
 
112
        for name in
 
113
        ('TestingTCPServer', 'TestingThreadingTCPServer')]
 
114
 
 
115
    # Set by load_tests()
 
116
    server_class = None
 
117
 
 
118
    def get_server(self, server_class=None, connection_handler_class=None):
 
119
        if server_class is not None:
 
120
            self.server_class = server_class
 
121
        if connection_handler_class is None:
 
122
            connection_handler_class = TCPConnectionHandler
 
123
        server = test_server.TestingTCPServerInAThread(
 
124
            ('localhost', 0), self.server_class, connection_handler_class)
 
125
        server.start_server()
 
126
        self.addCleanup(server.stop_server)
 
127
        return server
 
128
 
 
129
    def get_client(self):
 
130
        client = TCPClient()
 
131
        self.addCleanup(client.disconnect)
 
132
        return client
 
133
 
 
134
    def get_server_connection(self, server, conn_rank):
 
135
        return server.server.clients[conn_rank]
 
136
 
 
137
    def assertClientAddr(self, client, server, conn_rank):
 
138
        conn = self.get_server_connection(server, conn_rank)
 
139
        self.assertEquals(client.sock.getsockname(), conn[1])
 
140
 
 
141
    def test_start_stop(self):
 
142
        server = self.get_server()
 
143
        client = self.get_client()
 
144
        server.stop_server()
 
145
        # since the server doesn't accept connections anymore attempting to
 
146
        # connect should fail
 
147
        client = self.get_client()
 
148
        self.assertRaises(socket.error,
 
149
                          client.connect, (server.host, server.port))
 
150
 
 
151
    def test_client_talks_server_respond(self):
 
152
        server = self.get_server()
 
153
        client = self.get_client()
 
154
        client.connect((server.host, server.port))
 
155
        self.assertIs(None, client.write('ping\n'))
 
156
        resp = client.read()
 
157
        self.assertClientAddr(client, server, 0)
 
158
        self.assertEquals('pong\n', resp)
 
159
 
 
160
    def test_server_fails_to_start(self):
 
161
        class CantStart(Exception):
 
162
            pass
 
163
 
 
164
        class CantStartServer(test_server.TestingTCPServer):
 
165
 
 
166
            def server_bind(self):
 
167
                raise CantStart()
 
168
 
 
169
        # The exception is raised in the main thread
 
170
        self.assertRaises(CantStart,
 
171
                          self.get_server, server_class=CantStartServer)
 
172
 
 
173
    def test_server_fails_while_serving_or_stopping(self):
 
174
        class CantConnect(Exception):
 
175
            pass
 
176
 
 
177
        class FailingConnectionHandler(TCPConnectionHandler):
 
178
 
 
179
            def handle(self):
 
180
                raise CantConnect()
 
181
 
 
182
        server = self.get_server(
 
183
            connection_handler_class=FailingConnectionHandler)
 
184
        # The server won't fail until a client connect
 
185
        client = self.get_client()
 
186
        client.connect((server.host, server.port))
 
187
        # We make sure the server wants to handle a request, but the request is
 
188
        # guaranteed to fail. However, the server should make sure that the
 
189
        # connection gets closed, and stop_server should then raise the
 
190
        # original exception.
 
191
        client.write('ping\n')
 
192
        try:
 
193
            self.assertEqual('', client.read())
 
194
        except socket.error, e:
 
195
            # On Windows, failing during 'handle' means we get
 
196
            # 'forced-close-of-connection'. Possibly because we haven't
 
197
            # processed the write request before we close the socket.
 
198
            WSAECONNRESET = 10054
 
199
            if e.errno in (WSAECONNRESET,):
 
200
                pass
 
201
        # Now the server has raised the exception in its own thread
 
202
        self.assertRaises(CantConnect, server.stop_server)
 
203
 
 
204
    def test_server_crash_while_responding(self):
 
205
        sync = threading.Event()
 
206
        sync.clear()
 
207
        class FailToRespond(Exception):
 
208
            pass
 
209
 
 
210
        class FailingDuringResponseHandler(TCPConnectionHandler):
 
211
 
 
212
            def handle_connection(self):
 
213
                req = self.readline()
 
214
                threading.currentThread().set_sync_event(sync)
 
215
                raise FailToRespond()
 
216
 
 
217
        server = self.get_server(
 
218
            connection_handler_class=FailingDuringResponseHandler)
 
219
        client = self.get_client()
 
220
        client.connect((server.host, server.port))
 
221
        client.write('ping\n')
 
222
        sync.wait()
 
223
        self.assertEqual('', client.read()) # connection closed
 
224
        self.assertRaises(FailToRespond, server.pending_exception)
 
225
 
 
226
    def test_exception_swallowed_while_serving(self):
 
227
        sync = threading.Event()
 
228
        sync.clear()
 
229
        class CantServe(Exception):
 
230
            pass
 
231
 
 
232
        class FailingWhileServingConnectionHandler(TCPConnectionHandler):
 
233
 
 
234
            def handle(self):
 
235
                # We want to sync with the thread that is serving the
 
236
                # connection.
 
237
                threading.currentThread().set_sync_event(sync)
 
238
                raise CantServe()
 
239
 
 
240
        server = self.get_server(
 
241
            connection_handler_class=FailingWhileServingConnectionHandler)
 
242
        # Install the exception swallower
 
243
        server.set_ignored_exceptions(CantServe)
 
244
        client = self.get_client()
 
245
        # Connect to the server so the exception is raised there
 
246
        client.connect((server.host, server.port))
 
247
        # Wait for the exception to propagate.
 
248
        sync.wait()
 
249
        self.assertEqual('', client.read()) # connection closed
 
250
        # The connection wasn't served properly but the exception should have
 
251
        # been swallowed.
 
252
        server.pending_exception()
 
253
 
 
254
    def test_handle_request_closes_if_it_doesnt_process(self):
 
255
        server = self.get_server()
 
256
        client = self.get_client()
 
257
        server.server.serving = False
 
258
        client.connect((server.host, server.port))
 
259
        self.assertEqual('', client.read())
 
260
 
 
261
 
 
262
class TestTestingSmartServer(tests.TestCase):
 
263
 
 
264
    def test_sets_client_timeout(self):
 
265
        server = test_server.TestingSmartServer(('localhost', 0), None, None,
 
266
            root_client_path='/no-such-client/path')
 
267
        self.assertEqual(test_server._DEFAULT_TESTING_CLIENT_TIMEOUT,
 
268
                         server._client_timeout)
 
269
        sock = socket.socket()
 
270
        h = server._make_handler(sock)
 
271
        self.assertEqual(test_server._DEFAULT_TESTING_CLIENT_TIMEOUT,
 
272
                         h._client_timeout)
 
273
 
 
274
 
 
275
class FakeServer(object):
 
276
    """Minimal implementation to pass to TestingSmartConnectionHandler"""
 
277
    backing_transport = None
 
278
    root_client_path = '/'
 
279
 
 
280
 
 
281
class TestTestingSmartConnectionHandler(tests.TestCase):
 
282
 
 
283
    def test_connection_timeout_suppressed(self):
 
284
        self.overrideAttr(test_server, '_DEFAULT_TESTING_CLIENT_TIMEOUT', 0.01)
 
285
        s = FakeServer()
 
286
        server_sock, client_sock = portable_socket_pair()
 
287
        # This should timeout quickly, but not generate an exception.
 
288
        handler = test_server.TestingSmartConnectionHandler(server_sock,
 
289
            server_sock.getpeername(), s)
 
290
 
 
291
    def test_connection_shutdown_while_serving_no_error(self):
 
292
        s = FakeServer()
 
293
        server_sock, client_sock = portable_socket_pair()
 
294
        class ShutdownConnectionHandler(
 
295
            test_server.TestingSmartConnectionHandler):
 
296
 
 
297
            def _build_protocol(self):
 
298
                self.finished = True
 
299
                return super(ShutdownConnectionHandler, self)._build_protocol()
 
300
        # This should trigger shutdown after the entering _build_protocol, and
 
301
        # we should exit cleanly, without raising an exception.
 
302
        handler = ShutdownConnectionHandler(server_sock,
 
303
            server_sock.getpeername(), s)