def test_event_type_is_registered_on_first_call_only(self): protocol, connecting = self.patch_rpc_methods( side_effect=[succeed({}), succeed({})]) self.addCleanup((yield connecting)) system_id = factory.make_name("system_id") 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.logByID(event_name, system_id, description) self.assertThat( protocol.RegisterEventType, MockCalledOnceWith( ANY, name=event_name, description=event_detail.description, level=event_detail.level, ), ) self.assertThat(protocol.SendEvent, MockCalledOnce()) # Reset RPC call handlers. protocol.RegisterEventType.reset_mock() protocol.SendEvent.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.logByID(event_name, system_id, description) self.assertThat(protocol.RegisterEventType, MockNotCalled()) self.assertThat(protocol.SendEvent, 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)) system_id = factory.make_name("system_id") description = factory.make_name("description") event_name = random.choice(list(map_enum(EVENT_TYPES))) event_hub = NodeEventHub() # Fine the first time. yield event_hub.logByID(event_name, system_id, 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.logByID(event_name, system_id, description) # The event has been removed from the cache. self.assertThat(event_hub._types_registered, HasLength(0))