Skip to content

Commit

Permalink
Add test_ref_clock for all SHF devices
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian Duetsch committed Aug 21, 2024
1 parent 5b5c82a commit 42da239
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 0 deletions.
5 changes: 5 additions & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Copyright (C) 2024 Zurich Instruments
#
# This software may be modified and distributed under the terms
# of the MIT license. See the LICENSE file for details.
"""Pytest tests directory."""
5 changes: 5 additions & 0 deletions tests/test_shfqa.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from zhinst.toolkit import SHFQAChannelMode
from zhinst.toolkit.driver.devices.shfqa import Generator, QAChannel, Readout, SHFScope
from tests.utils import shf_test_ref_clock


def test_repr(shfqa):
Expand Down Expand Up @@ -81,3 +82,7 @@ def test_qa_readout(shfqa):
"0",
"readout",
)


def test_ref_clock(mock_connection, shfqa):
shf_test_ref_clock(mock_connection, shfqa)
5 changes: 5 additions & 0 deletions tests/test_shfqc.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from zhinst.toolkit import SHFQAChannelMode
from zhinst.toolkit.driver.devices.shfqa import Generator, QAChannel, Readout, SHFScope
from zhinst.toolkit.driver.devices.shfsg import AWG, Node, SGChannel
from tests.utils import shf_test_ref_clock


def test_repr(shfqc):
Expand Down Expand Up @@ -250,3 +251,7 @@ def test_configure_sine_generation(mock_connection, shfqc):
gains=(3.0, -1.0, 5.0, 1.0),
sine_generator_index=8,
)


def test_ref_clock(mock_connection, shfqc):
shf_test_ref_clock(mock_connection, shfqc)
5 changes: 5 additions & 0 deletions tests/test_shfsg.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import pytest

from zhinst.toolkit.driver.devices.shfsg import AWG, Node, SGChannel
from tests.utils import shf_test_ref_clock


def test_repr(shfsg):
Expand Down Expand Up @@ -171,3 +172,7 @@ def test_configure_sine_generation(mock_connection, shfsg):
gains=(3.0, -1.0, 5.0, 1.0),
sine_generator_index=8,
)


def test_ref_clock(mock_connection, shfsg):
shf_test_ref_clock(mock_connection, shfsg)
46 changes: 46 additions & 0 deletions tests/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
from itertools import cycle

import pytest


def shf_test_ref_clock(mock_connection, shf):
"""Test reference clock logic shared between all SHF devices."""
status = cycle([0])
source = 0
source_actual = 0

def getInt_side_effect(path):
if path == "/dev1234/system/clocks/referenceclock/in/status":
return next(status)
if path == "/dev1234/system/clocks/referenceclock/in/source":
return source
if path == "/dev1234/system/clocks/referenceclock/in/sourceactual":
return source_actual
raise RuntimeError("Invalid Node")

def get_side_effect(path, **kwargs):
value = getInt_side_effect(path)
return {path: {"timestamp": [0], "value": [value]}}

mock_connection.return_value.getInt.side_effect = getInt_side_effect
mock_connection.return_value.get.side_effect = get_side_effect

assert shf.check_ref_clock(sleep_time=0.001)
# Locked within time
status = iter([2] * 2 + [0] * 10)
assert shf.check_ref_clock(sleep_time=0.001)
# Locking error but actual_clock == clock
status = cycle([1])
assert not shf.check_ref_clock(sleep_time=0.001)
# Locking error and actual_clock != clock => reset clock to internal
source = 1
mock_connection.return_value.syncSetString.assert_not_called()
assert not shf.check_ref_clock(sleep_time=0.001)
mock_connection.return_value.syncSetString.assert_called_with(
"/dev1234/system/clocks/referenceclock/in/source", "internal"
)

# timeout
status = cycle([2])
with pytest.raises(TimeoutError) as e_info:
shf.check_ref_clock(timeout=0.01, sleep_time=0.001)

0 comments on commit 42da239

Please sign in to comment.