/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_server.py

  • Committer: Jelmer Vernooij
  • Date: 2017-05-22 00:56:52 UTC
  • mfrom: (6621.2.26 py3_pokes)
  • Revision ID: jelmer@jelmer.uk-20170522005652-yjahcr9hwmjkno7n
Merge Python3 porting work ('py3 pokes')

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 
17
17
import errno
18
18
import socket
19
 
import SocketServer
 
19
try:
 
20
    import socketserver
 
21
except ImportError:
 
22
    import SocketServer as socketserver
20
23
import sys
21
24
import threading
22
25
import traceback
53
56
    The TestServer interface provides a server for a given transport. We use
54
57
    these servers as loopback testing tools. For any given transport the
55
58
    Servers it provides must either allow writing, or serve the contents
56
 
    of os.getcwdu() at the time start_server is called.
 
59
    of osutils.getcwd() at the time start_server is called.
57
60
 
58
61
    Note that these are real servers - they must implement all the things
59
62
    that we want bzr transports to take advantage of.
66
69
        a database like svn, or a memory only transport, it should return
67
70
        a connection to a newly established resource for this Server.
68
71
        Otherwise it should return a url that will provide access to the path
69
 
        that was os.getcwdu() when start_server() was called.
 
72
        that was osutils.getcwd() when start_server() was called.
70
73
 
71
74
        Subsequent calls will return the same resource.
72
75
        """
268
271
 
269
272
 
270
273
class TestingTCPServerMixin(object):
271
 
    """Mixin to support running SocketServer.TCPServer in a thread.
 
274
    """Mixin to support running socketserver.TCPServer in a thread.
272
275
 
273
276
    Tests are connecting from the main thread, the server has to be run in a
274
277
    separate thread.
334
337
        # The following can be used for debugging purposes, it will display the
335
338
        # exception and the traceback just when it occurs instead of waiting
336
339
        # for the thread to be joined.
337
 
        # SocketServer.BaseServer.handle_error(self, request, client_address)
 
340
        # socketserver.BaseServer.handle_error(self, request, client_address)
338
341
 
339
342
        # We call close_request manually, because we are going to raise an
340
 
        # exception. The SocketServer implementation calls:
 
343
        # exception. The socketserver implementation calls:
341
344
        #   handle_error(...)
342
345
        #   close_request(...)
343
346
        # But because we raise the exception, close_request will never be
381
384
        try:
382
385
            sock.shutdown(socket.SHUT_RDWR)
383
386
            sock.close()
384
 
        except Exception, e:
 
387
        except Exception as e:
385
388
            if self.ignored_exceptions(e):
386
389
                pass
387
390
            else:
401
404
        thread.pending_exception()
402
405
 
403
406
 
404
 
class TestingTCPServer(TestingTCPServerMixin, SocketServer.TCPServer):
 
407
class TestingTCPServer(TestingTCPServerMixin, socketserver.TCPServer):
405
408
 
406
409
    def __init__(self, server_address, request_handler_class):
407
410
        TestingTCPServerMixin.__init__(self)
408
 
        SocketServer.TCPServer.__init__(self, server_address,
 
411
        socketserver.TCPServer.__init__(self, server_address,
409
412
                                        request_handler_class)
410
413
 
411
414
    def get_request(self):
422
425
 
423
426
 
424
427
class TestingThreadingTCPServer(TestingTCPServerMixin,
425
 
                                SocketServer.ThreadingTCPServer):
 
428
                                socketserver.ThreadingTCPServer):
426
429
 
427
430
    def __init__(self, server_address, request_handler_class):
428
431
        TestingTCPServerMixin.__init__(self)
429
 
        SocketServer.ThreadingTCPServer.__init__(self, server_address,
 
432
        socketserver.ThreadingTCPServer.__init__(self, server_address,
430
433
                                                 request_handler_class)
431
434
 
432
435
    def get_request(self):
441
444
        started.set()
442
445
        # We will be on our own once the server tells us we're detached
443
446
        detached.wait()
444
 
        SocketServer.ThreadingTCPServer.process_request_thread(
 
447
        socketserver.ThreadingTCPServer.process_request_thread(
445
448
            self, request, client_address)
446
449
        self.close_request(request)
447
450
        stopped.set()
559
562
            last_conn = None
560
563
            try:
561
564
                last_conn = osutils.connect_socket((self.host, self.port))
562
 
            except socket.error, e:
 
565
            except socket.error as e:
563
566
                # But ignore connection errors as the point is to unblock the
564
567
                # server thread, it may happen that it's not blocked or even
565
568
                # not started.
578
581
            # thread
579
582
            try:
580
583
                self._server_thread.join()
581
 
            except Exception, e:
 
584
            except Exception as e:
582
585
                if self.server.ignored_exceptions(e):
583
586
                    pass
584
587
                else:
599
602
        self.server._pending_exception(self._server_thread)
600
603
 
601
604
 
602
 
class TestingSmartConnectionHandler(SocketServer.BaseRequestHandler,
 
605
class TestingSmartConnectionHandler(socketserver.BaseRequestHandler,
603
606
                                    medium.SmartServerSocketStreamMedium):
604
607
 
605
608
    def __init__(self, request, client_address, server):
608
611
            server.root_client_path,
609
612
            timeout=_DEFAULT_TESTING_CLIENT_TIMEOUT)
610
613
        request.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
611
 
        SocketServer.BaseRequestHandler.__init__(self, request, client_address,
 
614
        socketserver.BaseRequestHandler.__init__(self, request, client_address,
612
615
                                                 server)
613
616
 
614
617
    def handle(self):