File: test_device_proxy.py

package info (click to toggle)
pytango 10.0.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 10,196 kB
  • sloc: python: 28,206; cpp: 16,380; sql: 255; sh: 82; makefile: 43
file content (751 lines) | stat: -rw-r--r-- 23,879 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
# SPDX-FileCopyrightText: All Contributors to the PyTango project
# SPDX-License-Identifier: LGPL-3.0-or-later
"""Client tests that run against the standard TangoTest device.

Due to a TANGO 9 issue (#821), the device is run without any database.
Note that this means that various features won't work:

 * No device configuration via properties.
 * No event generated by the server.
 * No memorized attributes.
 * No device attribute configuration via the database.

So don't even try to test anything of the above as it will not work
and is even likely to crash the device (!)

"""

import asyncio
import gc
import multiprocessing
import time
import weakref

import pytest

from tango import (
    AttributeInfo,
    AttributeInfoEx,
    AttributeInfoList,
    AttributeInfoListEx,
    DeviceInfo,
    DeviceProxy,
    EventType,
    ExtractAs,
    GreenMode,
    Group,
    PyTangoUserWarning,
    constants,
)
from tango.constants import AllAttr
from tango.server import Device, attribute
from tango.test_utils import (
    DeviceTestContext,
    assert_close,
    bytes_devstring,
    convert_to_type,
    str_devstring,
)
from tango.utils import (
    EventCallback,
    is_str_type,
    is_int_type,
    is_float_type,
    is_bool_type,
)

ATTRIBUTES = [
    "State",
    "Status",
    "ampli",
    "boolean_image",
    "boolean_image_ro",
    "boolean_scalar",
    "boolean_spectrum",
    "boolean_spectrum_ro",
    "double_image",
    "double_image_ro",
    "double_scalar",
    "double_scalar_rww",
    "double_scalar_w",
    "double_spectrum",
    "double_spectrum_ro",
    "echo_mode",
    "enum_image",
    "enum_image_ro",
    "enum_scalar",
    "enum_scalar_ro",
    "enum_spectrum",
    "enum_spectrum_ro",
    "float_image",
    "float_image_ro",
    "float_scalar",
    "float_spectrum",
    "float_spectrum_ro",
    "freq",
    "long64_image_ro",
    "long64_scalar",
    "long64_spectrum_ro",
    "long_image",
    "long_image_ro",
    "long_scalar",
    "long_scalar_rww",
    "long_scalar_w",
    "long_spectrum",
    "long_spectrum_ro",
    "no_value",
    "short_image",
    "short_image_ro",
    "short_scalar",
    "short_scalar_ro",
    "short_scalar_rww",
    "short_scalar_w",
    "short_spectrum",
    "short_spectrum_ro",
    "string_image",
    "string_image_ro",
    "string_scalar",
    "string_spectrum",
    "string_spectrum_ro",
    "throw_exception",
    "uchar_image",
    "uchar_image_ro",
    "uchar_scalar",
    "uchar_spectrum",
    "uchar_spectrum_ro",
    "ulong64_image_ro",
    "ulong64_scalar",
    "ulong64_spectrum_ro",
    "ulong_image_ro",
    "ulong_scalar",
    "ulong_spectrum_ro",
    "ushort_image",
    "ushort_image_ro",
    "ushort_scalar",
    "ushort_spectrum",
    "ushort_spectrum_ro",
    "wave",
]

WRITABLE_SCALAR_ATTRIBUTES = [
    a for a in ATTRIBUTES if "scalar" in a and a.split("_")[-1] not in ("ro", "rww")
]
WRITABLE_SPECTRUM_ATTRIBUTES = [
    a for a in ATTRIBUTES if "spectrum" in a and a.split("_")[-1] not in ("ro", "rww")
]

TEST_DOUBLE_ATTRIBUTES = (
    ("double_scalar_w", -28.2),
    ("double_spectrum", [-28.2, 23.4]),
    ("double_image", [[-28.2, 23.4], [-4.9, 6.5]]),
)

# Helpers


def ping_device(proxy):
    if proxy.get_green_mode() == GreenMode.Asyncio:
        asyncio.get_event_loop().run_until_complete(proxy.ping())
    else:
        proxy.ping()


