예제 #1
0
파일: cli.py 프로젝트: peajayni/pgevents
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)
예제 #2
0
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)
예제 #3
0
 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)
예제 #4
0
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
예제 #5
0
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
예제 #6
0
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
예제 #7
0
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
예제 #8
0
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)
예제 #9
0
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)
예제 #10
0
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
예제 #11
0
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
예제 #12
0
def clear_events():
    connection = data_access.connect(DSN)
    with data_access.cursor(connection) as cursor:
        data_access.truncate_events(cursor)
예제 #13
0
def connection():
    connection = data_access.connect(DSN)
    yield connection
    connection.close()
예제 #14
0
파일: app.py 프로젝트: peajayni/pgevents
 def connect(self):
     self.connection = data_access.connect(self.dsn)