def create_event(path, topic, string_payload): payload = json.loads(string_payload) app = app_loader.load(path) event = Event(topic=topic, payload=payload) connection = data_access.connect(app.dsn) with data_access.cursor(connection) as cursor: return data_access.create_event(cursor, event)
def test_run_processes_due_to_interval(): global foo_event_id, bar_event_id connection = data_access.connect(DSN) with data_access.cursor(connection) as cursor: foo_event_id = data_access.create_event(cursor, Event(topic=FOO_TOPIC)).id bar_event_id = data_access.create_event(cursor, Event(topic=BAR_TOPIC)).id app = App(DSN, CHANNEL, interval=1) @app.register(FOO_TOPIC) def handler(context): pass now = time.time() def continue_for_two_seconds(app): return time.time() < now + 2 app.run(should_continue=continue_for_two_seconds) with data_access.cursor(app.connection) as cursor: assert (data_access.get_event_by_id( cursor, foo_event_id).status == constants.PROCESSED) assert (data_access.get_event_by_id( cursor, bar_event_id).status == constants.PENDING)
def slow_running(): local_connection = data_access.connect(DSN) with data_access.cursor(local_connection) as cursor: event = data_access.get_next_event(cursor, [topic]) time.sleep(0.5) if event: data_access.mark_event_processed(cursor, event.id)
def test_create_and_get_event_with_process_after(): connection = data_access.connect(DSN) event = Event(topic="foo", process_after=timestamps.now() + timedelta(seconds=10)) with data_access.cursor(connection) as cursor: created = data_access.create_event(cursor, event) with data_access.cursor(connection) as cursor: retrieved = data_access.get_event_by_id(cursor, created.id) assert retrieved.process_after == event.process_after
def test_create_and_get_event_with_payload(payload): connection = data_access.connect(DSN) event = Event(topic="foo", payload=payload) with data_access.cursor(connection) as cursor: created = data_access.create_event(cursor, event) with data_access.cursor(connection) as cursor: retrieved = data_access.get_event_by_id(cursor, created.id) assert retrieved.payload == payload
def test_create_and_get_event_without_payload(): connection = data_access.connect(DSN) event = Event(topic="foo") with data_access.cursor(connection) as cursor: created = data_access.create_event(cursor, event) with data_access.cursor(connection) as cursor: retrieved = data_access.get_event_by_id(cursor, created.id) assert created == retrieved assert created.status == constants.PENDING
def test_get_next_event( first_process_after, second_process_after, expected_first_status, expected_second_status, ): connection = data_access.connect(DSN) topic = "foo" with data_access.cursor(connection) as cursor: first = data_access.create_event( cursor, Event(topic=topic, process_after=first_process_after) ) second = data_access.create_event( cursor, Event(topic=topic, process_after=second_process_after) ) time.sleep(0.1) def slow_running(): local_connection = data_access.connect(DSN) with data_access.cursor(local_connection) as cursor: event = data_access.get_next_event(cursor, [topic]) time.sleep(0.5) if event: data_access.mark_event_processed(cursor, event.id) def fast_running(): local_connection = data_access.connect(DSN) with data_access.cursor(local_connection) as cursor: event = data_access.get_next_event(cursor, [topic]) if event: data_access.mark_event_processed(cursor, event.id) slow_thread = Thread(target=slow_running) slow_thread.start() time.sleep(0.1) fast_thread = Thread(target=fast_running) fast_thread.start() slow_thread.join() fast_thread.join() with data_access.cursor(connection) as cursor: retrieved_first = data_access.get_event_by_id(cursor, first.id) assert retrieved_first.status == expected_first_status with data_access.cursor(connection) as cursor: retrieved_second = data_access.get_event_by_id(cursor, second.id) assert retrieved_second.status == expected_second_status
def send_notification(): global foo_event_id, bar_event_id time.sleep(1) connection = data_access.connect(DSN) with data_access.cursor(connection) as cursor: foo_event_id = data_access.create_event(cursor, Event(topic=FOO_TOPIC)).id bar_event_id = data_access.create_event(cursor, Event(topic=BAR_TOPIC)).id data_access.notify(cursor, CHANNEL)
def create_event_stream(handler_error=False): dsn = "dbname=test user=test password=test host=localhost" connection = data_access.connect(dsn) def foo(event): if handler_error: raise Exception("Deliberately thrown from handler") QUEUE.put("item") time.sleep(HANDLER_SLEEP_TIME) handlers = dict(test=foo) return EventStream(connection, handlers)
def test_mark_event_processed(): connection = data_access.connect(DSN) event = Event(topic="foo") with data_access.cursor(connection) as cursor: created = data_access.create_event(cursor, event) assert created.status == constants.PENDING assert created.processed_at == None with data_access.cursor(connection) as cursor: data_access.mark_event_processed(cursor, created.id) with data_access.cursor(connection) as cursor: retrieved = data_access.get_event_by_id(cursor, created.id) assert retrieved.status == constants.PROCESSED assert retrieved.processed_at is not None
def test_listen_notify_unlisten(): connection = data_access.connect(DSN) channel = "foo" with data_access.cursor(connection) as cursor: data_access.listen(cursor, channel) with data_access.cursor(connection) as cursor: data_access.notify(cursor, channel) connection.poll() assert connection.notifies while connection.notifies: connection.notifies.pop() with data_access.cursor(connection) as cursor: data_access.unlisten(cursor, channel) with data_access.cursor(connection) as cursor: data_access.notify(cursor, channel) connection.poll() assert not connection.notifies
def clear_events(): connection = data_access.connect(DSN) with data_access.cursor(connection) as cursor: data_access.truncate_events(cursor)
def connection(): connection = data_access.connect(DSN) yield connection connection.close()
def connect(self): self.connection = data_access.connect(self.dsn)