~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/stub_sftp.py

  • Committer: John Arbash Meinel
  • Date: 2010-09-01 18:37:26 UTC
  • mto: This revision was merged to the branch mainline in revision 5405.
  • Revision ID: john@arbash-meinel.com-20100901183726-gouh6wu8qtopwxwv
Use a separate completion event, loop on it, but don't loop forever.

Show diffs side-by-side

added added

removed removed

Lines of Context:
339
339
        return bytes_sent
340
340
 
341
341
 
 
342
class TransportWithStopEvent(paramiko.Transport):
 
343
    """Identical to a regular paramiko.Transport
 
344
 
 
345
    except when the main thread is finished processing events, we set an event,
 
346
    so that the rest of the code can wait for it.
 
347
    """
 
348
 
 
349
    def __init__(self, *args, **kwargs):
 
350
        super(TransportWithStopEvent, self).__init__(*args, **kwargs)
 
351
        self.stopped_event = threading.Event()
 
352
 
 
353
    def run(self):
 
354
        self.stopped_event.clear()
 
355
        try:
 
356
            super(TransportWithStopEvent, self).run()
 
357
        finally:
 
358
            self.stopped_event.set()
 
359
 
 
360
 
342
361
class TestingSFTPConnectionHandler(SocketServer.BaseRequestHandler):
343
362
 
344
363
    def setup(self):
345
364
        self.wrap_for_latency()
346
365
        tcs = self.server.test_case_server
347
 
        ssh_server = paramiko.Transport(self.request)
 
366
        ssh_server = TransportWithStopEvent(self.request)
348
367
        # Set it to a channel under 'bzr' so that we get debug info
349
368
        ssh_server.set_log_channel('bzr.paramiko.transport')
350
369
        ssh_server.add_server_key(tcs.get_host_key())
355
374
        # This blocks until the key exchange has been done
356
375
        ssh_server.start_server(None, server)
357
376
        # Continue blocking until the run() loop has completed
358
 
        ssh_server.completion_event.clear()
359
 
        ssh_server.completion_event.wait()
 
377
        end_time = time.time() + 1.0
 
378
        do_join = True
 
379
        while True:
 
380
            ssh_server.stopped_event.wait(0.1)
 
381
            if not ssh_server.active:
 
382
                e = ssh_server.get_exception()
 
383
                if e is not None:
 
384
                    raise e
 
385
            else:
 
386
                # server is still actively running for requests
 
387
                if time.time() >= end_time:
 
388
                    sys.stderr.write("hung waiting for paramiko.Transport"
 
389
                                     " %s to finish" % (ssh_server.getName(),))
 
390
                    do_join = False
 
391
                    break
 
392
        if do_join:
 
393
            # We already know the thread should have exited, so this should
 
394
            # always be fast
 
395
            ssh_server.join(1.0)
360
396
 
361
397
    def wrap_for_latency(self):
362
398
        tcs = self.server.test_case_server