def test_event_type_is_registered_on_first_call_only(self): protocol, connecting = self.patch_rpc_methods(side_effect=[{}, {}]) self.addCleanup((yield connecting)) mac_address = factory.make_mac_address() description = factory.make_name("description") event_name = random.choice(list(map_enum(EVENT_TYPES))) event_detail = EVENT_DETAILS[event_name] event_hub = NodeEventHub() # On the first call, the event type is registered before the log is # sent to the region. yield event_hub.logByMAC(event_name, mac_address, description) self.assertThat( protocol.RegisterEventType, MockCalledOnceWith( ANY, name=event_name, description=event_detail.description, level=event_detail.level, ), ) self.assertThat(protocol.SendEventMACAddress, MockCalledOnce()) # Reset RPC call handlers. protocol.RegisterEventType.reset_mock() protocol.SendEventMACAddress.reset_mock() # On the second call, the event type is known to be registered, so the # log is sent to the region immediately. yield event_hub.logByMAC(event_name, mac_address, description) self.assertThat(protocol.RegisterEventType, MockNotCalled()) self.assertThat(protocol.SendEventMACAddress, MockCalledOnce())
def test_updates_cache_if_event_type_not_found(self): protocol, connecting = self.patch_rpc_methods( side_effect=[succeed({}), fail(NoSuchEventType())]) self.addCleanup((yield connecting)) mac_address = factory.make_mac_address() description = factory.make_name("description") event_name = random.choice(list(map_enum(EVENT_TYPES))) event_hub = NodeEventHub() # Fine the first time. yield event_hub.logByMAC(event_name, mac_address, description) # The cache has been populated with the event name. self.assertThat(event_hub._types_registered, Equals({event_name})) # Second time it crashes. with ExpectedException(NoSuchEventType): yield event_hub.logByMAC(event_name, mac_address, description) # The event has been removed from the cache. self.assertThat(event_hub._types_registered, HasLength(0))