~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 19:38:55 UTC
  • mto: This revision was merged to the branch mainline in revision 5405.
  • Revision ID: john@arbash-meinel.com-20100901193855-bazh9hh25cpm1986
Use the simpler form for the test server

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
 
 
361
342
class TestingSFTPConnectionHandler(SocketServer.BaseRequestHandler):
362
343
 
363
344
    def setup(self):
364
345
        self.wrap_for_latency()
365
346
        tcs = self.server.test_case_server
366
 
        ssh_server = TransportWithStopEvent(self.request)
 
347
        ssh_server = paramiko.Transport(self.request)
367
348
        # Set it to a channel under 'bzr' so that we get debug info
368
349
        ssh_server.set_log_channel('bzr.paramiko.transport')
369
350
        ssh_server.add_server_key(tcs.get_host_key())
373
354
        server = tcs._server_interface(tcs)
374
355
        # This blocks until the key exchange has been done
375
356
        ssh_server.start_server(None, server)
376
 
        # Continue blocking until the run() loop has completed, this sets an
377
 
        # upper bound on how long a test can take, but 5s is pretty long, and
378
 
        # we don't really want to wait forever in case there really was a
379
 
        # problem.
380
 
        end_time = time.time() + 5.0
381
 
        do_join = True
382
 
        while True:
383
 
            ssh_server.stopped_event.wait(0.1)
384
 
            if not ssh_server.active:
385
 
                e = ssh_server.get_exception()
386
 
                if e is not None:
387
 
                    raise e
388
 
            else:
389
 
                # server is still actively running for requests
390
 
                if time.time() >= end_time:
391
 
                    sys.stderr.write("hung waiting for paramiko.Transport"
392
 
                                     " %s to finish" % (ssh_server.getName(),))
393
 
                    do_join = False
394
 
                    break
395
 
        if do_join:
396
 
            # We already know the thread should have exited, so this should
397
 
            # always be fast
398
 
            ssh_server.join(1.0)
 
357
        # Wait until the full conversation has been completed
 
358
        # TODO: We could play around with timeouts here...
 
359
        ssh_server.join()
399
360
 
400
361
    def wrap_for_latency(self):
401
362
        tcs = self.server.test_case_server