# Fixtures


@pytest.fixture(params=ATTRIBUTES)
def attributes(request):
    return request.param


@pytest.fixture(
    params=[a for a in ATTRIBUTES if a not in ("no_value", "throw_exception")]
)
def readable_attribute(request):
    return request.param


@pytest.fixture(params=WRITABLE_SCALAR_ATTRIBUTES)
def writable_scalar_attribute(request):
    return request.param


@pytest.fixture(params=WRITABLE_SPECTRUM_ATTRIBUTES)
def writable_spectrum_attribute(request):
    return request.param


@pytest.fixture
def simple_device_fqdn():
    class TestDevice(Device):
        pass

    context = DeviceTestContext(TestDevice, host="127.0.0.1")
    context.start()
    yield context.get_device_access()
    context.stop()


# Tests


def test_ping(tango_test):
    duration = tango_test.ping(wait=True)
    assert isinstance(duration, int)


def test_info(tango_test):
    info = tango_test.info()
    assert isinstance(info, DeviceInfo)
    assert info.dev_class == "TangoTest"
    info_dict = info.version_info
    assert isinstance(info_dict, dict)
    assert len(info_dict) > 0


def test_read_attribute(tango_test, readable_attribute):
    "Check that readable attributes can be read"
    # For read-only string spectrum and read-only string image types,
    # the following error is very likely to be raised:
    # -> MARSHAL CORBA system exception: MARSHAL_PassEndOfMessage
    # An explicit sleep fixes the problem, but it's annoying to maintain

    if readable_attribute in ["string_image_ro", "string_spectrum_ro"]:
        pytest.xfail()
    if readable_attribute == 'echo_mode':
        pytest.skip("This test is failing and temporarily disabled.")
    tango_test.read_attribute(readable_attribute, wait=True)


def test_read_write_attribute_with_green_modes(tango_test_with_green_modes):
    """
    Check that attributes can be read/write with all green modes
    """
    for attr_name, write_value in TEST_DOUBLE_ATTRIBUTES:
        tango_test_with_green_modes.write_attribute(attr_name, write_value, wait=True)
        read_attr = tango_test_with_green_modes.read_attribute(attr_name, wait=True)
        assert_close(read_attr.value, write_value)
        assert_close(read_attr.w_value, write_value)


@pytest.mark.asyncio
async def test_high_level_api_for_asyncio(tango_test):
    tango_test.set_green_mode(GreenMode.Asyncio)
    _ = await tango_test.long_scalar
    _ = await getattr(tango_test, "long_scalar")
    _ = await tango_test["long_scalar"]


def test_write_scalar_attribute(tango_test, writable_scalar_attribute):
    "Check that writable scalar attributes can be written"
    attr_name = writable_scalar_attribute
    config = tango_test.get_attribute_config(attr_name, wait=True)
    if is_bool_type(config.data_type):
        tango_test.write_attribute(attr_name, True, wait=True)
    elif is_int_type(config.data_type):
        tango_test.write_attribute(attr_name, 76, wait=True)
    elif is_float_type(config.data_type):
        tango_test.write_attribute(attr_name, -28.2, wait=True)
    elif is_str_type(config.data_type):
        tango_test.write_attribute(attr_name, "hello", wait=True)
    else:
        pytest.xfail("Not currently testing this type")


def test_write_read_spectrum_attribute(
    tango_test, writable_spectrum_attribute, extract_as
):
    "Check that writable spectrum attributes can be written and read"
    requested_type, expected_type = extract_as
    attr_name = writable_spectrum_attribute
    config = tango_test.get_attribute_config(attr_name, wait=True)
    if is_bool_type(config.data_type):
        write_values = [True, False]
    elif is_int_type(config.data_type):
        write_values = [76, 77]
    elif is_float_type(config.data_type):
        if requested_type == ExtractAs.String:
            # it is hard to find a proper float values, which can be converted to str,
            # most of them case UnicodeDecodeError, so we use the most simple one
            write_values = [0, 0]
        else:
            write_values = [-28.2, 44.3]
    elif is_str_type(config.data_type):
        if requested_type == ExtractAs.Numpy:
            expected_type = tuple
        if requested_type in [ExtractAs.ByteArray, ExtractAs.Bytes, ExtractAs.String]:
            pytest.xfail(
                "Conversion from (str,) to ByteArray, Bytes and String not supported. May be fixed in future"
            )
        write_values = ["hello", "hola"]
    else:
        pytest.xfail("Not currently testing this type")

    tango_test.write_attribute(attr_name, write_values, wait=True)
    read_attr = tango_test.read_attribute(attr_name, extract_as=requested_type)

    assert isinstance(read_attr.value, expected_type)
    assert_close(
        read_attr.value, convert_to_type(write_values, config.data_type, expected_type)
    )

    assert isinstance(read_attr.w_value, expected_type)
    assert_close(
        read_attr.w_value,
        convert_to_type(write_values, config.data_type, expected_type),
    )


