-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathtest_client_queue_integration.py
More file actions
217 lines (162 loc) · 6.79 KB
/
test_client_queue_integration.py
File metadata and controls
217 lines (162 loc) · 6.79 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
"""Integration tests for OverkizClient with ActionQueue."""
import asyncio
from unittest.mock import AsyncMock, patch
import pytest
from pyoverkiz.action_queue import ActionQueueSettings
from pyoverkiz.auth import UsernamePasswordCredentials
from pyoverkiz.client import OverkizClient
from pyoverkiz.client_settings import OverkizClientSettings
from pyoverkiz.enums import OverkizCommand, Server
from pyoverkiz.models import Action, Command
@pytest.mark.asyncio
async def test_client_without_queue_executes_immediately():
"""Test that client without queue executes actions immediately."""
client = OverkizClient(
server=Server.SOMFY_EUROPE,
credentials=UsernamePasswordCredentials("test@example.com", "test"),
)
action = Action(
device_url="io://1234-5678-9012/1",
commands=[Command(name=OverkizCommand.CLOSE)],
)
# Mock the internal execution
with patch.object(client, "_post", new_callable=AsyncMock) as mock_post:
mock_post.return_value = {"execId": "exec-123"}
result = await client.execute_action_group([action])
# Should return exec_id directly (string)
assert isinstance(result, str)
assert result == "exec-123"
# Should have called API immediately
mock_post.assert_called_once()
await client.close()
@pytest.mark.asyncio
async def test_client_with_queue_batches_actions():
"""Test that client with queue batches multiple actions."""
client = OverkizClient(
server=Server.SOMFY_EUROPE,
credentials=UsernamePasswordCredentials("test@example.com", "test"),
settings=OverkizClientSettings(action_queue=ActionQueueSettings(delay=0.1)),
)
actions = [
Action(
device_url=f"io://1234-5678-9012/{i}",
commands=[Command(name=OverkizCommand.CLOSE)],
)
for i in range(3)
]
with patch.object(client, "_post", new_callable=AsyncMock) as mock_post:
mock_post.return_value = {"execId": "exec-batched"}
# Queue multiple actions quickly - start them as tasks to allow batching
task1 = asyncio.create_task(client.execute_action_group([actions[0]]))
task2 = asyncio.create_task(client.execute_action_group([actions[1]]))
task3 = asyncio.create_task(client.execute_action_group([actions[2]]))
# Give them a moment to queue
await asyncio.sleep(0.01)
# Should have 3 actions pending
assert client.get_pending_actions_count() == 3
# Wait for all to execute
exec_id1 = await task1
exec_id2 = await task2
exec_id3 = await task3
# All should have the same exec_id (batched together)
assert exec_id1 == exec_id2 == exec_id3 == "exec-batched"
# Should have called API only once (batched)
mock_post.assert_called_once()
# Check that all 3 actions were in the batch
call_args = mock_post.call_args
payload = call_args[0][1] # Second argument is the payload
assert len(payload["actions"]) == 3
await client.close()
@pytest.mark.asyncio
async def test_client_manual_flush():
"""Test manually flushing the queue."""
client = OverkizClient(
server=Server.SOMFY_EUROPE,
credentials=UsernamePasswordCredentials("test@example.com", "test"),
settings=OverkizClientSettings(
action_queue=ActionQueueSettings(delay=10.0)
), # Long delay
)
action = Action(
device_url="io://1234-5678-9012/1",
commands=[Command(name=OverkizCommand.CLOSE)],
)
with patch.object(client, "_post", new_callable=AsyncMock) as mock_post:
mock_post.return_value = {"execId": "exec-flushed"}
# Start execution as a task to allow checking pending count
exec_task = asyncio.create_task(client.execute_action_group([action]))
# Give it a moment to queue
await asyncio.sleep(0.01)
# Should have 1 action pending
assert client.get_pending_actions_count() == 1
# Manually flush
await client.flush_action_queue()
# Should be executed now
assert client.get_pending_actions_count() == 0
exec_id = await exec_task
assert exec_id == "exec-flushed"
mock_post.assert_called_once()
await client.close()
@pytest.mark.asyncio
async def test_client_close_flushes_queue():
"""Test that closing the client flushes pending actions."""
client = OverkizClient(
server=Server.SOMFY_EUROPE,
credentials=UsernamePasswordCredentials("test@example.com", "test"),
settings=OverkizClientSettings(action_queue=ActionQueueSettings(delay=10.0)),
)
action = Action(
device_url="io://1234-5678-9012/1",
commands=[Command(name=OverkizCommand.CLOSE)],
)
with patch.object(client, "_post", new_callable=AsyncMock) as mock_post:
mock_post.return_value = {"execId": "exec-closed"}
# Start execution as a task
exec_task = asyncio.create_task(client.execute_action_group([action]))
# Give it a moment to queue
await asyncio.sleep(0.01)
# Close should flush
await client.close()
# Should be executed
exec_id = await exec_task
assert exec_id == "exec-closed"
mock_post.assert_called_once()
@pytest.mark.asyncio
async def test_client_queue_respects_max_actions():
"""Test that queue flushes when max actions is reached."""
client = OverkizClient(
server=Server.SOMFY_EUROPE,
credentials=UsernamePasswordCredentials("test@example.com", "test"),
settings=OverkizClientSettings(
action_queue=ActionQueueSettings(
delay=10.0,
max_actions=2, # Max 2 actions
),
),
)
actions = [
Action(
device_url=f"io://1234-5678-9012/{i}",
commands=[Command(name=OverkizCommand.CLOSE)],
)
for i in range(3)
]
with patch.object(client, "_post", new_callable=AsyncMock) as mock_post:
mock_post.return_value = {"execId": "exec-123"}
# Add 2 actions as tasks to trigger flush
task1 = asyncio.create_task(client.execute_action_group([actions[0]]))
task2 = asyncio.create_task(client.execute_action_group([actions[1]]))
# Wait a bit for flush
await asyncio.sleep(0.05)
# First 2 should be done
exec_id1 = await task1
exec_id2 = await task2
assert exec_id1 == "exec-123"
assert exec_id2 == "exec-123"
# Add third action - starts new batch
exec_id3 = await client.execute_action_group([actions[2]])
# Should have exec_id directly (waited for batch to complete)
assert exec_id3 == "exec-123"
# Should have been called twice (2 batches)
assert mock_post.call_count == 2
await client.close()