-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_ipython_live_updates.py
More file actions
652 lines (530 loc) · 23.4 KB
/
Copy pathtest_ipython_live_updates.py
File metadata and controls
652 lines (530 loc) · 23.4 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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
from unittest import mock
import pytest
from bec_ipython_client.callbacks.ipython_live_updates import IPythonLiveUpdates
from bec_lib import messages
from bec_lib.bec_errors import ScanInterruption, ScanRestart
from bec_lib.queue_items import QueueItem
@pytest.fixture
def ipython_live_updates_with_mocked_live(bec_client_mock):
"""Create IPythonLiveUpdates instance with mocked Live display."""
with mock.patch("bec_ipython_client.callbacks.ipython_live_updates.Live") as mock_live:
mock_instance = mock.MagicMock()
mock_live.return_value = mock_instance
live_updates = IPythonLiveUpdates(bec_client_mock)
yield live_updates, mock_live
@pytest.fixture
def queue_elements(bec_client_mock):
client = bec_client_mock
request_msg = messages.ScanQueueMessage(
scan_type="grid_scan",
parameter={"args": {"samx": [-5, 5, 3]}, "kwargs": {}},
queue="primary",
metadata={"RID": "something"},
)
request_block = messages.RequestBlock(
msg=request_msg,
RID="req_id",
scan_motors=["samx"],
report_instructions=[],
readout_priority={"monitored": ["samx"]},
is_scan=True,
scan_number=1,
scan_id="scan_id",
)
queue = QueueItem(
scan_manager=client.queue,
queue_id="queue_id",
request_blocks=[request_block],
status="PENDING",
active_request_block={},
scan_id=["scan_id"],
)
return queue, request_block, request_msg
@pytest.fixture
def sample_request_msg():
return messages.ScanQueueMessage(
scan_type="grid_scan",
parameter={"args": {"samx": [-5, 5, 3]}, "kwargs": {}},
queue="primary",
metadata={"RID": "something"},
)
@pytest.fixture
def sample_request_block(sample_request_msg):
return messages.RequestBlock(
msg=sample_request_msg,
RID="req_id",
scan_motors=["samx"],
report_instructions=[],
readout_priority={"monitored": ["samx"]},
is_scan=True,
scan_number=1,
scan_id="scan_id",
)
@pytest.fixture
def sample_queue_info_entry(sample_request_block):
return messages.QueueInfoEntry(
queue_id="test_queue_id",
scan_id=["scan_id"],
is_scan=[True],
request_blocks=[sample_request_block],
scan_number=[1],
status="RUNNING",
active_request_block=None,
)
@pytest.fixture
def sample_scan_queue_status(sample_queue_info_entry):
return messages.ScanQueueStatus(info=[sample_queue_info_entry], status="RUNNING")
@pytest.mark.timeout(20)
def test_live_updates_process_queue_pending(ipython_live_updates_with_mocked_live, queue_elements):
live_updates, mock_live = ipython_live_updates_with_mocked_live
client = live_updates.client
queue, request_block, request_msg = queue_elements
client.queue.queue_storage.current_scan_queue = {
"primary": messages.ScanQueueStatus(info=[], status="RUNNING")
}
with mock.patch.object(queue, "_update_with_buffer"):
with mock.patch(
"bec_lib.queue_items.QueueItem.queue_position", new_callable=mock.PropertyMock
) as queue_pos:
queue_pos.return_value = 2
with mock.patch.object(
live_updates, "_available_req_blocks", return_value=[request_block]
):
with mock.patch.object(live_updates, "_process_report_instructions") as process:
res = live_updates._process_queue(queue, request_msg, "req_id")
# Verify Live panel was created for showing queue status
mock_live.assert_called_once()
mock_live.return_value.start.assert_called_once()
process.assert_not_called()
assert res is False
@pytest.mark.timeout(20)
def test_live_updates_process_queue_running(ipython_live_updates_with_mocked_live, queue_elements):
live_updates, mock_live = ipython_live_updates_with_mocked_live
client = live_updates.client
queue, request_block, request_msg = queue_elements
queue = QueueItem(
scan_manager=client.queue,
queue_id="queue_id",
request_blocks=[request_block],
status="RUNNING",
active_request_block=None,
scan_id=["scan_id"],
)
live_updates._active_request = request_msg
request_block.report_instructions = [{"wait_table": 10}]
client.queue.queue_storage.current_scan_queue = {
"primary": messages.ScanQueueStatus(info=[], status="RUNNING")
}
with mock.patch.object(queue, "_update_with_buffer"):
with mock.patch(
"bec_lib.queue_items.QueueItem.queue_position", new_callable=mock.PropertyMock
):
with mock.patch.object(
live_updates, "_available_req_blocks", return_value=[request_block]
):
with mock.patch.object(live_updates, "_process_instruction") as process:
res = live_updates._process_queue(queue, request_msg, "req_id")
mock_live.assert_not_called()
process.assert_called_once_with({"wait_table": 10})
assert res is True
def test_process_request_repeats_on_ScanRestart_error(
ipython_live_updates_with_mocked_live, queue_elements
):
"""
Test that process_request handles ScanRestart by repeating the processing.
During restarts, we expect _stop_status_live to be called to clean up any existing live displays.
"""
live_updates, _ = ipython_live_updates_with_mocked_live
client = live_updates.client
_, _, request_msg = queue_elements
client.queue.queue_storage.current_scan_queue = {
"primary": messages.ScanQueueStatus(info=[], status="RUNNING")
}
callbacks = mock.MagicMock()
live_updates.client._sighandler = mock.MagicMock()
live_updates.client._sighandler.__enter__.side_effect = [
ScanRestart(request_msg),
ScanInterruption(),
]
with mock.patch.object(live_updates, "_stop_status_live"):
with pytest.raises(ScanInterruption):
live_updates.process_request(request_msg, callbacks)
# once for each _process_queue call (2 times due to ScanRestart), once per exception and once at the end
assert live_updates._stop_status_live.call_count == 5
@pytest.mark.timeout(20)
def test_live_updates_process_queue_without_status(bec_client_mock, queue_elements):
client = bec_client_mock
live_updates = IPythonLiveUpdates(client)
queue, _, request_msg = queue_elements
with mock.patch.object(queue, "_update_with_buffer"):
assert live_updates._process_queue(queue, request_msg, "req_id") is False
@pytest.mark.timeout(20)
def test_live_updates_process_queue_without_queue_number(bec_client_mock, queue_elements):
client = bec_client_mock
live_updates = IPythonLiveUpdates(client)
queue, _, request_msg = queue_elements
with mock.patch(
"bec_lib.queue_items.QueueItem.queue_position", new_callable=mock.PropertyMock
) as queue_pos:
queue = QueueItem(
scan_manager=client.queue,
queue_id="queue_id",
request_blocks=[request_msg],
status="PENDING",
active_request_block={},
scan_id=["scan_id"],
)
queue_pos.return_value = None
with mock.patch.object(queue, "_update_with_buffer"):
assert live_updates._process_queue(queue, request_msg, "req_id") is False
@pytest.mark.timeout(20)
def test_available_req_blocks(bec_client_mock, queue_elements):
client = bec_client_mock
live_updates = IPythonLiveUpdates(client)
queue, request_block, request_msg = queue_elements
# Test with matching RID
available_blocks = live_updates._available_req_blocks(queue, request_msg)
assert (
len(available_blocks) == 0
) # request_block.RID is "req_id", request_msg.metadata["RID"] is "something"
# Test with correct RID
request_block.RID = "something"
available_blocks = live_updates._available_req_blocks(queue, request_msg)
assert len(available_blocks) == 1
assert available_blocks[0] == request_block
@pytest.mark.timeout(20)
def test_available_req_blocks_multiple_blocks(bec_client_mock):
client = bec_client_mock
live_updates = IPythonLiveUpdates(client)
request_msg = messages.ScanQueueMessage(
scan_type="grid_scan",
parameter={"args": {"samx": [-5, 5, 3]}, "kwargs": {}},
queue="primary",
metadata={"RID": "test_rid"},
)
request_block1 = messages.RequestBlock(
msg=request_msg,
RID="test_rid",
scan_motors=["samx"],
report_instructions=[],
readout_priority={"monitored": ["samx"]},
is_scan=True,
scan_number=1,
scan_id="scan_id_1",
)
request_block2 = messages.RequestBlock(
msg=request_msg,
RID="test_rid",
scan_motors=["samy"],
report_instructions=[],
readout_priority={"monitored": ["samy"]},
is_scan=True,
scan_number=2,
scan_id="scan_id_2",
)
request_block3 = messages.RequestBlock(
msg=request_msg,
RID="different_rid",
scan_motors=["samz"],
report_instructions=[],
readout_priority={"monitored": ["samz"]},
is_scan=True,
scan_number=3,
scan_id="scan_id_3",
)
queue = QueueItem(
scan_manager=client.queue,
queue_id="queue_id",
request_blocks=[request_block1, request_block2, request_block3],
status="RUNNING",
active_request_block={},
scan_id=["scan_id_1", "scan_id_2", "scan_id_3"],
)
available_blocks = live_updates._available_req_blocks(queue, request_msg)
assert len(available_blocks) == 2
assert request_block1 in available_blocks
assert request_block2 in available_blocks
assert request_block3 not in available_blocks
@pytest.mark.timeout(20)
def test_element_in_queue_no_queue(bec_client_mock):
client = bec_client_mock
live_updates = IPythonLiveUpdates(client)
# Test when client.queue is None
client.queue = None
assert live_updates._element_in_queue() is False
@pytest.mark.timeout(20)
def test_element_in_queue_no_current_scan_queue(bec_client_mock):
client = bec_client_mock
live_updates = IPythonLiveUpdates(client)
# Test when current_scan_queue is None
client.queue.queue_storage.current_scan_queue = None
assert live_updates._element_in_queue() is False
@pytest.mark.timeout(20)
def test_element_in_queue_no_primary_queue(bec_client_mock):
client = bec_client_mock
live_updates = IPythonLiveUpdates(client)
# Test when primary queue doesn't exist
scan_queue_status = messages.ScanQueueStatus(info=[], status="RUNNING")
client.queue.queue_storage.current_scan_queue = {"secondary": scan_queue_status}
assert live_updates._element_in_queue() is False
@pytest.mark.timeout(20)
def test_element_in_queue_no_queue_info(bec_client_mock):
client = bec_client_mock
live_updates = IPythonLiveUpdates(client)
# Test when queue_info is empty
scan_queue_status = messages.ScanQueueStatus(info=[], status="RUNNING")
client.queue.queue_storage.current_scan_queue = {"primary": scan_queue_status}
assert live_updates._element_in_queue() is False
@pytest.mark.timeout(20)
def test_element_in_queue_no_current_queue(bec_client_mock, sample_scan_queue_status):
client = bec_client_mock
live_updates = IPythonLiveUpdates(client)
# Test when _current_queue is None
live_updates._current_queue = None
client.queue.queue_storage.current_scan_queue = {"primary": sample_scan_queue_status}
assert live_updates._element_in_queue() is False
@pytest.mark.timeout(20)
def test_element_in_queue_queue_id_not_in_info(bec_client_mock, sample_request_block):
client = bec_client_mock
live_updates = IPythonLiveUpdates(client)
# Test when queue_id is not in info
current_queue = mock.MagicMock()
current_queue.queue_id = "my_queue_id"
live_updates._current_queue = current_queue
queue_info_entry = messages.QueueInfoEntry(
queue_id="different_queue_id",
scan_id=["scan_id"],
is_scan=[True],
request_blocks=[sample_request_block],
scan_number=[1],
status="RUNNING",
active_request_block=None,
)
scan_queue_status = messages.ScanQueueStatus(info=[queue_info_entry], status="RUNNING")
client.queue.queue_storage.current_scan_queue = {"primary": scan_queue_status}
assert live_updates._element_in_queue() is False
@pytest.mark.timeout(20)
def test_element_in_queue_queue_id_in_info(bec_client_mock, sample_request_block):
client = bec_client_mock
live_updates = IPythonLiveUpdates(client)
# Test when queue_id is in info (should return True)
current_queue = mock.MagicMock()
current_queue.queue_id = "my_queue_id"
live_updates._current_queue = current_queue
queue_info_entry = messages.QueueInfoEntry(
queue_id="my_queue_id",
scan_id=["scan_id"],
is_scan=[True],
request_blocks=[sample_request_block],
scan_number=[1],
status="RUNNING",
active_request_block=None,
)
scan_queue_status = messages.ScanQueueStatus(info=[queue_info_entry], status="RUNNING")
client.queue.queue_storage.current_scan_queue = {"primary": scan_queue_status}
assert live_updates._element_in_queue() is True
@pytest.mark.timeout(20)
def test_process_pending_queue_element_locked_queue(
ipython_live_updates_with_mocked_live, queue_elements
):
"""Test _process_pending_queue_element when queue is LOCKED."""
live_updates, mock_live = ipython_live_updates_with_mocked_live
client = live_updates.client
queue, _, _ = queue_elements
lock = messages.ScanQueueLock(identifier="user123", reason="Manual hold for calibration")
# Create a locked queue status with locks
scan_queue_status = messages.ScanQueueStatus(info=[], status="LOCKED", locks=[lock])
client.queue.queue_storage.current_scan_queue = {"primary": scan_queue_status}
with mock.patch(
"bec_lib.queue_items.QueueItem.queue_position", new_callable=mock.PropertyMock
) as queue_pos:
queue_pos.return_value = 0
# Call the method
live_updates._process_pending_queue_element(queue)
# Verify Live panel was created with lock info
mock_live.assert_called_once()
call_args = mock_live.call_args[0][0]
# Extract the renderable text from Panel
panel_renderable = call_args.renderable
assert "Scan is waiting for the lock to be released" in panel_renderable
assert "user123" in panel_renderable
assert "Manual hold for calibration" in panel_renderable
# Verify start was called
mock_live.return_value.start.assert_called_once()
@pytest.mark.timeout(20)
def test_process_pending_queue_element_locked_queue_update_existing_live(
ipython_live_updates_with_mocked_live, queue_elements
):
"""Test _process_pending_queue_element updates existing Live when queue is LOCKED."""
live_updates, mock_live = ipython_live_updates_with_mocked_live
client = live_updates.client
queue, _, _ = queue_elements
# Create a lock
lock = messages.ScanQueueLock(identifier="user123", reason="Manual hold for calibration")
# Create a locked queue status
scan_queue_status = messages.ScanQueueStatus(info=[], status="LOCKED", locks=[lock])
client.queue.queue_storage.current_scan_queue = {"primary": scan_queue_status}
with mock.patch(
"bec_lib.queue_items.QueueItem.queue_position", new_callable=mock.PropertyMock
) as queue_pos:
queue_pos.return_value = 0
# Set up existing _status_live
existing_live = mock.MagicMock()
live_updates._status_live = existing_live
# Call the method
live_updates._process_pending_queue_element(queue)
# Verify Live was not created again
mock_live.assert_not_called()
# Verify update was called instead
existing_live.update.assert_called_once()
call_args = existing_live.update.call_args[0][0]
panel_renderable = call_args.renderable
assert "Scan is waiting for the lock to be released" in panel_renderable
assert "user123" in panel_renderable
@pytest.mark.timeout(20)
def test_process_pending_queue_element_multiple_locks(
ipython_live_updates_with_mocked_live, queue_elements
):
"""Test _process_pending_queue_element with multiple locks."""
live_updates, mock_live = ipython_live_updates_with_mocked_live
client = live_updates.client
queue, _, _ = queue_elements
# Create multiple locks
lock1 = messages.ScanQueueLock(identifier="user123", reason="Calibration")
lock2 = messages.ScanQueueLock(identifier="admin456", reason="Maintenance")
# Create a locked queue status with multiple locks
scan_queue_status = messages.ScanQueueStatus(info=[], status="LOCKED", locks=[lock1, lock2])
client.queue.queue_storage.current_scan_queue = {"primary": scan_queue_status}
with mock.patch(
"bec_lib.queue_items.QueueItem.queue_position", new_callable=mock.PropertyMock
) as queue_pos:
queue_pos.return_value = 0
# Call the method
live_updates._process_pending_queue_element(queue)
# Verify both locks are in the message
call_args = mock_live.call_args[0][0]
panel_renderable = call_args.renderable
assert "user123" in panel_renderable
assert "Calibration" in panel_renderable
assert "admin456" in panel_renderable
assert "Maintenance" in panel_renderable
@pytest.mark.timeout(20)
def test_process_pending_queue_element_queue_position_positive(
ipython_live_updates_with_mocked_live, queue_elements
):
"""Test _process_pending_queue_element when queue position > 0."""
live_updates, mock_live = ipython_live_updates_with_mocked_live
client = live_updates.client
queue, _, _ = queue_elements
# Create a running queue status
running_scan_queue_status = messages.ScanQueueStatus(info=[], status="RUNNING")
client.queue.queue_storage.current_scan_queue = {"primary": running_scan_queue_status}
with mock.patch(
"bec_lib.queue_items.QueueItem.queue_position", new_callable=mock.PropertyMock
) as queue_pos:
queue_pos.return_value = 2 # Queue position > 0
# Call the method
live_updates._process_pending_queue_element(queue)
# Verify Live panel was created with position info
mock_live.assert_called_once()
call_args = mock_live.call_args[0][0]
panel_renderable = call_args.renderable
assert "Scan is enqueued and is waiting for execution" in panel_renderable
assert "position in queue" in panel_renderable
assert "3" in panel_renderable # Position is displayed as queue_position + 1
assert "RUNNING" in panel_renderable
# Verify start was called
mock_live.return_value.start.assert_called_once()
@pytest.mark.timeout(20)
def test_process_pending_queue_element_queue_position_update_existing_live(
ipython_live_updates_with_mocked_live, queue_elements
):
"""Test _process_pending_queue_element updates existing Live when queue position > 0."""
live_updates, mock_live = ipython_live_updates_with_mocked_live
client = live_updates.client
queue, _, _ = queue_elements
# Create a running queue status
running_scan_queue_status = messages.ScanQueueStatus(info=[], status="RUNNING")
client.queue.queue_storage.current_scan_queue = {"primary": running_scan_queue_status}
with mock.patch(
"bec_lib.queue_items.QueueItem.queue_position", new_callable=mock.PropertyMock
) as queue_pos:
queue_pos.return_value = 1
# Set up existing _status_live
existing_live = mock.MagicMock()
live_updates._status_live = existing_live
# Call the method
live_updates._process_pending_queue_element(queue)
# Verify Live was not created again
mock_live.assert_not_called()
# Verify update was called instead
existing_live.update.assert_called_once()
call_args = existing_live.update.call_args[0][0]
panel_renderable = call_args.renderable
assert "Scan is enqueued and is waiting for execution" in panel_renderable
assert "2" in panel_renderable # Position is displayed as queue_position + 1
@pytest.mark.timeout(20)
def test_process_pending_queue_element_no_target_queue(
ipython_live_updates_with_mocked_live, queue_elements
):
"""Test _process_pending_queue_element when target queue is None."""
live_updates, mock_live = ipython_live_updates_with_mocked_live
client = live_updates.client
queue, _, _ = queue_elements
# Setup current_scan_queue but without the primary queue
client.queue.queue_storage.current_scan_queue = {
"secondary": messages.ScanQueueStatus(info=[], status="RUNNING")
}
with mock.patch(
"bec_lib.queue_items.QueueItem.queue_position", new_callable=mock.PropertyMock
) as queue_pos:
queue_pos.return_value = 0
# Call the method
live_updates._process_pending_queue_element(queue)
# Verify Live was never called since target_queue is None
mock_live.assert_not_called()
@pytest.mark.timeout(20)
def test_process_pending_queue_element_queue_position_zero_not_locked(
ipython_live_updates_with_mocked_live, queue_elements
):
"""Test _process_pending_queue_element when queue position is 0 and not locked."""
live_updates, mock_live = ipython_live_updates_with_mocked_live
client = live_updates.client
queue, _, _ = queue_elements
# Create a running queue status (not locked)
running_scan_queue_status = messages.ScanQueueStatus(info=[], status="RUNNING")
client.queue.queue_storage.current_scan_queue = {"primary": running_scan_queue_status}
with mock.patch(
"bec_lib.queue_items.QueueItem.queue_position", new_callable=mock.PropertyMock
) as queue_pos:
queue_pos.return_value = 0 # First in queue
# Call the method
live_updates._process_pending_queue_element(queue)
# Verify Live was not called since queue_position is 0 and not locked
mock_live.assert_not_called()
@pytest.mark.timeout(20)
def test_process_pending_queue_element_locked_then_position(
ipython_live_updates_with_mocked_live, queue_elements
):
"""Test _process_pending_queue_element prioritizes LOCKED status over position."""
live_updates, mock_live = ipython_live_updates_with_mocked_live
client = live_updates.client
queue, _, _ = queue_elements
# Create mock lock
lock = messages.ScanQueueLock(identifier="user123", reason="Manual hold for calibration")
# Create a locked queue status
scan_queue_status = messages.ScanQueueStatus(info=[], status="LOCKED", locks=[lock])
client.queue.queue_storage.current_scan_queue = {"primary": scan_queue_status}
with mock.patch(
"bec_lib.queue_items.QueueItem.queue_position", new_callable=mock.PropertyMock
) as queue_pos:
queue_pos.return_value = 3 # Also has a position in queue
# Call the method
live_updates._process_pending_queue_element(queue)
# Verify Live panel shows lock info (not position info)
call_args = mock_live.call_args[0][0]
panel_renderable = call_args.renderable
assert "lock" in panel_renderable.lower()
assert "user123" in panel_renderable
# Should not show position info
assert "position in queue" not in panel_renderable