def test_write_read_empty_spectrum_attribute(tango_test, writable_spectrum_attribute):
    "Check that writing empty list to spectrum attribute reads back as None."
    attr_name = writable_spectrum_attribute
    config = tango_test.get_attribute_config(attr_name, wait=True)
    if is_str_type(config.data_type):
        pytest.xfail(
            "Conversion from (str,) to numpy not supported. Probably, may be fixed in future"
        )

    tango_test.write_attribute(attr_name, [], wait=True)
    read_attr = tango_test.read_attribute(attr_name, wait=True)
    assert not len(read_attr.value)


def test_write_read_string_attribute(tango_test):
    attr_name = "string_scalar"
    bytes_big = 100000 * b"big data "
    str_big = bytes_big.decode("latin-1")

    values = [
        b"",
        "",
        "Hello, World!",
        b"Hello, World!",
        bytes_devstring,
        str_devstring,
        bytes_big,
        str_big,
    ]

    expected_values = [
        "",
        "",
        "Hello, World!",
        "Hello, World!",
        str_devstring,
        str_devstring,
        str_big,
        str_big,
    ]

    for value, expected_value in zip(values, expected_values):
        tango_test.write_attribute(attr_name, value, wait=True)
        result = tango_test.read_attribute(attr_name, wait=True)
        assert result.value == expected_value

    attr_name = "string_spectrum"
    for value, expected_value in zip(values, expected_values):
        tango_test.write_attribute(attr_name, ["", value, ""], wait=True)
        result = tango_test.read_attribute(attr_name, wait=True)
        assert result.value[1] == expected_value

    attr_name = "string_image"
    for value, expected_value in zip(values, expected_values):
        tango_test.write_attribute(attr_name, [[value], [value]], wait=True)
        result = tango_test.read_attribute(attr_name, wait=True)
        assert result.value == ((expected_value,), (expected_value,))


def test_set_non_existent_attribute_raises_by_default(tango_test):
    with pytest.raises(AttributeError, match="some_invalid_name"):
        tango_test.some_invalid_name = "123"


def test_set_non_existent_attribute_allowed_if_dynamic_interface_unfrozen(tango_test):
    with pytest.warns(PyTangoUserWarning):
        tango_test.unfreeze_dynamic_interface()
    tango_test.some_invalid_name = "123"
    assert tango_test.some_invalid_name == "123"


def test_dynamic_interface_can_be_toggled(tango_test):
    with pytest.warns(PyTangoUserWarning):
        tango_test.unfreeze_dynamic_interface()
    tango_test.some_invalid_name = "456"
    assert tango_test.some_invalid_name == "456"
    tango_test.freeze_dynamic_interface()
    with pytest.raises(AttributeError, match="another_invalid_name"):
        tango_test.another_invalid_name = "123"


def test_dynamic_interface_flag_can_be_read(tango_test):
    with pytest.warns(PyTangoUserWarning):
        tango_test.unfreeze_dynamic_interface()
    assert not tango_test.is_dynamic_interface_frozen()
    tango_test.freeze_dynamic_interface()
    assert tango_test.is_dynamic_interface_frozen()


def test_dynamic_interface_only_applies_to_device_proxy_instance(tango_test):
    other_proxy = DeviceProxy(tango_test.adm_name())
    with pytest.warns(PyTangoUserWarning):
        tango_test.unfreeze_dynamic_interface()
    other_proxy.freeze_dynamic_interface()
    assert not tango_test.is_dynamic_interface_frozen()
    assert other_proxy.is_dynamic_interface_frozen()


