Skip to content

Commit

Permalink
refactor: add expections for N1081B and add tests for it
Browse files Browse the repository at this point in the history
  • Loading branch information
furkan-bilgin committed Oct 13, 2024
1 parent 7f95a58 commit 20ef1b0
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
13 changes: 8 additions & 5 deletions src/daq/base.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import logging
import threading
from dataclasses import dataclass
from queue import Queue
from queue import Empty, Queue
from typing import Any

from daq.models import DAQJobMessage, DAQJobMessageStop, DAQJobStopError
Expand All @@ -25,10 +25,13 @@ def __init__(self, config: Any):

def consume(self):
# consume messages from the queue
while not self.message_in.empty():
message = self.message_in.get()
if not self.handle_message(message):
self.message_in.put_nowait(message)
while True:
try:
message = self.message_in.get_nowait()
if not self.handle_message(message):
self.message_in.put_nowait(message)
except Empty:
break

def handle_message(self, message: "DAQJobMessage") -> bool:
if isinstance(message, DAQJobMessageStop):
Expand Down
6 changes: 3 additions & 3 deletions src/daq/caen/n1081b.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ def start(self):
if not self._is_connected():
self._logger.info("Connecting to the device...")
self._connect_to_device()
self._logger.info("Connected!")

# Poll sections
self._poll_sections()
Expand All @@ -70,12 +71,11 @@ def _poll_sections(self):

res = self.device.get_function_results(section)
if not res:
continue
raise Exception("No results")

data = res["data"]
if "counters" not in data:
self._logger.info(f"No counters in section {section}")
continue
raise Exception(f"No counters in section {section}")

self._send_store_message(data, section.name)

Expand Down
8 changes: 6 additions & 2 deletions src/tests/test_n1081b.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ def test_poll_sections(self, mock_get_function_results):
@patch.object(N1081B, "get_function_results", return_value={"data": {}})
def test_poll_sections_no_counters(self, mock_get_function_results):
self.daq_job._send_store_message = MagicMock()
self.daq_job._poll_sections()
with self.assertRaises(Exception) as context:
self.daq_job._poll_sections()
self.assertTrue("No counters in section" in str(context.exception))
self.daq_job._send_store_message.assert_not_called()

@patch("time.sleep", return_value=None, side_effect=StopIteration)
Expand All @@ -71,7 +73,9 @@ def test_start(
@patch.object(N1081B, "get_function_results", return_value=None)
def test_poll_sections_no_results(self, mock_get_function_results):
self.daq_job._send_store_message = MagicMock()
self.daq_job._poll_sections()
with self.assertRaises(Exception) as context:
self.daq_job._poll_sections()
self.assertTrue("No results" in str(context.exception))
self.daq_job._send_store_message.assert_not_called()

@patch.object(
Expand Down

0 comments on commit 20ef1b0

Please sign in to comment.