~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_server.py

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:
237
237
    """
238
238
 
239
239
    def __init__(self, *args, **kwargs):
 
240
        # There are cases where the calling thread must wait, yet, if an
 
241
        # exception occurs the event should be set so the caller is not
 
242
        # blocked.
 
243
        try:
 
244
            event = kwargs.pop('event')
 
245
        except KeyError:
 
246
            # If the caller didn't pass a specific event, create our own
 
247
            event = threading.Event()
240
248
        super(ThreadWithException, self).__init__(*args, **kwargs)
 
249
        self.running = event
241
250
        self.exception = None
242
251
 
243
252
    def run(self):
244
253
        """Overrides Thread.run to capture any exception."""
 
254
        self.running.clear()
245
255
        try:
246
256
            super(ThreadWithException, self).run()
247
257
        except Exception, e:
248
258
            self.exception = sys.exc_info()
 
259
        finally:
 
260
            # Make sure the calling thread is released
 
261
            self.running.set()
 
262
 
249
263
 
250
264
    def join(self, *args, **kwargs):
251
265
        """Overrides Thread.join to raise any exception caught.
260
274
        # means it didn't encounter an exception.
261
275
        super(ThreadWithException, self).join(*args, **kwargs)
262
276
        if self.exception is not None:
263
 
            raise self.exception
 
277
            exc_class, exc_value, exc_tb = self.exception
 
278
            raise exc_class, exc_value, exc_tb
264
279
 
265
280
 
266
281
class SmartTCPServer_for_testing(server.SmartTCPServer):