def test_dynamic_interface_unfreeze_generates_a_user_warning(tango_test):
    with pytest.warns(PyTangoUserWarning):
        tango_test.unfreeze_dynamic_interface()


@pytest.mark.skip(reason="This test is failing and temporarily disabled.")
def test_read_attribute_config(tango_test):
    all_conf = tango_test.get_attribute_config(ATTRIBUTES)
    assert len(all_conf) == len(ATTRIBUTES)
    assert set([conf.name for conf in all_conf]) == set(ATTRIBUTES)

    for attr in ATTRIBUTES:
        tango_test.get_attribute_config(attr)


def test_read_attribute_config_ex():
    class TestDevice(Device):

        @attribute(dtype=int, unit="mA")
        def attr_config_int(self):
            return 1

        @attribute(dtype=bool)
        def attr_config_bool(self):
            return True

    def assert_attr_config_ok(dev_proxy):
        # testing that call to get_attribute_config_ex for all types of
        # input arguments gives same result and doesn't raise an exception
        ac1 = dev_proxy.get_attribute_config_ex("attr_config_int")
        ac2 = dev_proxy.get_attribute_config_ex(["attr_config_int"])
        assert repr(ac1) == repr(ac2)

    def assert_multiple_attrs_config_ok(dev_proxy):
        # testing that querying multiple attributes gives same result and
        # doesn't raise an exception
        ac1 = dev_proxy.get_attribute_config_ex("attr_config_int")
        ac2 = dev_proxy.get_attribute_config_ex("attr_config_bool")
        acs = dev_proxy.get_attribute_config_ex(("attr_config_int", "attr_config_bool"))
        acs_2 = dev_proxy.get_attribute_config_ex(
            ["attr_config_int", "attr_config_bool"]
        )

        acs_all = dev_proxy.get_attribute_config_ex(AllAttr)

        assert repr(ac1[0]) == repr(acs[0]) == repr(acs_2[0]) == repr(
            acs_all[0]
        ) and repr(ac2[0]) == repr(acs[1]) == repr(acs_2[1]) == repr(acs_all[1])

    with DeviceTestContext(TestDevice) as proxy:
        assert_attr_config_ok(proxy)
        assert_multiple_attrs_config_ok(proxy)

@pytest.mark.skip(reason="This test is failing and temporarily disabled.")
def test_attribute_list_query(tango_test):
    attrs = tango_test.attribute_list_query()
    assert isinstance(attrs, AttributeInfoList)
    assert all(isinstance(a, AttributeInfo) for a in attrs)
    assert {a.name for a in attrs} == set(ATTRIBUTES)


@pytest.mark.skip(reason="This test is failing and temporarily disabled.")
def test_attribute_list_query_ex(tango_test):
    attrs = tango_test.attribute_list_query_ex()
    assert isinstance(attrs, AttributeInfoListEx)
    assert all(isinstance(a, AttributeInfoEx) for a in attrs)
    assert {a.name for a in attrs} == set(ATTRIBUTES)


def test_device_proxy_dir_method(tango_test):
    lst = dir(tango_test)
    attrs = tango_test.get_attribute_list()
    cmds = tango_test.get_command_list()
    with pytest.warns(DeprecationWarning):
        pipes = tango_test.get_pipe_list()
    methods = dir(type(tango_test))
    internals = tango_test.__dict__.keys()
    # Check attributes
    assert set(attrs) < set(lst)
    assert set(map(str.lower, attrs)) < set(lst)
    # Check commands
    assert set(cmds) < set(lst)
    assert set(map(str.lower, cmds)) < set(lst)
    # Check pipes
    assert set(pipes) < set(lst)
    assert set(map(str.lower, pipes)) < set(lst)
    # Check internals
    assert set(methods) <= set(lst)
    # Check internals
    assert set(internals) <= set(lst)


def test_device_polling_command(tango_test):
    dct = {"SwitchStates": 1000, "DevVoid": 10000, "DumpExecutionState": 5000}

    for command, period in dct.items():
        tango_test.poll_command(command, period)

    ans = tango_test.polling_status()
    for info in ans:
        lines = info.split("\n")
        command = lines[0].split("= ")[1]
        period = int(lines[1].split("= ")[1])
        assert dct[command] == period


