~facundo/ubuntuone-client/lr-dont-delete-partial-nopartialfile

« back to all changes in this revision

Viewing changes to tests/syncdaemon/test_action_queue.py

  • Committer: Tarmac
  • Author(s): facundo at com
  • Date: 2010-09-24 20:52:03 UTC
  • mfrom: (720.1.3 ping-it-baby)
  • Revision ID: tarmac-20100924205203-ql0vtr0qezvuwwoc
Client periodically pings the server to check connection.

Show diffs side-by-side

added added

removed removed

Lines of Context:
49
49
from ubuntuone.syncdaemon import states
50
50
from ubuntuone.syncdaemon.action_queue import (
51
51
    ActionQueue, ActionQueueCommand, ChangePublicAccess, CreateUDF,
52
 
    DeleteVolume, Download, ListVolumes,
 
52
    DeleteVolume, Download, ListVolumes, ActionQueueProtocol,
53
53
    NoisyRequestQueue, RequestQueue, UploadProgressWrapper, Upload,
54
54
    CreateShare, DeleteShare, GetPublicFiles, GetDelta, GetDeltaFromScratch,
55
55
    TRANSFER_PROGRESS_THRESHOLD, Unlink, Move, MakeFile, MakeDir,
3726
3726
        self.assertTrue(self.handler.check_note('Session ID: %r' %
3727
3727
                                                str(request.session_id)))
3728
3728
 
 
3729
 
 
3730
class ActionQueueProtocolTests(TwistedTestCase):
 
3731
    """Test the ACQ class."""
 
3732
 
 
3733
    def setUp(self):
 
3734
        """Set up."""
 
3735
        # create an AQP and put a factory to it
 
3736
        self.aqp = ActionQueueProtocol()
 
3737
        obj = Mocker().mock()
 
3738
        obj.event_queue.push('SYS_CONNECTION_MADE')
 
3739
        self.aqp.factory = obj
 
3740
 
 
3741
        # set up the logger
 
3742
        self.handler = MementoHandler()
 
3743
        self.handler.setLevel(logging.DEBUG)
 
3744
        self.aqp.log.addHandler(self.handler)
 
3745
 
 
3746
    def tearDown(self):
 
3747
        """Tear down."""
 
3748
        self.aqp.log.removeHandler(self.handler)
 
3749
        task = self.aqp._looping_ping
 
3750
        if task is not None and task.running:
 
3751
            task.stop()
 
3752
 
 
3753
    def test_connection_made(self):
 
3754
        """Connection is made."""
 
3755
        mocker = Mocker()
 
3756
        obj = mocker.mock()
 
3757
        obj.event_queue.push('SYS_CONNECTION_MADE')
 
3758
        self.aqp.factory = obj
 
3759
 
 
3760
        # test
 
3761
        with mocker:
 
3762
            self.aqp.connectionMade()
 
3763
        self.assertTrue(self.handler.check_info('Connection made.'))
 
3764
        self.assertFalse(self.aqp._looping_ping is None)
 
3765
 
 
3766
    def test_connection_lost(self):
 
3767
        """Connection is lost."""
 
3768
        self.aqp.connectionLost('foo')
 
3769
        self.assertTrue(self.handler.check_info(
 
3770
                        'Connection lost, reason: foo.'))
 
3771
        self.assertTrue(self.aqp._looping_ping is None)
 
3772
 
 
3773
    def test_ping_connection_made_twice(self):
 
3774
        """If connection made is called twice, don't create two tasks."""
 
3775
        self.aqp.connectionMade()
 
3776
        task1 = self.aqp._looping_ping
 
3777
        self.aqp.connectionMade()
 
3778
        task2 = self.aqp._looping_ping
 
3779
        self.assertTrue(task1 is task2)
 
3780
 
 
3781
    def test_ping_connection_lost_twice(self):
 
3782
        """If connection lost is called twice, don't stop None."""
 
3783
        self.aqp.connectionMade()
 
3784
        self.assertFalse(self.aqp._looping_ping is None)
 
3785
        self.aqp.connectionLost('reason')
 
3786
        self.assertTrue(self.aqp._looping_ping is None)
 
3787
        self.aqp.connectionLost('reason')
 
3788
        self.assertTrue(self.aqp._looping_ping is None)
 
3789
 
 
3790
    @defer.inlineCallbacks
 
3791
    def test_ping_task_calls_ping(self):
 
3792
        """The task will call the _do_ping method."""
 
3793
        self.aqp._ping_delay = .1
 
3794
        deferred = defer.Deferred()
 
3795
 
 
3796
        def fake_ping():
 
3797
            """Stop the loop and trigger the deferred test."""
 
3798
            self.aqp._looping_ping.stop()
 
3799
            deferred.callback(True)
 
3800
 
 
3801
        self.aqp._do_ping = fake_ping
 
3802
        self.aqp.connectionMade()
 
3803
        yield deferred
 
3804
 
 
3805
    def test_ping_do_ping(self):
 
3806
        """Ping and log."""
 
3807
        # mock the request
 
3808
        mocker = Mocker()
 
3809
        req = mocker.mock()
 
3810
        req.rtt
 
3811
        mocker.result(1.123123)
 
3812
        self.aqp.ping = lambda: defer.succeed(req)
 
3813
 
 
3814
        # ping will be called, and req accessed, otherwise mocker will complain
 
3815
        with mocker:
 
3816
            self.aqp._do_ping()
 
3817
 
 
3818
        # check also the log
 
3819
        self.assertTrue(self.handler.check_debug('Ping! rtt: 1.123 segs'))
 
3820