summaryrefslogtreecommitdiffstats
path: root/deluge/tests/test_component.py
blob: 0345e24d3bab18fc2a6b1cc2c3f3d8793ddec806 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#
# This file is part of Deluge and is licensed under GNU General Public License 3.0, or later, with
# the additional special exception to link portions of this program with the OpenSSL library.
# See LICENSE for more details.
#

import pytest
import pytest_twisted
from twisted.internet import defer, threads

import deluge.component as component


class ComponentTester(component.Component):
    def __init__(self, name, depend=None):
        component.Component.__init__(self, name, depend=depend)
        self.start_count = 0
        self.stop_count = 0

    def start(self):
        self.start_count += 1

    def stop(self):
        self.stop_count += 1


class ComponentTesterDelayStart(ComponentTester):
    def start(self):
        def do_sleep():
            import time

            time.sleep(1)

        d = threads.deferToThread(do_sleep)

        def on_done(result):
            self.start_count += 1

        return d.addCallback(on_done)


class ComponentTesterUpdate(component.Component):
    def __init__(self, name):
        component.Component.__init__(self, name)
        self.counter = 0
        self.start_count = 0
        self.stop_count = 0

    def update(self):
        self.counter += 1

    def stop(self):
        self.stop_count += 1


class ComponentTesterShutdown(component.Component):
    def __init__(self, name):
        component.Component.__init__(self, name)
        self.shutdowned = False
        self.stop_count = 0

    def shutdown(self):
        self.shutdowned = True

    def stop(self):
        self.stop_count += 1


@pytest.mark.usefixtures('component')
class TestComponent:
    def tear_down(self):
        return component.shutdown()

    def test_start_component(self):
        def on_start(result, c):
            assert c._component_state == 'Started'
            assert c.start_count == 1

        c = ComponentTester('test_start_c1')
        d = component.start(['test_start_c1'])
        d.addCallback(on_start, c)
        return d

    def test_start_stop_depends(self):
        def on_stop(result, c1, c2):
            assert c1._component_state == 'Stopped'
            assert c2._component_state == 'Stopped'
            assert c1.stop_count == 1
            assert c2.stop_count == 1

        def on_start(result, c1, c2):
            assert c1._component_state == 'Started'
            assert c2._component_state == 'Started'
            assert c1.start_count == 1
            assert c2.start_count == 1
            return component.stop(['test_start_depends_c1']).addCallback(
                on_stop, c1, c2
            )

        c1 = ComponentTester('test_start_depends_c1')
        c2 = ComponentTester('test_start_depends_c2', depend=['test_start_depends_c1'])

        d = component.start(['test_start_depends_c2'])
        d.addCallback(on_start, c1, c2)
        return d

    def start_with_depends(self):
        c1 = ComponentTesterDelayStart('test_start_all_c1')
        c2 = ComponentTester('test_start_all_c2', depend=['test_start_all_c4'])
        c3 = ComponentTesterDelayStart(
            'test_start_all_c3', depend=['test_start_all_c5', 'test_start_all_c1']
        )
        c4 = ComponentTester('test_start_all_c4', depend=['test_start_all_c3'])
        c5 = ComponentTester('test_start_all_c5')

        d = component.start()
        return (d, c1, c2, c3, c4, c5)

    def finish_start_with_depends(self, *args):
        for c in args[1:]:
            component.deregister(c)

    def test_start_all(self):
        def on_start(*args):
            for c in args[1:]:
                assert c._component_state == 'Started'
                assert c.start_count == 1

        ret = self.start_with_depends()
        ret[0].addCallback(on_start, *ret[1:])
        ret[0].addCallback(self.finish_start_with_depends, *ret[1:])
        return ret[0]

    def test_register_exception(self):
        ComponentTester('test_register_exception_c1')
        with pytest.raises(component.ComponentAlreadyRegistered):
            ComponentTester(
                'test_register_exception_c1',
            )

    def test_stop_component(self):
        def on_stop(result, c):
            assert c._component_state == 'Stopped'
            assert not c._component_timer.running
            assert c.stop_count == 1

        def on_start(result, c):
            assert c._component_state == 'Started'
            return component.stop(['test_stop_component_c1']).addCallback(on_stop, c)

        c = ComponentTesterUpdate('test_stop_component_c1')
        d = component.start(['test_stop_component_c1'])
        d.addCallback(on_start, c)
        return d

    def test_stop_all(self):
        def on_stop(result, *args):
            for c in args:
                assert c._component_state == 'Stopped'
                assert c.stop_count == 1

        def on_start(result, *args):
            for c in args:
                assert c._component_state == 'Started'
            return component.stop().addCallback(on_stop, *args)

        ret = self.start_with_depends()
        ret[0].addCallback(on_start, *ret[1:])
        ret[0].addCallback(self.finish_start_with_depends, *ret[1:])
        return ret[0]

    def test_update(self):
        def on_start(result, c1, counter):
            assert c1._component_timer
            assert c1._component_timer.running
            assert c1.counter != counter
            return component.stop()

        c1 = ComponentTesterUpdate('test_update_c1')
        cnt = int(c1.counter)
        d = component.start(['test_update_c1'])

        d.addCallback(on_start, c1, cnt)
        return d

    def test_pause(self):
        def on_pause(result, c1, counter):
            assert c1._component_state == 'Paused'
            assert c1.counter != counter
            assert not c1._component_timer.running

        def on_start(result, c1, counter):
            assert c1._component_timer
            assert c1.counter != counter
            d = component.pause(['test_pause_c1'])
            d.addCallback(on_pause, c1, counter)
            return d

        c1 = ComponentTesterUpdate('test_pause_c1')
        cnt = int(c1.counter)
        d = component.start(['test_pause_c1'])

        d.addCallback(on_start, c1, cnt)
        return d

    @pytest_twisted.inlineCallbacks
    def test_component_start_error(self):
        ComponentTesterUpdate('test_pause_c1')
        yield component.start(['test_pause_c1'])
        yield component.pause(['test_pause_c1'])
        test_comp = component.get('test_pause_c1')
        with pytest.raises(component.ComponentException, match='Current state: Paused'):
            yield test_comp._component_start()

    @pytest_twisted.inlineCallbacks
    def test_start_paused_error(self):
        ComponentTesterUpdate('test_pause_c1')
        yield component.start(['test_pause_c1'])
        yield component.pause(['test_pause_c1'])

        # Deferreds that fail in component have to error handler which results in
        # twisted doing a log.err call which causes the test to fail.
        # Prevent failure by ignoring the exception
        # self._observer._ignoreErrors(component.ComponentException)

        result = yield component.start()
        assert [(result[0][0], result[0][1].value)] == [
            (
                defer.FAILURE,
                component.ComponentException(
                    'Trying to start component "%s" but it is '
                    'not in a stopped state. Current state: %s'
                    % ('test_pause_c1', 'Paused'),
                    '',
                ),
            )
        ]

    def test_shutdown(self):
        def on_shutdown(result, c1):
            assert c1.shutdowned
            assert c1._component_state == 'Stopped'
            assert c1.stop_count == 1

        def on_start(result, c1):
            d = component.shutdown()
            d.addCallback(on_shutdown, c1)
            return d

        c1 = ComponentTesterShutdown('test_shutdown_c1')
        d = component.start(['test_shutdown_c1'])
        d.addCallback(on_start, c1)
        return d