def test_device_polling_attribute(tango_test):
    dct = {"boolean_scalar": 1000, "double_scalar": 10000, "long_scalar": 5000}

    for attr, poll_period in dct.items():
        tango_test.poll_attribute(attr, poll_period)

    ans = tango_test.polling_status()
    for x in ans:
        lines = x.split("\n")
        attr = lines[0].split("= ")[1]
        poll_period = int(lines[1].split("= ")[1])
        assert dct[attr] == poll_period


def test_command_string(tango_test):
    cmd_name = "DevString"
    bytes_big = 100000 * b"big data "
    str_big = bytes_big.decode("latin-1")

    values = [
        b"",
        "",
        "Hello, World!",
        b"Hello, World!",
        bytes_devstring,
        str_devstring,
        bytes_big,
        str_big,
    ]

    expected_values = [
        "",
        "",
        "Hello, World!",
        "Hello, World!",
        str_devstring,
        str_devstring,
        str_big,
        str_big,
    ]

    for value, expected_value in zip(values, expected_values):
        result = tango_test.command_inout(cmd_name, value, wait=True)
        assert result == expected_value

    cmd_name = "DevVarStringArray"
    for value, expected_value in zip(values, expected_values):
        result = tango_test.command_inout(cmd_name, [value, value], wait=True)
        assert result == [expected_value, expected_value]

    cmd_name = "DevVarLongStringArray"
    for value, expected_value in zip(values, expected_values):
        result = tango_test.command_inout(
            cmd_name, [[-10, 200], [value, value]], wait=True
        )
        assert len(result) == 2
        assert_close(result[0], [-10, 200])
        assert_close(result[1], [expected_value, expected_value])


def test_command_raises_type_error_for_bad_input(tango_test):
    expected_message = "Invalid input argument for command"
    with pytest.raises(TypeError, match=expected_message):
        tango_test.command_inout("DevDouble", "123.0")
    with pytest.raises(TypeError, match=expected_message):
        tango_test.command_inout("DevLong64", [1, 2, 3])
    with pytest.raises(TypeError, match=expected_message):
        tango_test.command_inout("DevString", [{"invalid": "type"}, 123.0])
    with pytest.raises(TypeError, match=expected_message):
        tango_test.command_inout("DevVarStringArray", [1, 2, 3])
    with pytest.raises(TypeError, match=expected_message):
        tango_test.command_inout("DevVarLong64Array", ["1", 2.0, {3}])


def test_repr_uses_info(green_mode_device_proxy, simple_device_fqdn):
    proxy = green_mode_device_proxy(simple_device_fqdn)
    assert repr(proxy) == "TestDevice(test/nodb/testdevice)"


def test_repr_default_if_info_unavailable(green_mode_device_proxy, simple_device_fqdn):
    proxy = green_mode_device_proxy(simple_device_fqdn)

    def bad_info(self):
        raise RuntimeError("Break info for test")

    proxy.__class__.info = bad_info

    assert repr(proxy) == "Device(test/nodb/testdevice)"


def test_multiple_repr_calls_only_call_info_once(
    green_mode_device_proxy, simple_device_fqdn
):
    proxy = green_mode_device_proxy(simple_device_fqdn)

    def mock_info(self):
        self.info_call_count += 1
        return self.info_orig()

    proxy.__class__.info_orig = proxy.info
    proxy.__class__.info = mock_info
    proxy.__dict__["info_call_count"] = 0

    repr(proxy)
    assert proxy.info_call_count == 1
    repr(proxy)
    assert proxy.info_call_count == 1


def test_no_memory_leak_for_repr(green_mode_device_proxy, simple_device_fqdn):
    proxy = green_mode_device_proxy(simple_device_fqdn)
    ping_device(proxy)
    weak_ref = weakref.ref(proxy)

    repr(proxy)

    # clear strong reference and check if object can be garbage collected
    del proxy
    assert_object_released_after_gc(weak_ref)


