~didrocks/ubuntuone-client/dont-suffer-zg-crash

« back to all changes in this revision

Viewing changes to tests/syncdaemon/test_action_predicates.py

  • Committer: Bazaar Package Importer
  • Author(s): Rodney Dawes
  • Date: 2011-01-25 16:42:52 UTC
  • mto: This revision was merged to the branch mainline in revision 64.
  • Revision ID: james.westby@ubuntu.com-20110125164252-rl1pybasx1nsqgoy
Tags: upstream-1.5.3
ImportĀ upstreamĀ versionĀ 1.5.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# ubuntuone.syncdaemon.tests.test_action_queue - action queue tests
2
 
#
3
 
# Author: Tim Cole <tim.cole@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
 
""" ActionQueue tests """
19
 
 
20
 
from ubuntuone.syncdaemon.action_queue import (
21
 
    ActionQueue, ActionQueueCommand, RequestQueue, WaitForCondition)
22
 
from twisted.internet import defer
23
 
from twisted.internet.defer import inlineCallbacks
24
 
from twisted.trial.unittest import TestCase as TwistedTestCase
25
 
 
26
 
 
27
 
class AppendCommand(ActionQueueCommand):
28
 
    """Simple command which appends a value to a list."""
29
 
 
30
 
    def __init__(self, request_queue, append_to, value):
31
 
        """Initialize, remembering list and value."""
32
 
        super(AppendCommand, self).__init__(request_queue)
33
 
        self.append_to = append_to
34
 
        self.value = value
35
 
 
36
 
    def _run(self):
37
 
        """Do the actual append."""
38
 
        self.append_to.append(self.value)
39
 
        return defer.succeed(None)
40
 
 
41
 
    def __repr__(self):
42
 
        """Helper when debugging."""
43
 
        return "<AppendCommand: %s>" % (self.value,)
44
 
    __str__ = __repr__
45
 
 
46
 
 
47
 
class FakeEventQueue(object):
48
 
    """Fake event queue."""
49
 
 
50
 
    def __init__(self):
51
 
        """Initialize fake event queue instance."""
52
 
        self.events = []
53
 
        self._listeners = []
54
 
 
55
 
    def subscribe(self, listener):
56
 
        """Minimum to deal with listener."""
57
 
        self._listeners.append(listener)
58
 
 
59
 
    def push(self, name, *args, **kw):
60
 
        """Push event onto queue."""
61
 
        self.events.append((name, args, kw))
62
 
 
63
 
 
64
 
class FakeActionQueue(object):
65
 
    """Fake action queue."""
66
 
 
67
 
    def __init__(self, event_queue):
68
 
        """Initialize fake action queue instance."""
69
 
        self.event_queue = event_queue
70
 
 
71
 
 
72
 
class CommandTests(TwistedTestCase):
73
 
    """Isolated tests of the action queue command running infrastructure."""
74
 
 
75
 
    @inlineCallbacks
76
 
    def setUp(self):
77
 
        """Set up mocked event queue and so on."""
78
 
        yield TwistedTestCase.setUp(self)
79
 
        self.event_queue = FakeEventQueue()
80
 
        self.action_queue = ActionQueue(self.event_queue, main=None,
81
 
                                        host="bogus", port=1234,
82
 
                                        dns_srv=False)
83
 
        self.request_queue = RequestQueue("TEST_QUEUE",
84
 
                                          self.action_queue)
85
 
 
86
 
    def test_simple_commands(self):
87
 
        """Test command execution."""
88
 
        out = []
89
 
        AppendCommand(self.request_queue,
90
 
                      append_to=out, value="a").queue()
91
 
        AppendCommand(self.request_queue,
92
 
                      append_to=out, value="b").queue()
93
 
        self.request_queue.run()
94
 
        self.request_queue.run()
95
 
        self.assertEqual(["a", "b"], out)
96
 
 
97
 
    def test_blocked_command_commutative(self):
98
 
        """Test blocked command execution in a commutative queue."""
99
 
        self.request_queue.commutative = True
100
 
        out = []
101
 
        cmds = [AppendCommand(self.request_queue, append_to=out, value=v) \
102
 
                for v in ("a", "b", "c")]
103
 
        for cmd in cmds:
104
 
            cmd.queue()
105
 
        cmds[1].is_runnable = False
106
 
        self.request_queue.run()
107
 
        self.request_queue.run()
108
 
        self.assertEqual(["a", "c"], out)
109
 
        self.request_queue.run()
110
 
        self.assertEqual(["a", "c"], out)
111
 
        cmds[1].is_runnable = True
112
 
        self.request_queue.check_conditions()
113
 
        self.request_queue.run()
114
 
        self.assertEqual(["a", "c", "b"], out)
115
 
 
116
 
    def test_blocked_command_noncommutative(self):
117
 
        """Test blocked command execution in a non-commutative queue."""
118
 
        out = []
119
 
        cmds = [AppendCommand(self.request_queue, append_to=out, value=v) \
120
 
                for v in ("a", "b", "c")]
121
 
        for cmd in cmds:
122
 
            cmd.queue()
123
 
        cmds[1].is_runnable = False
124
 
        self.request_queue.run()
125
 
        self.request_queue.run()
126
 
        self.assertEqual(["a"], out)
127
 
        cmds[1].is_runnable = True
128
 
        self.request_queue.check_conditions()
129
 
        self.request_queue.run()
130
 
        self.assertEqual(["a", "b"], out)
131
 
        self.request_queue.run()
132
 
        self.assertEqual(["a", "b", "c"], out)
133
 
 
134
 
    def test_predicate_satisfaction(self):
135
 
        """Test that waits really wait."""
136
 
        fixture = [False]
137
 
        cmd = WaitForCondition(self.request_queue,
138
 
                               condition=lambda: fixture[0])
139
 
        cmd.queue()
140
 
        d = self.request_queue.run()
141
 
        d.addCallback(lambda _: fixture.append('x'))
142
 
 
143
 
        self.request_queue.check_conditions()
144
 
        self.assertEqual(1, len(fixture))
145
 
        self.assertEqual(0, len(self.request_queue.waiting))
146
 
 
147
 
        fixture[0] = True
148
 
        self.request_queue.check_conditions()
149
 
        self.assertEqual(2, len(fixture))
150
 
        self.assertEqual(0, len(self.request_queue.waiting))