~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_server.py

  • Committer: Shannon Weyrick
  • Date: 2011-11-04 13:40:04 UTC
  • mfrom: (6238 +trunk)
  • mto: This revision was merged to the branch mainline in revision 6256.
  • Revision ID: weyrick@mozek.us-20111104134004-033t2wqhc3ydzm0a
Merge

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
22
23
 
23
24
 
24
25
from bzrlib import (
25
26
    cethread,
 
27
    errors,
26
28
    osutils,
27
29
    transport,
28
30
    urlutils,
287
289
 
288
290
    def serve(self):
289
291
        self.serving = True
290
 
        self.stopped.clear()
291
292
        # We are listening and ready to accept connections
292
293
        self.started.set()
293
294
        try:
312
313
                self.process_request(request, client_address)
313
314
            except:
314
315
                self.handle_error(request, client_address)
315
 
                self.close_request(request)
 
316
        else:
 
317
            self.close_request(request)
316
318
 
317
319
    def get_request(self):
318
320
        return self.socket.accept()
332
334
        # The following can be used for debugging purposes, it will display the
333
335
        # exception and the traceback just when it occurs instead of waiting
334
336
        # for the thread to be joined.
335
 
 
336
337
        # 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)
337
347
        raise
338
348
 
339
349
    def ignored_exceptions_during_shutdown(self, e):
419
429
        SocketServer.ThreadingTCPServer.__init__(self, server_address,
420
430
                                                 request_handler_class)
421
431
 
422
 
    def get_request (self):
 
432
    def get_request(self):
423
433
        """Get the request and client address from the socket."""
424
434
        sock, addr = TestingTCPServerMixin.get_request(self)
425
435
        # The thread is not create yet, it will be updated in process_request
440
450
        t = TestThread(
441
451
            sync_event=stopped,
442
452
            name='%s -> %s' % (client_address, self.server_address),
443
 
            target = self.process_request_thread,
444
 
            args = (started, stopped, request, client_address))
 
453
            target=self.process_request_thread,
 
454
            args=(started, stopped, request, client_address))
445
455
        # Update the client description
446
456
        self.clients.pop()
447
457
        self.clients.append((request, client_address, t))
453
463
        if debug_threads():
454
464
            sys.stderr.write('Client thread %s started\n' % (t.name,))
455
465
        # 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)
456
468
        t.pending_exception()
457
469
 
458
470
    # The following methods are called by the main thread
508
520
            sync_event=self.server.started,
509
521
            target=self.run_server)
510
522
        self._server_thread.start()
511
 
        # Wait for the server thread to start (i.e release the lock)
 
523
        # Wait for the server thread to start (i.e. release the lock)
512
524
        self.server.started.wait()
513
525
        # Get the real address, especially the port
514
526
        self.host, self.port = self.server.server_address
596
608
                                                 server)
597
609
 
598
610
    def handle(self):
599
 
        while not self.finished:
600
 
            server_protocol = self._build_protocol()
601
 
            self._serve_one_request(server_protocol)
602
 
 
603
 
 
604
 
_DEFAULT_TESTING_CLIENT_TIMEOUT = 4.0
 
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
605
621
 
606
622
class TestingSmartServer(TestingThreadingTCPServer, server.SmartTCPServer):
607
623