/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/tests/test_test_server.py

  • Committer: Jelmer Vernooij
  • Date: 2018-02-18 21:42:57 UTC
  • mto: This revision was merged to the branch mainline in revision 6859.
  • Revision ID: jelmer@jelmer.uk-20180218214257-jpevutp1wa30tz3v
Update TODO to reference Breezy, not Bazaar.

Show diffs side-by-side

added added

removed removed

Lines of Context:
66
66
                self.sock.shutdown(socket.SHUT_RDWR)
67
67
                self.sock.close()
68
68
            except socket.error as e:
69
 
                if e.errno in (errno.EBADF, errno.ENOTCONN, errno.ECONNRESET):
 
69
                if e[0] in (errno.EBADF, errno.ENOTCONN):
70
70
                    # Right, the socket is already down
71
71
                    pass
72
72
                else:
94
94
        #       to implement it yet.
95
95
        req = self.request.recv(4096)
96
96
        # An empty string is allowed, to indicate the end of the connection
97
 
        if not req or (req.endswith(b'\n') and req.count(b'\n') == 1):
 
97
        if not req or (req.endswith('\n') and req.count('\n') == 1):
98
98
            return req
99
99
        raise ValueError('[%r] not a simple line' % (req,))
100
100
 
102
102
        req = self.readline()
103
103
        if not req:
104
104
            self.done = True
105
 
        elif req == b'ping\n':
106
 
            self.request.sendall(b'pong\n')
 
105
        elif req == 'ping\n':
 
106
            self.request.sendall('pong\n')
107
107
        else:
108
108
            raise ValueError('[%s] not understood' % req)
109
109
 
152
152
        server = self.get_server()
153
153
        client = self.get_client()
154
154
        client.connect((server.host, server.port))
155
 
        self.assertIs(None, client.write(b'ping\n'))
 
155
        self.assertIs(None, client.write('ping\n'))
156
156
        resp = client.read()
157
157
        self.assertClientAddr(client, server, 0)
158
 
        self.assertEqual(b'pong\n', resp)
 
158
        self.assertEqual('pong\n', resp)
159
159
 
160
160
    def test_server_fails_to_start(self):
161
161
        class CantStart(Exception):
188
188
        # guaranteed to fail. However, the server should make sure that the
189
189
        # connection gets closed, and stop_server should then raise the
190
190
        # original exception.
191
 
        client.write(b'ping\n')
 
191
        client.write('ping\n')
192
192
        try:
193
 
            self.assertEqual(b'', client.read())
 
193
            self.assertEqual('', client.read())
194
194
        except socket.error as e:
195
195
            # On Windows, failing during 'handle' means we get
196
196
            # 'forced-close-of-connection'. Possibly because we haven't
218
218
            # We use 'request' instead of 'self' below because the test matters
219
219
            # more and we need a container to properly set connection_thread.
220
220
            def handle_connection(request):
221
 
                request.readline()
 
221
                req = request.readline()
222
222
                # Capture the thread and make it use 'caught' so we can wait on
223
223
                # the event that will be set when the exception is caught. We
224
224
                # also capture the thread to know where to look.
230
230
            connection_handler_class=FailingDuringResponseHandler)
231
231
        client = self.get_client()
232
232
        client.connect((server.host, server.port))
233
 
        client.write(b'ping\n')
 
233
        client.write('ping\n')
234
234
        # Wait for the exception to be caught
235
235
        caught.wait()
236
 
        self.assertEqual(b'', client.read())  # connection closed
 
236
        self.assertEqual('', client.read()) # connection closed
237
237
        # Check that the connection thread did catch the exception,
238
238
        # http://pad.lv/869366 was wrongly checking the server thread which
239
239
        # works for TestingTCPServer where the connection is handled in the
252
252
        # so the handler below can access it when it's executed (it's
253
253
        # instantiated when the request is processed)
254
254
        self.connection_thread = None
255
 
 
256
255
        class CantServe(Exception):
257
256
            pass
258
257
 
278
277
        client.connect((server.host, server.port))
279
278
        # Wait for the exception to be caught
280
279
        caught.wait()
281
 
        self.assertEqual(b'', client.read())  # connection closed
 
280
        self.assertEqual('', client.read()) # connection closed
282
281
        # The connection wasn't served properly but the exception should have
283
282
        # been swallowed (see test_server_crash_while_responding remark about
284
283
        # http://pad.lv/869366 explaining why we can't check the server thread
292
291
        server = self.get_server()
293
292
        client = self.get_client()
294
293
        server.server.serving = False
295
 
        try:
296
 
            client.connect((server.host, server.port))
297
 
            self.assertEqual(b'', client.read())
298
 
        except socket.error as e:
299
 
            if e.errno != errno.ECONNRESET:
300
 
                raise
 
294
        client.connect((server.host, server.port))
 
295
        self.assertEqual('', client.read())
301
296
 
302
297
 
303
298
class TestTestingSmartServer(tests.TestCase):
304
299
 
305
300
    def test_sets_client_timeout(self):
306
 
        server = test_server.TestingSmartServer(
307
 
            ('localhost', 0), None, None,
 
301
        server = test_server.TestingSmartServer(('localhost', 0), None, None,
308
302
            root_client_path='/no-such-client/path')
309
303
        self.assertEqual(test_server._DEFAULT_TESTING_CLIENT_TIMEOUT,
310
304
                         server._client_timeout)
327
321
        s = FakeServer()
328
322
        server_sock, client_sock = portable_socket_pair()
329
323
        # This should timeout quickly, but not generate an exception.
330
 
        test_server.TestingSmartConnectionHandler(
331
 
            server_sock, server_sock.getpeername(), s)
 
324
        handler = test_server.TestingSmartConnectionHandler(server_sock,
 
325
            server_sock.getpeername(), s)
332
326
 
333
327
    def test_connection_shutdown_while_serving_no_error(self):
334
328
        s = FakeServer()
335
329
        server_sock, client_sock = portable_socket_pair()
336
 
 
337
330
        class ShutdownConnectionHandler(
338
 
                test_server.TestingSmartConnectionHandler):
 
331
            test_server.TestingSmartConnectionHandler):
339
332
 
340
333
            def _build_protocol(self):
341
334
                self.finished = True
342
335
                return super(ShutdownConnectionHandler, self)._build_protocol()
343
336
        # This should trigger shutdown after the entering _build_protocol, and
344
337
        # we should exit cleanly, without raising an exception.
345
 
        ShutdownConnectionHandler(server_sock, server_sock.getpeername(), s)
 
338
        handler = ShutdownConnectionHandler(server_sock,
 
339
            server_sock.getpeername(), s)