/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: 2020-05-06 02:13:25 UTC
  • mfrom: (7490.7.21 work)
  • mto: This revision was merged to the branch mainline in revision 7501.
  • Revision ID: jelmer@jelmer.uk-20200506021325-awbmmqu1zyorz7sj
Merge 3.1 branch.

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[0] in (errno.EBADF, errno.ENOTCONN):
 
69
                if e.errno in (errno.EBADF, errno.ENOTCONN, errno.ECONNRESET):
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('\n') and req.count('\n') == 1):
 
97
        if not req or (req.endswith(b'\n') and req.count(b'\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 == 'ping\n':
106
 
            self.request.sendall('pong\n')
 
105
        elif req == b'ping\n':
 
106
            self.request.sendall(b'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('ping\n'))
 
155
        self.assertIs(None, client.write(b'ping\n'))
156
156
        resp = client.read()
157
157
        self.assertClientAddr(client, server, 0)
158
 
        self.assertEqual('pong\n', resp)
 
158
        self.assertEqual(b'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('ping\n')
 
191
        client.write(b'ping\n')
192
192
        try:
193
 
            self.assertEqual('', client.read())
 
193
            self.assertEqual(b'', 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
 
                req = request.readline()
 
221
                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('ping\n')
 
233
        client.write(b'ping\n')
234
234
        # Wait for the exception to be caught
235
235
        caught.wait()
236
 
        self.assertEqual('', client.read()) # connection closed
 
236
        self.assertEqual(b'', 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
 
255
256
        class CantServe(Exception):
256
257
            pass
257
258
 
277
278
        client.connect((server.host, server.port))
278
279
        # Wait for the exception to be caught
279
280
        caught.wait()
280
 
        self.assertEqual('', client.read()) # connection closed
 
281
        self.assertEqual(b'', client.read())  # connection closed
281
282
        # The connection wasn't served properly but the exception should have
282
283
        # been swallowed (see test_server_crash_while_responding remark about
283
284
        # http://pad.lv/869366 explaining why we can't check the server thread
291
292
        server = self.get_server()
292
293
        client = self.get_client()
293
294
        server.server.serving = False
294
 
        client.connect((server.host, server.port))
295
 
        self.assertEqual('', client.read())
 
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
296
301
 
297
302
 
298
303
class TestTestingSmartServer(tests.TestCase):
299
304
 
300
305
    def test_sets_client_timeout(self):
301
 
        server = test_server.TestingSmartServer(('localhost', 0), None, None,
 
306
        server = test_server.TestingSmartServer(
 
307
            ('localhost', 0), None, None,
302
308
            root_client_path='/no-such-client/path')
303
309
        self.assertEqual(test_server._DEFAULT_TESTING_CLIENT_TIMEOUT,
304
310
                         server._client_timeout)
321
327
        s = FakeServer()
322
328
        server_sock, client_sock = portable_socket_pair()
323
329
        # This should timeout quickly, but not generate an exception.
324
 
        handler = test_server.TestingSmartConnectionHandler(server_sock,
325
 
            server_sock.getpeername(), s)
 
330
        test_server.TestingSmartConnectionHandler(
 
331
            server_sock, server_sock.getpeername(), s)
326
332
 
327
333
    def test_connection_shutdown_while_serving_no_error(self):
328
334
        s = FakeServer()
329
335
        server_sock, client_sock = portable_socket_pair()
 
336
 
330
337
        class ShutdownConnectionHandler(
331
 
            test_server.TestingSmartConnectionHandler):
 
338
                test_server.TestingSmartConnectionHandler):
332
339
 
333
340
            def _build_protocol(self):
334
341
                self.finished = True
335
342
                return super(ShutdownConnectionHandler, self)._build_protocol()
336
343
        # This should trigger shutdown after the entering _build_protocol, and
337
344
        # we should exit cleanly, without raising an exception.
338
 
        handler = ShutdownConnectionHandler(server_sock,
339
 
            server_sock.getpeername(), s)
 
345
        ShutdownConnectionHandler(server_sock, server_sock.getpeername(), s)