From 10b3866db4be92eb7f46af61f19e3c05122882c6 Mon Sep 17 00:00:00 2001
From: lukken <lukken@astron.nl>
Date: Wed, 17 Nov 2021 10:46:00 +0000
Subject: [PATCH] L2SS-205: Dry method to perform device turn off

Closes-Bug: L2SS-493
---
 .../integration_test/device_proxy.py               | 14 ++++++++++++++
 .../integration_test/devices/test_device_recv.py   | 12 +++---------
 .../integration_test/devices/test_device_sdp.py    | 13 +++----------
 .../integration_test/devices/test_device_sst.py    | 10 +---------
 .../integration_test/devices/test_device_unb2.py   | 10 +---------
 5 files changed, 22 insertions(+), 37 deletions(-)

diff --git a/tangostationcontrol/tangostationcontrol/integration_test/device_proxy.py b/tangostationcontrol/tangostationcontrol/integration_test/device_proxy.py
index 00ba0904c..b0085d964 100644
--- a/tangostationcontrol/tangostationcontrol/integration_test/device_proxy.py
+++ b/tangostationcontrol/tangostationcontrol/integration_test/device_proxy.py
@@ -1,3 +1,5 @@
+import time
+
 from tango import DeviceProxy
 
 
@@ -6,3 +8,15 @@ class TestDeviceProxy(DeviceProxy):
     def __init__(self, *args, **kwargs):
         super(TestDeviceProxy, self).__init__(*args, **kwargs)
         self.set_timeout_millis(10000)
+
+    @staticmethod
+    def test_device_turn_off(endpoint):
+        d = TestDeviceProxy(endpoint)
+        try:
+            d.Off()
+        except Exception as e:
+            """Failing to turn Off devices should not raise errors here"""
+            print(f"Failed to turn device off in teardown {e}")
+
+            """Wait for 1 second to prevent propagating reconnection errors"""
+            time.sleep(1)
diff --git a/tangostationcontrol/tangostationcontrol/integration_test/devices/test_device_recv.py b/tangostationcontrol/tangostationcontrol/integration_test/devices/test_device_recv.py
index 6d9353936..bbb5cab66 100644
--- a/tangostationcontrol/tangostationcontrol/integration_test/devices/test_device_recv.py
+++ b/tangostationcontrol/tangostationcontrol/integration_test/devices/test_device_recv.py
@@ -7,6 +7,8 @@
 # Distributed under the terms of the APACHE license.
 # See LICENSE.txt for more info.
 
+import time
+
 from tango._tango import DevState
 
 from tangostationcontrol.integration_test.device_proxy import TestDeviceProxy
@@ -18,15 +20,7 @@ class TestDeviceRECV(base.IntegrationTestCase):
     def setUp(self):
         super(TestDeviceRECV, self).setUp()
 
-    def tearDown(self):
-        """Turn device Off in teardown to prevent blocking tests"""
-        d = TestDeviceProxy("STAT/RECV/1")
-
-        try:
-            d.Off()
-        except Exception as e:
-            """Failing to turn Off devices should not raise errors here"""
-            print(f"Failed to turn device off in teardown {e}")
+        self.addCleanup(TestDeviceProxy.test_device_turn_off, "STAT/RECV/1")
 
     def test_device_proxy_recv(self):
         """Test if we can successfully create a DeviceProxy and fetch state"""
diff --git a/tangostationcontrol/tangostationcontrol/integration_test/devices/test_device_sdp.py b/tangostationcontrol/tangostationcontrol/integration_test/devices/test_device_sdp.py
index f12fc0ae4..669b64ae1 100644
--- a/tangostationcontrol/tangostationcontrol/integration_test/devices/test_device_sdp.py
+++ b/tangostationcontrol/tangostationcontrol/integration_test/devices/test_device_sdp.py
@@ -7,7 +7,8 @@
 # Distributed under the terms of the APACHE license.
 # See LICENSE.txt for more info.
 
-from tango import DeviceProxy
+import time
+
 from tango._tango import DevState
 
 from tangostationcontrol.integration_test.device_proxy import TestDeviceProxy
@@ -20,15 +21,7 @@ class TestDeviceSDP(base.IntegrationTestCase):
         """Intentionally recreate the device object in each test"""
         super(TestDeviceSDP, self).setUp()
 
-    def tearDown(self):
-        """Turn device Off in teardown to prevent blocking tests"""
-        d = TestDeviceProxy("STAT/SDP/1")
-
-        try:
-            d.Off()
-        except Exception as e:
-            """Failing to turn Off devices should not raise errors here"""
-            print(f"Failed to turn device off in teardown {e}")
+        self.addCleanup(TestDeviceProxy.test_device_turn_off, "STAT/SDP/1")
 
     def test_device_proxy_sdp(self):
         """Test if we can successfully create a DeviceProxy and fetch state"""
diff --git a/tangostationcontrol/tangostationcontrol/integration_test/devices/test_device_sst.py b/tangostationcontrol/tangostationcontrol/integration_test/devices/test_device_sst.py
index 210dc93de..0dc4579d7 100644
--- a/tangostationcontrol/tangostationcontrol/integration_test/devices/test_device_sst.py
+++ b/tangostationcontrol/tangostationcontrol/integration_test/devices/test_device_sst.py
@@ -22,15 +22,7 @@ class TestDeviceSST(base.IntegrationTestCase):
         """Intentionally recreate the device object in each test"""
         super(TestDeviceSST, self).setUp()
 
-    def tearDown(self):
-        """Turn device Off in teardown to prevent blocking tests"""
-        d = TestDeviceProxy("STAT/SST/1")
-
-        try:
-            d.Off()
-        except Exception as e:
-            """Failing to turn Off devices should not raise errors here"""
-            print(f"Failed to turn device off in teardown {e}")
+        self.addCleanup(TestDeviceProxy.test_device_turn_off, "STAT/SST/1")
 
     def test_device_proxy_sst(self):
         """Test if we can successfully create a DeviceProxy and fetch state"""
diff --git a/tangostationcontrol/tangostationcontrol/integration_test/devices/test_device_unb2.py b/tangostationcontrol/tangostationcontrol/integration_test/devices/test_device_unb2.py
index 4a0382f2a..09c62c5e0 100644
--- a/tangostationcontrol/tangostationcontrol/integration_test/devices/test_device_unb2.py
+++ b/tangostationcontrol/tangostationcontrol/integration_test/devices/test_device_unb2.py
@@ -19,15 +19,7 @@ class TestDeviceUNB2(base.IntegrationTestCase):
         """Intentionally recreate the device object in each test"""
         super(TestDeviceUNB2, self).setUp()
 
-    def tearDown(self):
-        """Turn device Off in teardown to prevent blocking tests"""
-        d = TestDeviceProxy("STAT/UNB2/1")
-
-        try:
-            d.Off()
-        except Exception as e:
-            """Failing to turn Off devices should not raise errors here"""
-            print(f"Failed to turn device off in teardown {e}")
+        self.addCleanup(TestDeviceProxy.test_device_turn_off, "STAT/UNB2/1")
 
     def test_device_proxy_unb2(self):
         """Test if we can successfully create a DeviceProxy and fetch state"""
-- 
GitLab