def test_push(self): """Test if PushConsumer works properly.""" with self.app.test_request_context(): user_hub = EventHub("TestPush", self.celery) user_hub_id = user_hub.hub_id push_function = PushConsumer(self.backend, user_hub_id) # The notifier that push notifications to the client via SSE sse_notifier = self.notifications.sse_notifier_for(user_hub_id) push_function.consume(self.event_json) # Popping subscribe message. Somehow, if the option # ignore_subscribe_messages is True, the other messages # are not detected. propagated_messages = sse_notifier.backend.listen() message = next(propagated_messages) # Getting expected message and checking it with the sent one message = next(propagated_messages) assert message['data'].decode("utf-8") == self.event_json
def create_hub(self, hub_alias): """Create an EventHub to aggregate certain types of events.""" hub = EventHub(hub_alias, self.celery) self._hubs[hub.hub_id] = hub return hub
def setUp(self): super(EventHubAndFiltersTest, self).setUp() self.event_hub = EventHub("TestFilters", self.celery) self.event_hub_id = self.event_hub.hub_id
class EventHubAndFiltersTest(NotificationsFlaskTestCase): def setUp(self): super(EventHubAndFiltersTest, self).setUp() self.event_hub = EventHub("TestFilters", self.celery) self.event_hub_id = self.event_hub.hub_id def test_register_consumer(self): """ The client can register consumers using a decorator or calling directly :method register_consumer:. The client also can deregister consumers. """ @self.event_hub.register_consumer def write_to_file(event_json, *args, **kwargs): f = open("events.log", "a+w") f.write(event_json) push_consumer = PushConsumer(self.backend, self.event_hub_id) self.event_hub.register_consumer(push_consumer) # The previous consumers are indeed registered assert self.event_hub.is_registered(push_consumer) is True assert self.event_hub.is_registered(write_to_file) is True # Registering the same PushConsumer as a sequence of consumers repeated_push_consumer = PushConsumer(self.backend, self.event_hub_id) self.event_hub.register_consumer(repeated_push_consumer) # The previous operation has no effect as the consumer has # been previously registered registered = list(self.event_hub.registered_consumers) assert len(list(filter(self.event_hub.is_registered, registered))) == 2 # Deregister previous consumers for consumer in [write_to_file, push_consumer]: self.event_hub.deregister_consumer(consumer) assert len(self.event_hub.registered_consumers) == 0 def test_and_filters(self): f1 = WithEventType("user") f2 = WithSender("system") f3 = WithRecipients(["jvican"]) f1f2 = f1 & f2 f1f2f3 = f1 & f2 & f3 assert f1f2f3(self.event) is True assert f1(self.event) is True self.event["event_type"] = "info" assert f1(self.event) is False assert f1f2(self.event) is False assert f1f2f3(self.event) is False assert f2(self.event) is True self.event["sender"] = "antisystem" assert f2(self.event) is False assert f1f2f3(self.event) is False assert f3(self.event) is True self.event["recipients"] = ["johndoe"] assert f3(self.event) is False assert f1f2f3(self.event) is False def test_or_filters(self): f1 = BeforeDate(self.tomorrow) f2 = WithId("1234") f3 = Not(Expired()) f1f2f3 = f1 | f2 | f3 assert f1f2f3(self.event) is True assert f1(self.event) is True self.event["timestamp"] = self.next_to_tomorrow_tm assert f1(self.event) is False assert f1f2f3(self.event) is True assert f2(self.event) is True self.event["event_id"] = "123" assert f2(self.event) is False assert f1f2f3(self.event) is True assert f3(self.event) is True self.event["expiration_datetime"] = datetime.now() assert f3(self.event) is False assert f1f2f3(self.event) is False def test_xor_filters(self): # f1 is false in the beginning f1 = AfterDate(self.next_to_tomorrow) f2 = WithId("1234") f1f2 = f1 ^ f2 assert f1f2(self.event) is True self.event["timestamp"] = self.next_to_next_to_tomorrow_tm assert f1f2(self.event) is False self.event["event_id"] = "123" assert f1f2(self.event) is True self.event["timestamp"] = self.next_to_tomorrow_tm assert f1f2(self.event) is False