def test_event_dicts(): evt = Event(event_id="event_id", topic="topic", evt_type="evt_type", activity="activity", timestamp="timestamp", ref_id="ref_id") assert "event_id" == evt.event_id assert "topic" == evt.topic assert "evt_type" == evt.evt_type assert "activity" == evt.activity assert "timestamp" == evt.timestamp assert "ref_id" == evt.ref_id dict_evt = evt.to_dict() assert "event_id" == dict_evt.get("event_id") assert "topic" == dict_evt.get("topic") assert "evt_type" == dict_evt.get("evt_type") assert "activity" == dict_evt.get("activity") assert "timestamp" == dict_evt.get("timestamp") assert "ref_id" == dict_evt.get("ref_id") from_dict_evt = Event.from_dict(dict_evt) assert "event_id" == from_dict_evt.event_id assert "topic" == from_dict_evt.topic assert "evt_type" == from_dict_evt.evt_type assert "activity" == from_dict_evt.activity assert "timestamp" == from_dict_evt.timestamp assert "ref_id" == from_dict_evt.ref_id
def test_handle_event(): httpretty.register_uri(httpretty.POST, "http://example.com", body='{"origin": "127.0.0.1"}') with RedisRunnerContext(): subscriber = Subscriber(stream=uuid4().hex, subscriber_name=uuid4().hex, topics=["system"], evt_types=["SYS"], activities=["init", "verb"], protocol=Protocol.HTTP, endpoint="http://example.com") event = Event( event_id="test-event-", topic="system", evt_type="SYS", activity="init", ref_id="00", ) ok = subscriber.handle_event(event, "consumer_group", "consumer_id") assert ok called = httpretty.last_request() body = json.loads(called.body) assert body["stream"] == subscriber.stream assert body["event"]["event_id"] == event.event_id assert body["event"]["topic"] == event.topic assert body["event"]["evt_type"] == event.evt_type assert body["event"]["activity"] == event.activity assert body["event"]["ref_id"] == event.ref_id assert body["consumer_group"] == "consumer_group" assert body["consumer_id"] == "consumer_id" assert body["subscriber_name"] == subscriber.subscriber_name
def test_send_event(): with RedisRunnerContext(): client = ProtonPack.redis_connect() test_stream_name = uuid4().hex ProtonPack.create_stream(test_stream_name) length = client.xlen(test_stream_name) assert length == 1 evt = Event(topic="test", evt_type="TEST", activity="test_send_event") ProtonPack.send_event(test_stream_name, evt) length = client.xlen(test_stream_name) assert length == 2
def test_acknowledge_events(): with RedisRunnerContext(): groupname = "group" consumername = "consumer" test_stream_name = uuid4().hex evt = Event(topic="test", evt_type="TEST", activity="test_send_event") evt_id = ProtonPack.send_event(test_stream_name, evt) assert evt_id is not None ProtonPack.create_consumer_group(test_stream_name, groupname) ProtonPack.poll_events(test_stream_name, groupname, consumername, 100, 10000) ack = ProtonPack.acknowledge_events(test_stream_name, groupname, evt_id) assert ack == 1
def get_stream_info(stream): if request.method == "GET": info = ProtonPack.stream_info(stream) return make_response( json_dumps({ "status": "ok", "stream": stream, "info": info }), 200) elif request.method == "POST": raw = request.get_json(force=True) if isinstance(raw, collections.Mapping): event = Event.from_dict(raw) evt_id = ProtonPack.send_event(stream, event) raw["event_id"] = evt_id app.logger.debug({"action": "event_posted", "event": raw}) return make_response( json_dumps({ "status": "ok", "stream": stream, "events": [evt_id] }), 200) else: evt_ids = [] for evt in raw: event = Event.from_dict(evt) evt_id = ProtonPack.send_event(stream, event) evt["event_id"] = evt_id evt_ids.append(evt_id) app.logger.debug({"action": "event_posted", "event": evt}) return make_response( json_dumps({ "status": "ok", "stream": stream, "events": evt_ids }), 200)
def test_workers_unite(): httpretty.register_uri( httpretty.POST, "http://example.com", body='{"origin": "127.0.0.1"}', status=200, ) with RedisRunnerContext(): # -- create a subscriber that is mocked with httpretty subscriber = Subscriber(stream=uuid4().hex, subscriber_name=uuid4().hex, topics=["system"], evt_types=["SYS"], activities=["init", "verb"], protocol=Protocol.HTTP, endpoint="http://example.com") ok = SubscriberManager.put_subscriber(subscriber) assert ok # -- prepare an event to send event = Event( topic="system", evt_type="SYS", activity="init", ref_id="00", ) ok = ProtonPack.send_event(subscriber.stream, event) assert ok # -- start up a worker and have it consume the stream gb = GhostBuster(subscriber.stream, "consumer_group", "consumer_id") gb.start(False) # -- assert the handler uri is called called = httpretty.last_request() print(dir(called)) assert called.body is not None assert called.body != "" body = json.loads(called.body) assert body["stream"] == subscriber.stream assert body["event"]["topic"] == event.topic assert body["event"]["evt_type"] == event.evt_type assert body["event"]["activity"] == event.activity assert body["event"]["ref_id"] == event.ref_id assert body["consumer_group"] == "consumer_group" assert body["consumer_id"] == "consumer_id" assert body["subscriber_name"] == subscriber.subscriber_name
def test_handle_event_mismatch_activities(): httpretty.register_uri(httpretty.POST, "http://example.com") with RedisRunnerContext(): subscriber = Subscriber(stream=uuid4().hex, subscriber_name=uuid4().hex, topics=["system"], evt_types=["SYS"], activities=["init", "verb"], protocol=Protocol.HTTP, endpoint="http://example.com") event = Event( event_id="test-event-", topic="system", evt_type="SYS", activity="write", ref_id="00", ) ok = subscriber.matches(event) assert ok is False assert httpretty.has_request() is False