Skip to content

Commit

Permalink
test that connections are closed on gc
Browse files Browse the repository at this point in the history
  • Loading branch information
jenshnielsen committed Sep 11, 2023
1 parent d9bfe3d commit f336288
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion qcodes/tests/test_visa.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import gc
import logging
import re
from pathlib import Path

Expand All @@ -6,7 +8,8 @@
import pyvisa.constants
from pytest import FixtureRequest

from qcodes.instrument.visa import VisaInstrument
from qcodes.instrument import Instrument, VisaInstrument
from qcodes.instrument_drivers.american_magnetics import AMIModel430
from qcodes.validators import Numbers


Expand Down Expand Up @@ -109,6 +112,33 @@ def _make_mock_visa():
mv.close()


def test_visa_gc_closes_connection(caplog) -> None:
def use_magnet():
_ = AMIModel430(
"x",
address="GPIB::1::INSTR",
pyvisa_sim_file="AMI430.yaml",
terminator="\n",
)
assert list(Instrument._all_instruments.keys()) == ["x"]

# ensure that any unused instruments that have not been gced are gced before running
gc.collect()
caplog.clear()
with caplog.at_level(logging.INFO, logger="qcodes.instrument.visa"):
use_magnet()
gc.collect()
# at this stage the instrument created in use_magnet has gone out of scope
# and we have triggered an explicit gc so the finalize method has been triggered/
# We test this
# and the instrument should no longer be in the instrument registry
assert list(Instrument._all_instruments.keys()) == []
assert (
caplog.records[-1].message
== "Closing VISA handle to x as there are no non weak references to the instrument."
)


def test_ask_write_local(mock_visa) -> None:

# test normal ask and write behavior
Expand Down

0 comments on commit f336288

Please sign in to comment.