~tcole/ubuntuone-client/request-queue-predicates

« back to all changes in this revision

Viewing changes to tests/syncdaemon/test_action_queue.py

  • Committer: tim.cole at canonical
  • Date: 2009-08-22 00:50:15 UTC
  • mfrom: (160.2.10 trunk)
  • Revision ID: tim.cole@canonical.com-20090822005015-6tdpcitod026r6z2
mergeĀ fromĀ trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
#
 
3
# Author: John R. Lenton <john.lenton@canonical.com>
 
4
#
 
5
# Copyright 2009 Canonical Ltd.
 
6
#
 
7
# This program is free software: you can redistribute it and/or modify it
 
8
# under the terms of the GNU General Public License version 3, as published
 
9
# by the Free Software Foundation.
 
10
#
 
11
# This program is distributed in the hope that it will be useful, but
 
12
# WITHOUT ANY WARRANTY; without even the implied warranties of
 
13
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
 
14
# PURPOSE.  See the GNU General Public License for more details.
 
15
#
 
16
# You should have received a copy of the GNU General Public License along
 
17
# with this program.  If not, see <http://www.gnu.org/licenses/>.
 
18
""" Tests for the action queue """
 
19
from __future__ import with_statement
 
20
 
 
21
import logging
 
22
import shutil
 
23
import unittest
 
24
 
 
25
import dbus
 
26
from dbus.mainloop.glib import DBusGMainLoop
 
27
from twisted.internet import defer
 
28
 
 
29
from contrib.testing.testcase import (
 
30
    BaseTwistedTestCase,
 
31
    MementoHandler,
 
32
)
 
33
 
 
34
from ubuntuone.syncdaemon.dbus_interface import DBusInterface
 
35
from ubuntuone.syncdaemon.main import Main
 
36
from ubuntuone.syncdaemon.action_queue import NoisyRequestQueue
 
37
 
 
38
DBusInterface.test = True
 
39
 
 
40
 
 
41
class AcionQueueTests(BaseTwistedTestCase):
 
42
    """ Basic tests to check ActionQueue """
 
43
 
 
44
    def setUp(self):
 
45
        '''
 
46
        prepare to run the test
 
47
        '''
 
48
        BaseTwistedTestCase.setUp(self)
 
49
        self.root = self.mktemp('root')
 
50
        self.shares = self.mktemp('shares')
 
51
        self.data = self.mktemp('data')
 
52
        self.handler = MementoHandler()
 
53
        self.handler.setLevel(logging.ERROR)
 
54
        self.main = Main(root_dir=self.root,
 
55
                         shares_dir=self.shares,
 
56
                         data_dir=self.data,
 
57
                         host='localhost', port=0,
 
58
                         dns_srv=False, ssl=False,
 
59
                         disable_ssl_verify=True,
 
60
                         realm='fake.realm',
 
61
                         mark_interval=60,
 
62
                         handshake_timeout=2,
 
63
                         glib_loop=DBusGMainLoop(set_as_default=True))
 
64
        logging.getLogger('ubuntuone.SyncDaemon').addHandler(self.handler)
 
65
        dbus.service.BusName.__del__ = lambda _: None
 
66
 
 
67
    def tearDown(self):
 
68
        '''
 
69
        cleanup after the test
 
70
        '''
 
71
        self.main.shutdown()
 
72
        shutil.rmtree(self.root)
 
73
        shutil.rmtree(self.shares)
 
74
        shutil.rmtree(self.data)
 
75
        for record in self.handler.records:
 
76
            exc_info = getattr(record, 'exc_info', None)
 
77
            if exc_info is not None:
 
78
                raise exc_info[0], exc_info[1], exc_info[2]
 
79
        BaseTwistedTestCase.tearDown(self)
 
80
 
 
81
    @defer.inlineCallbacks
 
82
    def test_content_queue_has_only_one_op_per_node(self):
 
83
        '''
 
84
        Check that the content queue uniquifies operations per node.
 
85
        '''
 
86
        yield self.main.start()
 
87
        # totally fake, we don't care: the messages are only validated on run
 
88
        self.main.action_q.download('foo', 'bar', 0, 0)
 
89
        self.main.action_q.upload('foo', 'bar', 0, 0, 0, 0, 0)
 
90
        self.assertEqual(len(self.main.action_q.content_queue.waiting), 1)
 
91
 
 
92
    @defer.inlineCallbacks
 
93
    def test_content_queue_has_only_one_op_per_node_even_counting_markers(self):
 
94
        '''
 
95
        Check that the content queue uniquifies operations per node
 
96
        even when some of the operations were added using markers.
 
97
        '''
 
98
        yield self.main.start()
 
99
        self.main.action_q.download('foo', 'bar', 0, 0)
 
100
        self.main.action_q.uuid_map.set('foo', 'feh')
 
101
        self.main.action_q.uuid_map.set('bar', 'bah')
 
102
        self.main.action_q.upload('feh', 'bah', 0, 0, 0, 0, 0)
 
103
        self.assertEqual(len(self.main.action_q.content_queue.waiting), 1)
 
104
 
 
105
    @defer.inlineCallbacks
 
106
    def test_aq_resolve_uuid_maybe(self):
 
107
        '''
 
108
        Check action_q.resolve_uuid_maybe does what it's supposed to
 
109
        '''
 
110
        yield self.main.start()
 
111
        self.assertEqual(self.main.action_q.resolve_uuid_maybe('foo'), 'foo')
 
112
        self.main.action_q.uuid_map.set('foo', 'feh')
 
113
        self.assertEqual(self.main.action_q.resolve_uuid_maybe('foo'), 'feh')
 
114
 
 
115
 
 
116
class TestNoisyRQ(unittest.TestCase):
 
117
    '''
 
118
    Tests for NoisyRequestQueue
 
119
    '''
 
120
 
 
121
    def test_noisy_rq_blurts_about_head(self):
 
122
        '''
 
123
        Test NRQ calls its callback when head is set
 
124
        '''
 
125
        rq = NoisyRequestQueue('name', None,
 
126
                               lambda h, w:
 
127
                                   setattr(self, 'result', (h, tuple(w))))
 
128
        rq._head = 'blah'
 
129
        self.assertEqual(self.result, ('blah', ()))
 
130
 
 
131
    def test_noisy_rq_blurts_about_waiting(self):
 
132
        '''
 
133
        Test NRQ calls its callback when the waiting queue is altered.
 
134
        '''
 
135
        class BlackHole(object):
 
136
            '''The universal tool.'''
 
137
            __call__ = __getattr__ = lambda *_, **__: BlackHole()
 
138
        class FakeCommand(object):
 
139
            '''Yet another fake action queue command'''
 
140
            def run(self):
 
141
                '''run that just succeeds'''
 
142
                return defer.succeed(None)
 
143
        def cb(head, waiting):
 
144
            '''NRQ testing callback'''
 
145
            evts.append((head, tuple(waiting)))
 
146
        evts = []
 
147
        cmd = FakeCommand()
 
148
        rq = NoisyRequestQueue('name', BlackHole(), cb)
 
149
        rq.queue('one')
 
150
        rq.queue_top(cmd)
 
151
        rq.run()
 
152
        self.assertEqual(evts, [(None, ()),           # __init__
 
153
                                (None, ('one',)),     # queue
 
154
                                (None, (cmd, 'one')), # queue_top
 
155
                                (cmd, ('one',)),      # run
 
156
                                (None, ('one',)),     # done
 
157
                                ])