|
3 | 3 | from collections import deque
|
4 | 4 | from random import choice
|
5 | 5 | from random import randint
|
| 6 | +import struct |
6 | 7 |
|
7 | 8 | from controller.constants import CURI_VID
|
| 9 | +from controller.constants import NUM_WELLS |
8 | 10 | from controller.constants import SERIAL_COMM_BAUD_RATE
|
9 | 11 | from controller.constants import SERIAL_COMM_BYTESIZE
|
10 | 12 | from controller.constants import SERIAL_COMM_READ_TIMEOUT
|
11 | 13 | from controller.constants import SerialCommPacketTypes
|
| 14 | +from controller.constants import STIM_WELL_IDX_TO_MODULE_ID |
| 15 | +from controller.constants import StimulatorCircuitStatuses |
12 | 16 | from controller.constants import STM_VID
|
13 | 17 | from controller.exceptions import InstrumentCommandResponseError
|
14 | 18 | from controller.exceptions import NoInstrumentDetectedError
|
15 | 19 | from controller.subsystems import instrument_comm
|
16 | 20 | from controller.subsystems.instrument_comm import InstrumentComm
|
17 | 21 | from controller.utils.aio import clean_up_tasks
|
| 22 | +from controller.utils.serial_comm import convert_adc_readings_to_circuit_status |
18 | 23 | from controller.utils.serial_comm import create_data_packet
|
19 | 24 | import pytest
|
20 | 25 | import serial
|
21 | 26 | from serial.tools.list_ports_common import ListPortInfo
|
22 | 27 |
|
23 | 28 | from ..fixtures import fixture__wait_tasks_clean
|
24 | 29 | from ..helpers import compare_exceptions
|
| 30 | +from ..helpers import random_bool |
25 | 31 | from ..helpers import random_serial_comm_timestamp
|
26 | 32 |
|
27 | 33 |
|
@@ -310,20 +316,52 @@ async def test_InstrumentComm__handles_start_stim_checks_command__success(
|
310 | 316 | ):
|
311 | 317 | test_ic, test_instrument = test_instrument_comm_obj_with_connection
|
312 | 318 |
|
313 |
| - test_command = {"command": "start_stim_checks"} |
| 319 | + test_well_indices = list(range(4)) |
| 320 | + test_well_indices.extend([i for i in range(4, NUM_WELLS) if random_bool()]) |
| 321 | + |
| 322 | + # set known adc readings in simulator. these first 4 values are hard coded, if this test fails might need to update them |
| 323 | + adc_readings = [(0, 0), (0, 2039), (0, 2049), (1113, 0)] |
| 324 | + adc_readings.extend([(i, i + 100) for i in range(NUM_WELLS - len(adc_readings))]) |
| 325 | + |
| 326 | + test_command = {"command": "start_stim_checks", "well_indices": test_well_indices} |
314 | 327 |
|
315 | 328 | run_task = asyncio.create_task(test_ic.run(asyncio.Future()))
|
316 | 329 |
|
317 | 330 | await test_ic._from_monitor_queue.put(test_command)
|
| 331 | + |
318 | 332 | # set up response
|
| 333 | + adc_readings_ordered_by_module_id = [None] * NUM_WELLS |
| 334 | + for well_idx, readings in enumerate(adc_readings): |
| 335 | + module_id = STIM_WELL_IDX_TO_MODULE_ID[well_idx] |
| 336 | + adc_readings_ordered_by_module_id[module_id] = readings |
| 337 | + response_body = bytes([]) |
| 338 | + for module_readings in adc_readings_ordered_by_module_id: |
| 339 | + status = convert_adc_readings_to_circuit_status(*module_readings) |
| 340 | + response_body += struct.pack("<HHB", *module_readings, status) |
319 | 341 | test_instrument.send.append(
|
320 | 342 | create_data_packet(
|
321 |
| - random_serial_comm_timestamp(), SerialCommPacketTypes.STIM_IMPEDANCE_CHECK, bytes([0]) |
| 343 | + random_serial_comm_timestamp(), SerialCommPacketTypes.STIM_IMPEDANCE_CHECK, response_body |
322 | 344 | )
|
323 | 345 | )
|
324 | 346 |
|
325 |
| - assert await asyncio.wait_for(test_ic._comm_to_monitor_queue.get(), timeout=1) == test_command |
326 |
| - |
327 |
| - # TODO assert something else here? |
| 347 | + msg_to_main = await asyncio.wait_for(test_ic._comm_to_monitor_queue.get(), timeout=1) |
| 348 | + |
| 349 | + # make sure that the message sent back to main contains the correct values |
| 350 | + stimulator_circuit_statuses = {} |
| 351 | + for well_idx in test_well_indices: |
| 352 | + well_readings = adc_readings[well_idx] |
| 353 | + status_int = convert_adc_readings_to_circuit_status(*well_readings) |
| 354 | + status = list(StimulatorCircuitStatuses)[status_int + 1].name.lower() |
| 355 | + stimulator_circuit_statuses[well_idx] = status |
| 356 | + |
| 357 | + assert msg_to_main == { |
| 358 | + **test_command, |
| 359 | + "stimulator_circuit_statuses": stimulator_circuit_statuses, |
| 360 | + "adc_readings": { |
| 361 | + well_idx: readings |
| 362 | + for well_idx, readings in enumerate(adc_readings) |
| 363 | + if well_idx in test_well_indices |
| 364 | + }, |
| 365 | + } |
328 | 366 |
|
329 | 367 | await clean_up_tasks({run_task})
|
0 commit comments