From 1980992828b42dc7c4ae66862b70689c65c259e9 Mon Sep 17 00:00:00 2001 From: srobert Date: Wed, 9 Nov 2016 14:36:02 -0500 Subject: [PATCH] Taking executor out of the context --- virtualpdu/core.py | 4 ++-- virtualpdu/tests/integration/test_core_integration.py | 6 +++++- virtualpdu/tests/unit/test_core.py | 6 +++--- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/virtualpdu/core.py b/virtualpdu/core.py index 02c58c4..0c25ad2 100644 --- a/virtualpdu/core.py +++ b/virtualpdu/core.py @@ -26,6 +26,7 @@ class Core(object): self.store = store self.default_state = default_state self.logger = logging.getLogger(__name__) + self.executor = ThreadPoolExecutor(max_workers=1) def pdu_outlet_state_changed(self, pdu, outlet, state): self.store[(pdu, outlet)] = state @@ -60,8 +61,7 @@ class Core(object): self.logger.error("Unknown power state: {}".format(state)) return - with ThreadPoolExecutor(max_workers=1) as executor: - executor.submit(switch_power) + self.executor.submit(switch_power) def get_pdu_outlet_state(self, pdu, outlet): try: diff --git a/virtualpdu/tests/integration/test_core_integration.py b/virtualpdu/tests/integration/test_core_integration.py index a86e06d..d8193a2 100644 --- a/virtualpdu/tests/integration/test_core_integration.py +++ b/virtualpdu/tests/integration/test_core_integration.py @@ -13,9 +13,9 @@ # limitations under the License. import random +import time from pysnmp.entity.rfc3413.oneliner import cmdgen - from virtualpdu import core from virtualpdu import drivers from virtualpdu.drivers import libvirt_driver @@ -67,12 +67,16 @@ class TestCoreIntegration(base.TestCase): snmp_client_.set( self.outlet_oid, apc_rackpdu.APCRackPDUOutletControl.states.IMMEDIATE_OFF) + + time.sleep(0.1) self.assertEqual(drivers.POWER_OFF, self.driver.get_power_state('test')) snmp_client_.set( self.outlet_oid, apc_rackpdu.APCRackPDUOutletControl.states.IMMEDIATE_ON) + + time.sleep(0.1) self.assertEqual(drivers.POWER_ON, self.driver.get_power_state('test')) diff --git a/virtualpdu/tests/unit/test_core.py b/virtualpdu/tests/unit/test_core.py index 56ca421..042ed5c 100644 --- a/virtualpdu/tests/unit/test_core.py +++ b/virtualpdu/tests/unit/test_core.py @@ -36,7 +36,7 @@ class TestCore(base.TestCase): self.core.pdu_outlet_state_changed(pdu='my_pdu', outlet=1, state=core.POWER_OFF) - time.sleep(0) + time.sleep(0.1) self.driver_mock.power_off.assert_called_with('server_one') def test_pdu_outlet_state_changed_machine_not_in_mapping_noop(self): @@ -51,14 +51,14 @@ class TestCore(base.TestCase): self.core.pdu_outlet_state_changed(pdu='my_pdu', outlet=1, state=core.POWER_ON) - time.sleep(0) + time.sleep(0.1) self.driver_mock.power_on.assert_called_with('server_one') def test_pdu_outlet_state_changed_on_reboot(self): self.core.pdu_outlet_state_changed(pdu='my_pdu', outlet=1, state=core.REBOOT) - time.sleep(0) + time.sleep(0.1) self.driver_mock.assert_has_calls([mock.call.power_off('server_one'), mock.call.power_on('server_one')])