-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathtest_action_queue.py
More file actions
451 lines (330 loc) · 12.9 KB
/
test_action_queue.py
File metadata and controls
451 lines (330 loc) · 12.9 KB
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
"""Tests for ActionQueue."""
import asyncio
from unittest.mock import AsyncMock
import pytest
from pyoverkiz.action_queue import ActionQueue, ActionQueueSettings, QueuedExecution
from pyoverkiz.enums import ExecutionMode, OverkizCommand
from pyoverkiz.models import Action, Command
@pytest.fixture
def mock_executor():
"""Create a mock executor function."""
async def executor(actions, mode, label):
# Return immediately, no delay
return f"exec-{len(actions)}-{mode}-{label}"
return AsyncMock(side_effect=executor)
@pytest.mark.asyncio
async def test_action_queue_single_action(mock_executor):
"""Test queue with a single action."""
queue = ActionQueue(executor=mock_executor, settings=ActionQueueSettings(delay=0.1))
action = Action(
device_url="io://1234-5678-9012/1",
commands=[Command(name=OverkizCommand.CLOSE)],
)
queued = await queue.add([action])
assert isinstance(queued, QueuedExecution)
# Wait for the batch to execute
exec_id = await queued
assert exec_id.startswith("exec-1-")
# Verify executor was called
mock_executor.assert_called_once()
@pytest.mark.asyncio
async def test_action_queue_batching(mock_executor):
"""Test that multiple actions are batched together."""
queue = ActionQueue(executor=mock_executor, settings=ActionQueueSettings(delay=0.2))
actions = [
Action(
device_url=f"io://1234-5678-9012/{i}",
commands=[Command(name=OverkizCommand.CLOSE)],
)
for i in range(3)
]
# Add actions in quick succession
queued1 = await queue.add([actions[0]])
queued2 = await queue.add([actions[1]])
queued3 = await queue.add([actions[2]])
# All should return the same exec_id
exec_id1 = await queued1
exec_id2 = await queued2
exec_id3 = await queued3
assert exec_id1 == exec_id2 == exec_id3
assert "exec-3-" in exec_id1 # 3 actions in batch
# Executor should be called only once
mock_executor.assert_called_once()
@pytest.mark.asyncio
async def test_action_queue_max_actions_flush(mock_executor):
"""Test that queue flushes when max actions is reached."""
queue = ActionQueue(
executor=mock_executor, settings=ActionQueueSettings(delay=10.0, max_actions=3)
)
actions = [
Action(
device_url=f"io://1234-5678-9012/{i}",
commands=[Command(name=OverkizCommand.CLOSE)],
)
for i in range(5)
]
# Add 3 actions - should trigger flush
queued1 = await queue.add([actions[0]])
queued2 = await queue.add([actions[1]])
queued3 = await queue.add([actions[2]])
# Wait a bit for flush to complete
await asyncio.sleep(0.05)
# First 3 should be done
assert queued1.is_done()
assert queued2.is_done()
assert queued3.is_done()
# Add 2 more - should start a new batch
queued4 = await queue.add([actions[3]])
queued5 = await queue.add([actions[4]])
# Wait for second batch
await queued4
await queued5
# Should have been called twice (2 batches)
assert mock_executor.call_count == 2
@pytest.mark.asyncio
async def test_action_queue_mode_change_flush(mock_executor):
"""Test that queue flushes when command mode changes."""
queue = ActionQueue(executor=mock_executor, settings=ActionQueueSettings(delay=0.5))
action = Action(
device_url="io://1234-5678-9012/1",
commands=[Command(name=OverkizCommand.CLOSE)],
)
# Add action with normal mode
queued1 = await queue.add([action], mode=None)
# Add action with high priority - should flush previous batch
queued2 = await queue.add([action], mode=ExecutionMode.HIGH_PRIORITY)
# Wait for both batches
exec_id1 = await queued1
exec_id2 = await queued2
# Should be different exec_ids (different batches)
assert exec_id1 != exec_id2
# Should have been called twice
assert mock_executor.call_count == 2
@pytest.mark.asyncio
async def test_action_queue_label_change_flush(mock_executor):
"""Test that queue flushes when label changes."""
queue = ActionQueue(executor=mock_executor, settings=ActionQueueSettings(delay=0.5))
action = Action(
device_url="io://1234-5678-9012/1",
commands=[Command(name=OverkizCommand.CLOSE)],
)
# Add action with label1
queued1 = await queue.add([action], label="label1")
# Add action with label2 - should flush previous batch
queued2 = await queue.add([action], label="label2")
# Wait for both batches
exec_id1 = await queued1
exec_id2 = await queued2
# Should be different exec_ids (different batches)
assert exec_id1 != exec_id2
# Should have been called twice
assert mock_executor.call_count == 2
@pytest.mark.asyncio
async def test_action_queue_duplicate_device_merge(mock_executor):
"""Test that queue merges commands for duplicate devices."""
queue = ActionQueue(executor=mock_executor, settings=ActionQueueSettings(delay=0.5))
action1 = Action(
device_url="io://1234-5678-9012/1",
commands=[Command(name=OverkizCommand.CLOSE)],
)
action2 = Action(
device_url="io://1234-5678-9012/1",
commands=[Command(name=OverkizCommand.OPEN)],
)
queued1 = await queue.add([action1])
queued2 = await queue.add([action2])
exec_id1 = await queued1
exec_id2 = await queued2
assert exec_id1 == exec_id2
mock_executor.assert_called_once()
@pytest.mark.asyncio
async def test_action_queue_duplicate_device_merge_order(mock_executor):
"""Test that command order is preserved when merging."""
queue = ActionQueue(executor=mock_executor, settings=ActionQueueSettings(delay=0.1))
action1 = Action(
device_url="io://1234-5678-9012/1",
commands=[Command(name=OverkizCommand.CLOSE)],
)
action2 = Action(
device_url="io://1234-5678-9012/1",
commands=[Command(name=OverkizCommand.OPEN)],
)
queued = await queue.add([action1, action2])
await queued
args, _ = mock_executor.call_args
actions = args[0]
assert len(actions) == 1
assert [command.name for command in actions[0].commands] == [
OverkizCommand.CLOSE,
OverkizCommand.OPEN,
]
@pytest.mark.asyncio
async def test_action_queue_duplicate_device_merge_does_not_mutate_inputs(
mock_executor,
):
"""Test that merge behavior does not mutate caller-owned Action commands."""
queue = ActionQueue(executor=mock_executor, settings=ActionQueueSettings(delay=0.1))
action1 = Action(
device_url="io://1234-5678-9012/1",
commands=[Command(name=OverkizCommand.CLOSE)],
)
action2 = Action(
device_url="io://1234-5678-9012/1",
commands=[Command(name=OverkizCommand.OPEN)],
)
queued = await queue.add([action1, action2])
await queued
assert [command.name for command in action1.commands] == [OverkizCommand.CLOSE]
assert [command.name for command in action2.commands] == [OverkizCommand.OPEN]
@pytest.mark.asyncio
async def test_action_queue_manual_flush(mock_executor):
"""Test manual flush of the queue."""
queue = ActionQueue(
executor=mock_executor, settings=ActionQueueSettings(delay=10.0)
) # Long delay
action = Action(
device_url="io://1234-5678-9012/1",
commands=[Command(name=OverkizCommand.CLOSE)],
)
queued = await queue.add([action])
# Manually flush
await queue.flush()
# Should be done now
assert queued.is_done()
exec_id = await queued
assert exec_id.startswith("exec-1-")
@pytest.mark.asyncio
async def test_action_queue_shutdown(mock_executor):
"""Test that shutdown flushes pending actions."""
queue = ActionQueue(
executor=mock_executor, settings=ActionQueueSettings(delay=10.0)
)
action = Action(
device_url="io://1234-5678-9012/1",
commands=[Command(name=OverkizCommand.CLOSE)],
)
queued = await queue.add([action])
# Shutdown should flush
await queue.shutdown()
# Should be done
assert queued.is_done()
mock_executor.assert_called_once()
@pytest.mark.asyncio
async def test_action_queue_error_propagation(mock_executor):
"""Test that exceptions are propagated to all waiters."""
# Make executor raise an exception
mock_executor.side_effect = ValueError("API Error")
queue = ActionQueue(executor=mock_executor, settings=ActionQueueSettings(delay=0.1))
action = Action(
device_url="io://1234-5678-9012/1",
commands=[Command(name=OverkizCommand.CLOSE)],
)
queued1 = await queue.add([action])
queued2 = await queue.add([action])
# Both should raise the same exception
with pytest.raises(ValueError, match="API Error"):
await queued1
with pytest.raises(ValueError, match="API Error"):
await queued2
@pytest.mark.asyncio
async def test_action_queue_get_pending_count():
"""Test getting pending action count."""
mock_executor = AsyncMock(return_value="exec-123")
queue = ActionQueue(executor=mock_executor, settings=ActionQueueSettings(delay=0.5))
assert queue.get_pending_count() == 0
action = Action(
device_url="io://1234-5678-9012/1",
commands=[Command(name=OverkizCommand.CLOSE)],
)
await queue.add([action])
assert queue.get_pending_count() == 1
await queue.add([action])
assert queue.get_pending_count() == 1
# Wait for flush
await asyncio.sleep(0.6)
assert queue.get_pending_count() == 0
@pytest.mark.asyncio
async def test_queued_execution_awaitable():
"""Test that QueuedExecution is properly awaitable."""
queued = QueuedExecution()
# Set result in background
async def set_result():
await asyncio.sleep(0.05)
queued.set_result("exec-123")
task = asyncio.create_task(set_result())
# Await the result
result = await queued
assert result == "exec-123"
# Ensure background task has completed
await task
@pytest.mark.asyncio
async def test_action_queue_settings_validate():
"""Test that validate raises on invalid settings."""
with pytest.raises(ValueError, match="positive"):
ActionQueueSettings(delay=-1).validate()
with pytest.raises(ValueError, match="at least 1"):
ActionQueueSettings(max_actions=0).validate()
# Valid settings should not raise
ActionQueueSettings(delay=0.5, max_actions=10).validate()
@pytest.mark.asyncio
async def test_action_queue_add_empty_actions(mock_executor):
"""Test that add raises ValueError for empty action list."""
queue = ActionQueue(executor=mock_executor, settings=ActionQueueSettings(delay=0.1))
with pytest.raises(ValueError, match="at least one Action"):
await queue.add([])
@pytest.mark.asyncio
async def test_action_queue_executor_cancelled_propagates():
"""Test that CancelledError during execution propagates to waiters."""
async def cancelling_executor(actions, mode, label):
raise asyncio.CancelledError
queue = ActionQueue(
executor=AsyncMock(side_effect=cancelling_executor),
settings=ActionQueueSettings(delay=0.05),
)
action = Action(
device_url="io://1234-5678-9012/1",
commands=[Command(name=OverkizCommand.CLOSE)],
)
queued = await queue.add([action])
with pytest.raises(asyncio.CancelledError):
await queued
@pytest.mark.asyncio
async def test_action_queue_flush_empty(mock_executor):
"""Test that flushing an empty queue is a no-op."""
queue = ActionQueue(executor=mock_executor, settings=ActionQueueSettings(delay=0.1))
await queue.flush()
mock_executor.assert_not_called()
@pytest.mark.asyncio
async def test_action_queue_shutdown_empty(mock_executor):
"""Test that shutting down an empty queue is a no-op."""
queue = ActionQueue(executor=mock_executor, settings=ActionQueueSettings(delay=0.1))
await queue.shutdown()
mock_executor.assert_not_called()
@pytest.mark.asyncio
async def test_action_queue_no_self_cancel_during_delayed_flush():
"""Test that _delayed_flush does not cancel itself via _prepare_flush.
When _delayed_flush fires and calls _prepare_flush, the flush task is still
the running coroutine. _prepare_flush must not cancel it, otherwise the batch
would fail with CancelledError when the executor performs I/O.
"""
cancel_detected = False
async def slow_executor(actions, mode, label):
nonlocal cancel_detected
try:
await asyncio.sleep(0.05)
except asyncio.CancelledError:
cancel_detected = True
raise
return "exec-ok"
queue = ActionQueue(
executor=AsyncMock(side_effect=slow_executor),
settings=ActionQueueSettings(delay=0.05),
)
action = Action(
device_url="io://1234-5678-9012/1",
commands=[Command(name=OverkizCommand.CLOSE)],
)
queued = await queue.add([action])
exec_id = await queued
assert exec_id == "exec-ok"
assert not cancel_detected, "_delayed_flush cancelled itself via _prepare_flush"