~jameinel/bzr/leaking-test-experiment

« back to all changes in this revision

Viewing changes to bzrlib/tests/http_server.py

  • Committer: Vincent Ladeuil
  • Date: 2010-05-26 15:25:59 UTC
  • Revision ID: v.ladeuil+lp@free.fr-20100526152559-che4hqfn4pwo8s6g
Add an event to ThreadWithException that can be shared with the calling thread.

* bzrlib/tests/test_server.py:
(ThreadWithException.__init__): Add an 'event' parameter that can
be shared with the caller.
(ThreadWithException.run): Sset the event once the exception has
been recorded so we can release the caller and provide the
exception.
(ThreadWithException.join): Properly raise the recorded exception.

* bzrlib/tests/test_http.py:
(TestHTTPServer.test_force_invalid_protocol): Just rely on
assertRaises or we don't get the traceback on error.
(TestHTTPServer.test_server_start_and_stop): Cleanup.

* bzrlib/tests/http_server.py:
(HttpServer._http_start): Simplified.
(HttpServer.start_server): Leave the ThreadWithException do its
work.
(HttpServer.stop_server): Shutdown the server only if it was
started.

Show diffs side-by-side

added added

removed removed

Lines of Context:
626
626
 
627
627
    def _http_start(self, started):
628
628
        """Server thread main entry point. """
629
 
        server = None
630
 
        try:
631
 
            server = self._get_httpd()
632
 
            self._http_base_url = '%s://%s:%s/' % (self._url_protocol,
633
 
                                                   self.host, self.port)
634
 
        except:
635
 
            # Whatever goes wrong, we save the exception for the main
636
 
            # thread. Note that since we are running in a thread, no signal
637
 
            # can be received, so we don't care about KeyboardInterrupt.
638
 
            self._http_exception = sys.exc_info()
639
 
 
640
 
        if server is not None:
641
 
            # From now on, exceptions are taken care of by the
642
 
            # SocketServer.BaseServer or the request handler.
643
 
            server.serve(started)
644
 
        if not started.isSet():
645
 
            # Hmm, something went wrong, but we can release the caller anyway
646
 
            started.set()
 
629
        server = self._get_httpd()
 
630
        self._http_base_url = '%s://%s:%s/' % (self._url_protocol,
 
631
                                               self.host, self.port)
 
632
        server.serve(started)
647
633
 
648
634
    def _get_remote_url(self, path):
649
635
        path_parts = path.split(os.path.sep)
679
665
        self._home_dir = os.getcwdu()
680
666
        self._local_path_parts = self._home_dir.split(os.path.sep)
681
667
        self._http_base_url = None
 
668
        self.logs = []
682
669
 
683
670
        # Create the server thread
684
671
        started = threading.Event()
685
 
        self._http_thread = threading.Thread(target=self._http_start,
686
 
                                             args = (started,))
 
672
        self._http_thread = test_server.ThreadWithException(
 
673
            event=started, target=self._http_start, args=(started,))
687
674
        self._http_thread.setDaemon(True)
688
 
        self._http_exception = None
689
675
        self._http_thread.start()
690
676
        # Wait for the server thread to start (i.e release the lock)
691
677
        started.wait()
692
 
        self._http_thread.name = self._http_base_url
693
 
        if 'threads' in tests.selftest_debug_flags:
694
 
            print 'Thread started: %s' % (self._http_thread.name,)
695
 
 
696
 
 
697
 
        if self._http_exception is not None:
698
 
            # Something went wrong during server start
699
 
            exc_class, exc_value, exc_tb = self._http_exception
700
 
            raise exc_class, exc_value, exc_tb
701
 
        self.logs = []
 
678
        if self._httpd is None:
 
679
            if 'threads' in tests.selftest_debug_flags:
 
680
                print 'Server %s:% start failed ' % (self.host, self.port)
 
681
        else:
 
682
            self._http_thread.name = self._http_base_url
 
683
            if 'threads' in tests.selftest_debug_flags:
 
684
                print 'Thread started: %s' % (self._http_thread.name,)
 
685
 
 
686
        # If an exception occured during the server start, it will get raised
 
687
        self._http_thread.join(timeout=0)
702
688
 
703
689
    def stop_server(self):
704
690
        """See bzrlib.transport.Server.tearDown."""
705
 
        self._httpd.shutdown()
706
 
        if 'threads' in tests.selftest_debug_flags:
707
 
            print 'Try    joining: %s' % (self._http_thread.name,)
708
 
        self._httpd.join_thread(self._http_thread)
709
 
        if 'threads' in tests.selftest_debug_flags:
710
 
            print 'Thread  joined: %s' % (self._http_thread.name,)
 
691
        if self._httpd is not None:
 
692
            # The server has been started successfully, shut it down now
 
693
            self._httpd.shutdown()
 
694
            if 'threads' in tests.selftest_debug_flags:
 
695
                print 'Try    joining: %s' % (self._http_thread.name,)
 
696
            self._httpd.join_thread(self._http_thread)
 
697
            if 'threads' in tests.selftest_debug_flags:
 
698
                print 'Thread  joined: %s' % (self._http_thread.name,)
711
699
 
712
700
    def get_url(self):
713
701
        """See bzrlib.transport.Server.get_url."""