~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_server.py

  • Committer: Vincent Ladeuil
  • Date: 2011-06-15 11:36:05 UTC
  • mto: This revision was merged to the branch mainline in revision 5975.
  • Revision ID: v.ladeuil+lp@free.fr-20110615113605-p7zyyfry9wy1hquc
Make ContentConflict resolution more robust

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
import SocketServer
20
20
import sys
21
21
import threading
22
 
import traceback
23
22
 
24
23
 
25
24
from bzrlib import (
26
25
    cethread,
27
 
    errors,
28
26
    osutils,
29
27
    transport,
30
28
    urlutils,
214
212
    def start_server(self, backing_server=None):
215
213
        """Setup the Chroot on backing_server."""
216
214
        if backing_server is not None:
217
 
            self.backing_transport = transport.get_transport_from_url(
 
215
            self.backing_transport = transport.get_transport(
218
216
                backing_server.get_url())
219
217
        else:
220
 
            self.backing_transport = transport.get_transport_from_path('.')
 
218
            self.backing_transport = transport.get_transport('.')
221
219
        self.backing_transport.clone('added-by-filter').ensure_base()
222
220
        self.filter_func = lambda x: 'added-by-filter/' + x
223
221
        super(TestingPathFilteringServer, self).start_server()
235
233
    def start_server(self, backing_server=None):
236
234
        """Setup the Chroot on backing_server."""
237
235
        if backing_server is not None:
238
 
            self.backing_transport = transport.get_transport_from_url(
 
236
            self.backing_transport = transport.get_transport(
239
237
                backing_server.get_url())
240
238
        else:
241
 
            self.backing_transport = transport.get_transport_from_path('.')
 
239
            self.backing_transport = transport.get_transport('.')
242
240
        super(TestingChrootServer, self).start_server()
243
241
 
244
242
    def get_bogus_url(self):
267
265
            #raise AssertionError('thread %s hung' % (self.name,))
268
266
 
269
267
 
270
 
class TestingTCPServerMixin(object):
 
268
class TestingTCPServerMixin:
271
269
    """Mixin to support running SocketServer.TCPServer in a thread.
272
270
 
273
271
    Tests are connecting from the main thread, the server has to be run in a
289
287
 
290
288
    def serve(self):
291
289
        self.serving = True
 
290
        self.stopped.clear()
292
291
        # We are listening and ready to accept connections
293
292
        self.started.set()
294
293
        try:
313
312
                self.process_request(request, client_address)
314
313
            except:
315
314
                self.handle_error(request, client_address)
316
 
        else:
317
 
            self.close_request(request)
 
315
                self.close_request(request)
318
316
 
319
317
    def get_request(self):
320
318
        return self.socket.accept()
334
332
        # The following can be used for debugging purposes, it will display the
335
333
        # exception and the traceback just when it occurs instead of waiting
336
334
        # for the thread to be joined.
 
335
 
337
336
        # SocketServer.BaseServer.handle_error(self, request, client_address)
338
 
 
339
 
        # We call close_request manually, because we are going to raise an
340
 
        # exception. The SocketServer implementation calls:
341
 
        #   handle_error(...)
342
 
        #   close_request(...)
343
 
        # But because we raise the exception, close_request will never be
344
 
        # triggered. This helps client not block waiting for a response when
345
 
        # the server gets an exception.
346
 
        self.close_request(request)
347
337
        raise
348
338
 
349
339
    def ignored_exceptions_during_shutdown(self, e):
429
419
        SocketServer.ThreadingTCPServer.__init__(self, server_address,
430
420
                                                 request_handler_class)
431
421
 
432
 
    def get_request(self):
 
422
    def get_request (self):
433
423
        """Get the request and client address from the socket."""
434
424
        sock, addr = TestingTCPServerMixin.get_request(self)
435
425
        # The thread is not create yet, it will be updated in process_request
450
440
        t = TestThread(
451
441
            sync_event=stopped,
452
442
            name='%s -> %s' % (client_address, self.server_address),
453
 
            target=self.process_request_thread,
454
 
            args=(started, stopped, request, client_address))
 
443
            target = self.process_request_thread,
 
444
            args = (started, stopped, request, client_address))
455
445
        # Update the client description
456
446
        self.clients.pop()
457
447
        self.clients.append((request, client_address, t))
463
453
        if debug_threads():
464
454
            sys.stderr.write('Client thread %s started\n' % (t.name,))
465
455
        # If an exception occured during the thread start, it will get raised.
466
 
        # In rare cases, an exception raised during the request processing may
467
 
        # also get caught here (see http://pad.lv/869366)
468
456
        t.pending_exception()
469
457
 
470
458
    # The following methods are called by the main thread
520
508
            sync_event=self.server.started,
521
509
            target=self.run_server)
522
510
        self._server_thread.start()
523
 
        # Wait for the server thread to start (i.e. release the lock)
 
511
        # Wait for the server thread to start (i.e release the lock)
524
512
        self.server.started.wait()
525
513
        # Get the real address, especially the port
526
514
        self.host, self.port = self.server.server_address
601
589
    def __init__(self, request, client_address, server):
602
590
        medium.SmartServerSocketStreamMedium.__init__(
603
591
            self, request, server.backing_transport,
604
 
            server.root_client_path,
605
 
            timeout=_DEFAULT_TESTING_CLIENT_TIMEOUT)
 
592
            server.root_client_path)
606
593
        request.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
607
594
        SocketServer.BaseRequestHandler.__init__(self, request, client_address,
608
595
                                                 server)
609
596
 
610
597
    def handle(self):
611
 
        try:
612
 
            while not self.finished:
613
 
                server_protocol = self._build_protocol()
614
 
                self._serve_one_request(server_protocol)
615
 
        except errors.ConnectionTimeout:
616
 
            # idle connections aren't considered a failure of the server
617
 
            return
618
 
 
619
 
 
620
 
_DEFAULT_TESTING_CLIENT_TIMEOUT = 60.0
 
598
        while not self.finished:
 
599
            server_protocol = self._build_protocol()
 
600
            self._serve_one_request(server_protocol)
 
601
 
621
602
 
622
603
class TestingSmartServer(TestingThreadingTCPServer, server.SmartTCPServer):
623
604
 
626
607
        TestingThreadingTCPServer.__init__(self, server_address,
627
608
                                           request_handler_class)
628
609
        server.SmartTCPServer.__init__(self, backing_transport,
629
 
            root_client_path, client_timeout=_DEFAULT_TESTING_CLIENT_TIMEOUT)
630
 
 
 
610
                                       root_client_path)
631
611
    def serve(self):
632
612
        self.run_server_started_hooks()
633
613
        try:
685
665
        self.chroot_server = ChrootServer(
686
666
            self.get_backing_transport(backing_transport_server))
687
667
        self.chroot_server.start_server()
688
 
        self.backing_transport = transport.get_transport_from_url(
 
668
        self.backing_transport = transport.get_transport(
689
669
            self.chroot_server.get_url())
690
670
        super(SmartTCPServer_for_testing, self).start_server()
691
671
 
697
677
 
698
678
    def get_backing_transport(self, backing_transport_server):
699
679
        """Get a backing transport from a server we are decorating."""
700
 
        return transport.get_transport_from_url(
701
 
            backing_transport_server.get_url())
 
680
        return transport.get_transport(backing_transport_server.get_url())
702
681
 
703
682
    def get_url(self):
704
683
        url = self.server.get_url()
715
694
    def get_backing_transport(self, backing_transport_server):
716
695
        """Get a backing transport from a server we are decorating."""
717
696
        url = 'readonly+' + backing_transport_server.get_url()
718
 
        return transport.get_transport_from_url(url)
 
697
        return transport.get_transport(url)
719
698
 
720
699
 
721
700
class SmartTCPServer_for_testing_v2_only(SmartTCPServer_for_testing):
736
715
    def get_backing_transport(self, backing_transport_server):
737
716
        """Get a backing transport from a server we are decorating."""
738
717
        url = 'readonly+' + backing_transport_server.get_url()
739
 
        return transport.get_transport_from_url(url)
 
718
        return transport.get_transport(url)