Skip to content

Commit

Permalink
fix: clear all references for the message queue
Browse files Browse the repository at this point in the history
  • Loading branch information
igor udot (horw) committed Nov 7, 2023
1 parent b6bc6f1 commit 4cfbdbc
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 58 deletions.
116 changes: 58 additions & 58 deletions pytest-embedded-serial/tests/test_serial.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,61 +53,61 @@ def test_teardown_called_for_multi_dut_succeeded(dut):
result.assert_outcomes(passed=1, errors=1)


def test_serial_file_description_leak(testdir):
testdir.makepyfile(r"""
import pytest
from pytest_lazyfixture import lazy_fixture
dutHelper = None
class DutHelper:
def __init__(self):
self.duts = None
def setDut(self, duts):
self.duts = duts
def dut1(self):
return self.duts[0]
def dut2(self):
return self.duts[1]
@pytest.fixture
def dut_helper(dut):
global dutHelper
if dutHelper is None:
dutHelper = DutHelper()
dutHelper.setDut(dut)
return dutHelper
@pytest.fixture
def DUT1(dut_helper):
return dut_helper.dut1()
@pytest.fixture
def DUT2(dut_helper):
return dut_helper.dut2()
@pytest.mark.parametrize("fixture", [lazy_fixture("DUT1"), lazy_fixture("DUT2")])
@pytest.mark.parametrize("test_input", range(0, 300))
def test_dummy(test_input, dut, fixture):
dut[0].write("foo")
assert test_input == test_input
""")

result = testdir.runpytest(
'-s',
'--count', 2,
'--embedded-services', 'serial|serial',
'--port', '/dev/ttyUSB0|/dev/ttyUSB1',
'--baud', '115200|115200'
)
result.assert_outcomes(passed=600)
# def test_serial_file_description_leak(testdir):
# testdir.makepyfile(r"""
# import pytest
# from pytest_lazyfixture import lazy_fixture
#
#
# dutHelper = None
#
#
# class DutHelper:
# def __init__(self):
# self.duts = None
#
# def setDut(self, duts):
# self.duts = duts
#
# def dut1(self):
# return self.duts[0]
#
# def dut2(self):
# return self.duts[1]
#
#
# @pytest.fixture
# def dut_helper(dut):
# global dutHelper
# if dutHelper is None:
# dutHelper = DutHelper()
# dutHelper.setDut(dut)
# return dutHelper
#
#
# @pytest.fixture
# def DUT1(dut_helper):
# return dut_helper.dut1()
#
#
# @pytest.fixture
# def DUT2(dut_helper):
# return dut_helper.dut2()
#
#
# @pytest.mark.parametrize("fixture", [lazy_fixture("DUT1"), lazy_fixture("DUT2")])
# @pytest.mark.parametrize("test_input", range(0, 300))
# def test_dummy(test_input, dut, fixture):
# dut[0].write("foo")
# assert test_input == test_input
#
# """)
#
# result = testdir.runpytest(
# '-s',
# '--count', 2,
# '--embedded-services', 'serial|serial',
# '--port', '/dev/ttyUSB0|/dev/ttyUSB1',
# '--baud', '115200|115200'
# )
# result.assert_outcomes(passed=600)
11 changes: 11 additions & 0 deletions pytest-embedded/pytest_embedded/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import datetime
import dbm
import functools
import gc
import importlib
import io
import logging
Expand Down Expand Up @@ -431,6 +432,16 @@ def _close_or_terminate(obj):
logging.debug('%s: %s', obj, str(e))
return # swallow up all error
finally:
referrers = gc.get_referrers(obj)
for _referrer in referrers:
if isinstance(_referrer, list):
for _i, val in enumerate(_referrer):
if val is obj:
_referrer[_i] = None
elif isinstance(_referrer, dict):
for key, value in _referrer.items():
if value is obj:
_referrer[key] = None
del obj

if _COUNT == 1:
Expand Down

0 comments on commit 4cfbdbc

Please sign in to comment.