def test_timer_event(self): """ Test the handling of a timer event. """ from supvisors.context import Context from supvisors.ttypes import AddressStates context = Context(self.supvisors) with patch.object(self.supvisors.zmq.publisher, 'send_address_status') as mocked_send: # test address states excepting RUNNING: nothing happens for state in [x for x in AddressStates._values() if x != AddressStates.RUNNING]: context.on_timer_event() for address in context.addresses.values(): self.assertEqual(AddressStates.UNKNOWN, address.state) self.assertEqual(0, mocked_send.call_count) # test RUNNING address state with recent local_time test_addresses = ['10.0.0.1', '10.0.0.3', '10.0.0.5'] for address_name in test_addresses: address = context.addresses[address_name] address._state = AddressStates.RUNNING address.local_time = time.time() context.on_timer_event() for address_name in test_addresses: self.assertEqual(AddressStates.RUNNING, context.addresses[address_name].state) for address_name in [x for x in context.addresses.keys() if x not in test_addresses]: self.assertEqual(AddressStates.UNKNOWN, context.addresses[address_name].state) self.assertEqual(0, mocked_send.call_count) # test RUNNING address state with one recent local_time and with auto_fence activated address1 = context.addresses['10.0.0.3'] address1.local_time = time.time() - 100 context.on_timer_event() self.assertEqual(AddressStates.ISOLATING, address1.state) for address_name in [x for x in test_addresses if x != '10.0.0.3']: self.assertEqual(AddressStates.RUNNING, context.addresses[address_name].state) for address_name in [x for x in context.addresses.keys() if x not in test_addresses]: self.assertEqual(AddressStates.UNKNOWN, context.addresses[address_name].state) self.assertEqual(call(address1), mocked_send.call_args) # test with one other recent local_time and with auto_fence deactivated with patch.object(self.supvisors.options, 'auto_fence', False): mocked_send.reset_mock() address2 = context.addresses['10.0.0.5'] address2.local_time = time.time() - 100 address3 = context.addresses['10.0.0.1'] address3.local_time = time.time() - 100 context.on_timer_event() self.assertEqual(AddressStates.SILENT, address2.state) self.assertEqual(AddressStates.SILENT, address3.state) self.assertEqual(AddressStates.ISOLATING, address1.state) for address_name in [x for x in context.addresses.keys() if x not in test_addresses]: self.assertEqual(AddressStates.UNKNOWN, context.addresses[address_name].state) self.assertItemsEqual([call(address2), call(address3)], mocked_send.call_args_list)
def setUp(self): """ Create a logger that stores log traces. """ self.supvisors = DummySupvisors() from supvisors.ttypes import AddressStates self.all_states = AddressStates._values()
def setUp(self): """ Create a logger that stores log traces. """ self.supvisors = MockedSupvisors() from supvisors.ttypes import AddressStates self.all_states = AddressStates._values()
def test_timer_event(self): """ Test the handling of a timer event. """ from supvisors.context import Context from supvisors.ttypes import AddressStates context = Context(self.supvisors) with patch.object(self.supvisors.zmq.publisher, 'send_address_status') as mocked_send: # test address states excepting RUNNING: nothing happens for state in [ x for x in AddressStates._values() if x != AddressStates.RUNNING ]: context.on_timer_event() for address in context.addresses.values(): self.assertEqual(AddressStates.UNKNOWN, address.state) self.assertEqual(0, mocked_send.call_count) # test RUNNING address state with recent local_time test_addresses = ['10.0.0.1', '10.0.0.3', '10.0.0.5'] for address_name in test_addresses: address = context.addresses[address_name] address._state = AddressStates.RUNNING address.local_time = time.time() context.on_timer_event() for address_name in test_addresses: self.assertEqual(AddressStates.RUNNING, context.addresses[address_name].state) for address_name in [ x for x in context.addresses.keys() if x not in test_addresses ]: self.assertEqual(AddressStates.UNKNOWN, context.addresses[address_name].state) self.assertEqual(0, mocked_send.call_count) # test RUNNING address state with one recent local_time and with auto_fence activated address1 = context.addresses['10.0.0.3'] address1.local_time = time.time() - 100 context.on_timer_event() self.assertEqual(AddressStates.ISOLATING, address1.state) for address_name in [x for x in test_addresses if x != '10.0.0.3']: self.assertEqual(AddressStates.RUNNING, context.addresses[address_name].state) for address_name in [ x for x in context.addresses.keys() if x not in test_addresses ]: self.assertEqual(AddressStates.UNKNOWN, context.addresses[address_name].state) self.assertEqual(call(address1), mocked_send.call_args) # test with one other recent local_time and with auto_fence deactivated with patch.object(self.supvisors.options, 'auto_fence', False): mocked_send.reset_mock() address2 = context.addresses['10.0.0.5'] address2.local_time = time.time() - 100 address3 = context.addresses['10.0.0.1'] address3.local_time = time.time() - 100 context.on_timer_event() self.assertEqual(AddressStates.SILENT, address2.state) self.assertEqual(AddressStates.SILENT, address3.state) self.assertEqual(AddressStates.ISOLATING, address1.state) for address_name in [ x for x in context.addresses.keys() if x not in test_addresses ]: self.assertEqual(AddressStates.UNKNOWN, context.addresses[address_name].state) self.assertItemsEqual( [call(address2), call(address3)], mocked_send.call_args_list)