126
126
# not even an event
127
127
self.assertRaises(TypeError, self.eq.push)
130
self.assertRaises(event_queue.InvalidEventError,
131
self.eq.push, "no-such-event")
133
# not enough or incorrect args for that event
134
self.assertRaises(TypeError, self.eq.push, "FS_FILE_MOVE")
135
self.assertRaises(TypeError, self.eq.push, "FS_FILE_MOVE", 1, 2, 3)
136
self.assertRaises(TypeError,
137
self.eq.push, "FS_FILE_MOVE", 1, 2, path_to=3)
138
self.assertRaises(TypeError,
139
self.eq.push, "FS_FILE_MOVE", 1, path_from=2, path_to=3)
141
# all ok... args, kwargs, and mixed
142
self.eq.push("FS_FILE_MOVE", 1, 2)
129
# incorrect args, only kwargs supported
130
self.assertRaises(TypeError, self.eq.push, "FS_FILE_MOVE", 1)
131
self.assertRaises(TypeError,
132
self.eq.push, "FS_FILE_MOVE", 1, path_to=2)
143
135
self.eq.push("FS_FILE_MOVE", path_from=1, path_to=2)
144
self.eq.push("FS_FILE_MOVE", 1, path_to=2)
146
137
def test_events_kwargs(self):
147
138
"""Test that all events are defined correctly with tuples or lists.
160
151
class Create(object):
161
152
def __init__(self):
163
def handle_FS_FILE_CREATE(self, a):
154
def handle_FS_FILE_CREATE(self, path):
168
159
self.eq.subscribe(c)
169
self.eq.push("FS_FILE_CREATE", 1)
160
self.eq.push("FS_FILE_CREATE", path=1)
170
161
self.assertEqual(c.a, 1)
171
162
self.eq.unsubscribe(c)
173
164
# don't get what don't listen
175
166
self.eq.subscribe(c)
176
self.eq.push("FS_FILE_DELETE", 1)
167
self.eq.push("FS_FILE_DELETE", path=1)
177
168
self.assertEqual(c.a, None)
178
169
self.eq.unsubscribe(c)
183
174
# helper class, pylint: disable-msg=C0111
184
175
class Create(object):
185
def handle_FS_FILE_CREATE(self, a, b): # it should be only 'a' here
176
def handle_FS_FILE_CREATE(self, notpath): # it should be path here
190
181
self.eq.subscribe(c)
193
self.assertRaises(TypeError, self.eq.push, "FS_FILE_CREATE")
194
self.assertRaises(TypeError, self.eq.push, "FS_FILE_CREATE", 1, 2)
196
# approved by EQ, but the listener has a wrong signature
183
# the listener has a wrong signature
197
184
# this is logged as an error/exception
198
self.eq.push("FS_FILE_CREATE", 1)
185
self.eq.push("FS_FILE_CREATE", path=1)
199
186
self.assertTrue(self.log_handler.check_error('FS_FILE_CREATE',
200
187
'Create object'))
204
191
def test_log_pushing_data(self):
205
192
"""Pushed event and info should be logged."""
206
self.eq.push("AQ_QUERY_ERROR", 'item', error='err')
193
self.eq.push("AQ_QUERY_ERROR", item='item', error='err')
207
194
self.assertTrue(self.log_handler.check_debug(
208
"push_event: AQ_QUERY_ERROR, args:('item',), kw:{'error': 'err'}"))
195
"push_event: AQ_QUERY_ERROR, kwargs: "
196
"{'item': 'item', 'error': 'err'}"))
210
198
def test_log_delete_in_info(self):
211
199
"""Pushed any deletion event should be logged in info."""
212
self.eq.push("FS_DIR_DELETE", 'path')
200
self.eq.push("FS_DIR_DELETE", path='path')
213
201
self.assertTrue(self.log_handler.check_info(
214
202
"push_event: FS_DIR_DELETE"))
217
205
"""SYS_USER_CONNECT event info must not be logged."""
218
206
self.eq.push("SYS_USER_CONNECT", access_token='foo')
219
207
self.assertTrue(self.log_handler.check_debug(
220
"push_event: SYS_USER_CONNECT, args:*, kw:*"))
208
"push_event: SYS_USER_CONNECT, kwargs: *"))
223
211
class PushTestsWithCallback(BaseEQTestCase):
230
218
d = defer.Deferred()
231
219
# helper class, pylint: disable-msg=C0111
232
220
class BadListener(object):
233
def handle_FS_FILE_CREATE(self, a, b): # it should be only 'a' here
221
def handle_FS_FILE_CREATE(self, notpath): # it should be path here
234
222
d.callback(False)
236
224
class GoodListener(object):
237
def handle_FS_FILE_CREATE(self, a):
225
def handle_FS_FILE_CREATE(self, path):
240
228
bl = BadListener()
241
229
gl = GoodListener()
248
236
self.eq.unsubscribe(gl)
249
237
self.addCleanup(cleanup)
252
self.assertRaises(TypeError, self.eq.push, "FS_FILE_CREATE")
253
self.assertRaises(TypeError, self.eq.push, "FS_FILE_CREATE", 1, 2)
255
# approved by EQ, but one listener has a wrong signature
256
self.eq.push("FS_FILE_CREATE", 1)
239
# one listener has a wrong signature
240
self.eq.push("FS_FILE_CREATE", path=1)
257
241
def callback(result):
258
242
""" asserts that GoodListener was called. """
259
243
self.assertTrue(result)
267
251
d = defer.Deferred()
268
252
# helper class, pylint: disable-msg=C0111
269
253
class Listener(object):
270
def handle_default(self, *args, **kwargs):
254
def handle_default(self, event, **kwargs):
255
d.callback((event, kwargs))
274
258
self.eq.subscribe(l)
279
263
self.addCleanup(cleanup)
281
265
# push some event and expect it'll be handled by handle_default
282
self.eq.push("FS_FILE_CREATE", 1)
266
self.eq.push("FS_FILE_CREATE", path=1)
283
267
def callback(result):
284
268
""" asserts that GoodListener was called. """
285
269
self.assertEquals(2, len(result))
286
270
self.assertEquals('FS_FILE_CREATE', result[0])
287
self.assertEquals(1, result[1])
271
self.assertEquals({'path': 1}, result[1])
289
273
d.addCallback(callback)
301
def handle_FS_FILE_CREATE(self, a):
285
def handle_FS_FILE_CREATE(self, path):
302
286
self.events.append('FS_FILE_CREATE')
303
self.eq.push('FS_FILE_MOVE', a, 2)
287
self.eq.push('FS_FILE_MOVE', path_from=path, path_to=2)
305
def handle_FS_FILE_DELETE(self, a):
289
def handle_FS_FILE_DELETE(self, path):
306
290
self.events.append('FS_FILE_DELETE')
308
def handle_FS_FILE_MOVE(self, *args):
292
def handle_FS_FILE_MOVE(self, path_from, path_to):
309
293
self.events.append('FS_FILE_MOVE')
324
308
self.addCleanup(cleanup)
326
310
# push some events to unleash the event madness
327
self.eq.push("FS_FILE_CREATE", 1)
328
self.eq.push('FS_FILE_DELETE', 2)
311
self.eq.push("FS_FILE_CREATE", path=1)
312
self.eq.push('FS_FILE_DELETE', path=2)
330
314
def callback(result):
331
315
""" asserts that Listener was called in the right order"""