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
|
# -*- coding: utf-8 -*-
# test_incoming_processing_flow.py
# Copyright (C) 2017 LEAP
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
Unit tests for incoming box processing flow.
"""
from mock import Mock, call
from leap.soledad.client import interfaces
from leap.soledad.client.incoming import IncomingBoxProcessingLoop
from twisted.internet import defer
from twisted.trial import unittest
from zope.interface import implementer
@implementer(interfaces.IIncomingBoxConsumer)
class GoodConsumer(object):
def __init__(self):
self.name = 'GoodConsumer'
self.processed, self.saved = [], []
def process(self, item, item_id, encrypted=True):
self.processed.append(item_id)
return defer.succeed([item_id])
def save(self, parts, item_id):
self.saved.append(item_id)
return defer.succeed(None)
class ProcessingFailedConsumer(GoodConsumer):
def __init__(self):
self.name = 'ProcessingFailedConsumer'
self.processed, self.saved = [], []
def process(self, item, item_id, encrypted=True):
return defer.fail()
class SavingFailedConsumer(GoodConsumer):
def __init__(self):
self.name = 'SavingFailedConsumer'
self.processed, self.saved = [], []
def save(self, parts, item_id):
return defer.fail()
class IncomingBoxProcessingTestCase(unittest.TestCase):
def setUp(self):
self.box = Mock()
self.loop = IncomingBoxProcessingLoop(self.box)
def _set_pending_items(self, pending):
self.box.list_pending.return_value = defer.succeed(pending)
pending_iter = iter([defer.succeed(item) for item in pending])
self.box.fetch_for_processing.side_effect = pending_iter
@defer.inlineCallbacks
def test_processing_flow_reserves_a_message(self):
self._set_pending_items(['one_item'])
self.loop.add_consumer(GoodConsumer())
yield self.loop()
self.box.fetch_for_processing.assert_called_once_with('one_item')
@defer.inlineCallbacks
def test_no_consumers(self):
items = ['one', 'two', 'three']
self._set_pending_items(items)
yield self.loop()
self.box.fetch_for_processing.assert_not_called()
self.box.delete.assert_not_called()
@defer.inlineCallbacks
def test_pending_list_with_multiple_items(self):
items = ['one', 'two', 'three']
self._set_pending_items(items)
consumer = GoodConsumer()
self.loop.add_consumer(consumer)
yield self.loop()
calls = [call('one'), call('two'), call('three')]
self.box.fetch_for_processing.assert_has_calls(calls)
@defer.inlineCallbacks
def test_good_consumer_process_all(self):
items = ['one', 'two', 'three']
self._set_pending_items(items)
consumer = GoodConsumer()
self.loop.add_consumer(consumer)
yield self.loop()
self.assertEquals(items, consumer.processed)
@defer.inlineCallbacks
def test_good_consumer_saves_all(self):
items = ['one', 'two', 'three']
self._set_pending_items(items)
consumer = GoodConsumer()
self.loop.add_consumer(consumer)
yield self.loop()
self.assertEquals(items, consumer.saved)
@defer.inlineCallbacks
def test_multiple_good_consumers_process_all(self):
items = ['one', 'two', 'three']
self._set_pending_items(items)
consumer = GoodConsumer()
consumer2 = GoodConsumer()
self.loop.add_consumer(consumer)
self.loop.add_consumer(consumer2)
yield self.loop()
self.assertEquals(items, consumer.processed)
self.assertEquals(items, consumer2.processed)
@defer.inlineCallbacks
def test_good_consumer_marks_as_processed(self):
items = ['one', 'two', 'three']
self._set_pending_items(items)
consumer = GoodConsumer()
self.loop.add_consumer(consumer)
yield self.loop()
self.box.set_processed.assert_has_calls([call(x) for x in items])
@defer.inlineCallbacks
def test_good_consumer_deletes_items(self):
items = ['one', 'two', 'three']
self._set_pending_items(items)
consumer = GoodConsumer()
self.loop.add_consumer(consumer)
yield self.loop()
self.box.delete.assert_has_calls([call(x) for x in items])
@defer.inlineCallbacks
def test_processing_failed_doesnt_mark_as_processed(self):
items = ['one', 'two', 'three']
self._set_pending_items(items)
consumer = ProcessingFailedConsumer()
self.loop.add_consumer(consumer)
yield self.loop()
self.box.set_processed.assert_not_called()
@defer.inlineCallbacks
def test_processing_failed_doesnt_delete(self):
items = ['one', 'two', 'three']
self._set_pending_items(items)
consumer = ProcessingFailedConsumer()
self.loop.add_consumer(consumer)
yield self.loop()
self.box.delete.assert_not_called()
@defer.inlineCallbacks
def test_processing_failed_marks_as_failed(self):
items = ['one', 'two', 'three']
self._set_pending_items(items)
consumer = ProcessingFailedConsumer()
self.loop.add_consumer(consumer)
yield self.loop()
self.box.set_failed.assert_has_calls([call(x) for x in items])
@defer.inlineCallbacks
def test_saving_failed_marks_as_processed(self):
items = ['one', 'two', 'three']
self._set_pending_items(items)
consumer = SavingFailedConsumer()
self.loop.add_consumer(consumer)
yield self.loop()
self.box.set_processed.assert_has_calls([call(x) for x in items])
@defer.inlineCallbacks
def test_saving_failed_doesnt_delete(self):
items = ['one', 'two', 'three']
self._set_pending_items(items)
consumer = SavingFailedConsumer()
self.loop.add_consumer(consumer)
yield self.loop()
self.box.delete.assert_not_called()
@defer.inlineCallbacks
def test_saving_failed_marks_as_failed(self):
items = ['one', 'two', 'three']
self._set_pending_items(items)
consumer = SavingFailedConsumer()
self.loop.add_consumer(consumer)
yield self.loop()
self.box.set_failed.assert_has_calls([call(x) for x in items])
|