def test_no_memory_leak_for_str(green_mode_device_proxy, simple_device_fqdn):
    proxy = green_mode_device_proxy(simple_device_fqdn)
    ping_device(proxy)
    weak_ref = weakref.ref(proxy)

    str(proxy)

    # clear strong reference and check if object can be garbage collected
    del proxy
    assert_object_released_after_gc(weak_ref)


def test_no_cyclic_ref_for_proxy(green_mode_device_proxy, simple_device_fqdn):
    proxy = green_mode_device_proxy(simple_device_fqdn)
    ping_device(proxy)
    weak_ref = weakref.ref(proxy)

    # clear strong reference and check if object is immediately released
    del proxy
    assert_object_released_without_gc(weak_ref)


def group_client_lifecycle(device_trl):
    group = Group("test")
    group_weak_ref = weakref.ref(group)
    group.add(device_trl)
    del group
    assert_object_released_without_gc(group_weak_ref)


def device_proxy_lifecycle(device_trl):
    proxy = DeviceProxy(device_trl)
    proxy_weak_ref = weakref.ref(proxy)
    del proxy
    assert_object_released_without_gc(proxy_weak_ref)


@pytest.mark.extra_src_test
@pytest.mark.parametrize("subject", [group_client_lifecycle, device_proxy_lifecycle])
def test_client_destructor_does_not_deadlock(
    tango_test_process_device_trl_with_function_scope, subject
):
    proc, device_trl = tango_test_process_device_trl_with_function_scope

    # Create client to be run in a subprocess.
    # We want this client to wait for a "not connected event"
    # while rapidly creating and destroying objects (using "subject" function)
    # to try to induce a race condition.
    # The "not connected event" is about 2 seconds after the event heartbeat period
    # of 10 seconds - we add some margin either side.
    # We use some Event objects for additional synchronisation
    heartbeat_margin = 0.5
    delay_before_heartbeat = constants.EVENT_HEARTBEAT_PERIOD - heartbeat_margin
    max_time_until_not_connected_event = delay_before_heartbeat + 3.0
    process_startup_timeout = 1.0
    process_exit_timeout = 1.0
    proxy_created = multiprocessing.Event()
    process_terminated = multiprocessing.Event()
    client = multiprocessing.Process(
        target=continuously_call_subject_during_not_connected_event,
        args=(
            device_trl,
            proxy_created,
            process_terminated,
            delay_before_heartbeat,
            max_time_until_not_connected_event,
            subject,
        ),
    )
    # start client, and wait for it to create a DeviceProxy
    client.start()
    assert proxy_created.wait(timeout=process_startup_timeout)

    # now that client has created DeviceProxy, we terminate the device server process
    # and inform the client
    proc.terminate()
    process_terminated.set()

    # finally, wait for client.
    # - if deadlocked, the exitcode will be None.
    # - if it has some other problem, the exitcode will be non-zero.
    client.join(timeout=max_time_until_not_connected_event + process_exit_timeout)
    assert client.exitcode == 0


def continuously_call_subject_during_not_connected_event(
    device_trl: str,
    proxy_created: multiprocessing.Event,
    process_terminated: multiprocessing.Event,
    delay_before_heartbeat: float,
    max_duration: float,
    subject: callable,
):
    # stateless subscription
    cb = EventCallback()
    proxy = DeviceProxy(device_trl)
    proxy_created.set()
    eid = proxy.subscribe_event(
        "boolean_scalar", EventType.CHANGE_EVENT, cb, stateless=True
    )
    assert eid > 0

    # wait for Tango device server to be terminated by parent
    terminate_timeout = 1.0
    assert process_terminated.wait(timeout=terminate_timeout)

    # wait for a "not connected event" while rapidly calling the
    # subject function (which will create and destroy objects)
    # to try to induce the race condition
    start_time = time.time()
    time.sleep(delay_before_heartbeat)
    while time.time() - start_time < max_duration:
        subject(device_trl)


def assert_object_released_after_gc(weak_ref):
    gc.collect()
    assert weak_ref() is None


def assert_object_released_without_gc(weak_ref):
    try:
        assert weak_ref() is None
    except AssertionError:
        if weak_ref().get_green_mode() == GreenMode.Asyncio:
            pytest.xfail("Sometimes fails with a concurrent.Future ref cycle")
        else:
            raise