示例#1
0
async def test_async_comprehension(event_loop):
    def embiggen(e):
        data = e.json()
        data["Height"] *= 10
        data["Distance"] *= 10

    stream_name = str(uuid.uuid4())

    async with connect(
        loop=event_loop,
        username="******",
        password="******",
        name="comprehensions",
    ) as c:

        await given_a_stream_with_three_events(c, stream_name)

        jumps = (
            e.event
            async for e in c.iter(stream_name, batch_size=2)
            if e.type == "pony_jumped"
        )
        big_jumps = (embiggen(e) async for e in jumps)

        events_read = 0

        async for event in big_jumps:
            print(event)
            events_read += 1

        assert events_read == 3
示例#2
0
    async def aggregate_fn(self):
        log.trace("enter Aggregate.aggregate_fn()")
        _loop = asyncio.get_event_loop()
        async with connect(host=EVENT_STORE_URL,
                           port=EVENT_STORE_TCP_PORT,
                           username=EVENT_STORE_USER,
                           password=EVENT_STORE_PASS,
                           loop=_loop) as c:
            await c.connect()
            try:
                await self.create_subscription(c)
            except exceptions.SubscriptionCreationFailed as e:
                if e.message.find("already exists"):
                    log.info(
                        f"{self.subscription_name} {self.watched_stream_name} subscription found."
                    )
                else:
                    log.exception(e)
            dialogue_stream = await c.connect_subscription(
                self.subscription_name, self.watched_stream_name)

            async for event in dialogue_stream.events:
                event_obj = json.loads(event.event.data)
                log.debug("aggregate_fn() responding to: %s" %
                          json.dumps(event_obj))
                try:
                    await self.post_to_aggregate_stream(
                        str(event_obj["event_id"]), await
                        self.update_backend(event_obj), event.type)
                    await dialogue_stream.ack(event)
                except KeyError as e:
                    log.error(f"key missing from event: {e}")
                except Exception as e:
                    log.exception(e)
        log.trace("exit Aggregate.aggregate_fn()")
示例#3
0
async def test_single_event_roundtrip(event_loop):

    stream_name = str(uuid.uuid4())

    try:
        async with connect(loop=event_loop) as c:
            print("1")
            await c.publish_event(stream_name,
                                  "thing_happened",
                                  body={
                                      "thing": 1,
                                      "happening": True
                                  })
            print("1")

            result = await c.get_event(stream_name, 0)

            print("1")
            assert isinstance(result, messages.Event)
            assert result.event.type == "thing_happened"

            print("1")
            data = result.event.json()
            assert data["thing"] == 1
            assert data["happening"] is True
    except Exception as e:
        print(e)
        assert False
 async def __run_writer(self, stream_name, stream_type, data):
     async with photonpump.connect(
             host=self.config['host'],
             port=self.config['port'],
             username=self.config['username'],
             password=self.config['password']) as conn:
         await self.__write_event(conn, stream_name, stream_type, data)
 async def __run_reader(self, stream_name):
     async with photonpump.connect(
             host=self.config['host'],
             port=self.config['port'],
             username=self.config['username'],
             password=self.config['password']) as conn:
         return await self.__read_an_event(conn, stream_name)
示例#6
0
async def test_three_events_publish(event_loop):
    stream_name = str(uuid.uuid4())
    async with connect(username="******", password="******") as c:
        result = await c.publish(
            stream_name,
            [
                messages.NewEvent(
                    "pony_jumped",
                    data={
                        "Pony": "Derpy Hooves",
                        "Height": 10,
                        "Distance": 13
                    },
                ),
                messages.NewEvent(
                    "pony_jumped",
                    data={
                        "Pony": "Sparkly Hooves",
                        "Height": 4,
                        "Distance": 9
                    },
                ),
                messages.NewEvent(
                    "pony_jumped",
                    data={
                        "Pony": "Unlikely Hooves",
                        "Height": 73,
                        "Distance": 912
                    },
                ),
            ],
        )
        assert result.first_event_number == 0
        assert result.last_event_number == 2
async def run(cfg):
    async with photonpump.connect(host=cfg['host'],
                                  port=cfg['port'],
                                  username=cfg['username'],
                                  password=cfg['password']) as conn:
        logging.info('Connected to EventStore')
        actions = []

        for stream in cfg['streams']:

            try:
                subscription = await setup_subscription(
                    conn, stream['name'], cfg['subscription'])
                logging.info("Created subscriptions %s", subscription)
            except Exception as exn:
                logging.error(
                    "Failed while connecting to subscriptions: %s \n sleeping before exit",
                    exn,
                    exc_info=True)

                return

            actions.append(events_actor(subscription, stream['events']))

        return await asyncio.gather(*actions)
示例#8
0
async def responder_fn(_slack_client):
    _loop = asyncio.get_event_loop()
    async with connect(host=EVENT_STORE_URL,
                       port=EVENT_STORE_TCP_PORT,
                       username=EVENT_STORE_USER,
                       password=EVENT_STORE_PASS,
                       loop=_loop) as c:
        await c.connect()
        try:
            await create_subscription("slackposter", "aggregate", c)
        except exceptions.SubscriptionCreationFailed as e:
            if e.message.find("'slackposter' already exists."):
                log.info("Slackposter aggregate subscription found.")
            else:
                raise e
        responses_to_send = await c.connect_subscription(
            "slackposter", "aggregate")
        async for event in responses_to_send.events:
            if event.type == "response_created":
                event_obj = json.loads(event.event.data)
                log.debug("responder_fn() responding to: %s" %
                          json.dumps(event_obj))
                dialogue_object = await fetch_dialogue_object(event.event.data)
                try:
                    await slackclient_call(_slack_client,
                                           dialogue_object["response"],
                                           dialogue_object["event"]["channel"],
                                           dialogue_object["event"]["ts"])
                    await post_to_dialogue_stream(dialogue_object["event_id"])
                    await responses_to_send.ack(event)
                except Exception as e:
                    log.exception(e)
            else:
                await responses_to_send.ack(event)
示例#9
0
async def test_iter_from_missing_stream(event_loop):

    async with connect(loop=event_loop) as c:
        try:
            [e async for e in c.iter("my-stream-that-isnt-a-stream")]
            assert False
        except Exception as e:
            assert isinstance(e, exceptions.StreamNotFound)
示例#10
0
async def test_missing_stream(event_loop):
    stream_name = str(uuid.uuid4())
    async with connect(
        loop=event_loop, username="******", password="******"
    ) as c:
        with pytest.raises(exceptions.StreamNotFound) as exc:
            await c.get_event(stream_name, 0)
        assert exc.value.stream == stream_name
示例#11
0
async def test_publish_raises_exception_if_not_authenticated(event_loop):
    stream_name = str(uuid.uuid4())

    async with connect() as conn:
        with pytest.raises(exceptions.AccessDenied):
            await conn.publish(
                stream_name,
                [messages.NewEvent("pony_jumped", data={})],
            )
示例#12
0
async def test_connect_logs_deprecation_warning_when_used_with_loop_parameter(
    event_loop, ):
    with pytest.warns(DeprecationWarning) as record:
        async with connect(loop=event_loop) as conn:
            await conn.ping(conversation_id=uuid.uuid4())

    assert len(record) == 1
    assert "The loop parameter has been deprecated" in record[0].message.args[
        0]
示例#13
0
async def test_subscribe_to(event_loop):

    async with connect(username="******", password="******") as conn:
        stream_name = str(uuid.uuid4())
        event_id = uuid.uuid4()

        await conn.publish_event(stream_name, "my-event-type", id=event_id)

        subscription = await conn.subscribe_to(stream_name, start_from=0)

        event = await subscription.events.anext()
        assert event.received_event.id == event_id
示例#14
0
async def test_connect_subscription(event_loop):

    async with connect(username="******", password="******", loop=event_loop) as conn:
        subscription_name = str(uuid.uuid4())
        stream_name = str(uuid.uuid4())
        event_id = uuid.uuid4()

        await conn.create_subscription(subscription_name, stream_name, start_from=-1)
        subscription = await conn.connect_subscription(subscription_name, stream_name)
        await conn.publish_event(stream_name, "my-event-type", id=event_id)

        event = await subscription.events.anext()
        assert event.original_event_id == event_id
示例#15
0
async def test_read_with_max_count_and_from_event(event_loop):
    stream_name = str(uuid.uuid4())
    async with connect(username="******", password="******") as c:
        await given_a_stream_with_three_events(c, stream_name)

        result = await c.get(stream_name, max_count=1, from_event=2)
        assert isinstance(result, list)
        assert len(result) == 1

        event = result[0]
        assert event.type == "pony_jumped"
        data = event.json()
        assert data["Pony"] == "Unlikely Hooves"
示例#16
0
async def test_read_multiple(event_loop):
    stream_name = str(uuid.uuid4())
    async with connect(username="******", password="******") as c:
        await given_a_stream_with_three_events(c, stream_name)

        result = await c.get(stream_name)
        assert isinstance(result, messages.StreamSlice)
        assert len(result) == 3

        event = result[1]
        assert event.type == "pony_jumped"
        data = event.json()
        assert data["Pony"] == "Sparkly Hooves"
        assert data["Height"] == 4
示例#17
0
async def test_single_event_publish(event_loop):
    stream_name = str(uuid.uuid4())
    async with connect(username="******", password="******") as conn:
        result = await conn.publish_event(
            stream_name,
            "testEvent",
            id=uuid.uuid4(),
            body={
                "greeting": "hello",
                "target": "world"
            },
        )
        assert isinstance(result, messages_pb2.WriteEventsCompleted)
        assert result.first_event_number == 0
示例#18
0
async def test_iterall(event_loop):
    async with connect(loop=event_loop,
                       name="iter_all",
                       username="******",
                       password="******") as c:
        stream_name = str(uuid.uuid4())
        await given_a_stream_with_three_events(c, stream_name)

        events_read = 0

        async for event in c.iter_all(batch_size=2):
            events_read += 1

        assert events_read >= 3
示例#19
0
async def test_setting_retry_policy(event_loop):
    class silly_retry_policy(DiscoveryRetryPolicy):
        def __init__(self):
            super().__init__()

        def should_retry(self, _):
            pass

        async def wait(self, seed):
            pass

    expected_policy = silly_retry_policy()

    async with connect(retry_policy=expected_policy) as client:
        assert client.connector.discovery.retry_policy == expected_policy
示例#20
0
async def test_iterall(event_loop):
    async with connect(
        username="******",  # iter_all aggregates all streams so it needs systemwide-read perms
        password="******",
        name="iter_all",
    ) as c:
        stream_name = str(uuid.uuid4())
        await given_two_streams_with_two_events(c, stream_name)

        events_read = 0

        async for _ in c.iter_all(batch_size=2):
            events_read += 1

        assert events_read >= 3
示例#21
0
async def test_single_event_roundtrip(event_loop):
    stream_name = str(uuid.uuid4())
    async with connect(username="******", password="******") as c:
        await c.publish_event(
            stream_name, "thing_happened", body={"thing": 1, "happening": True}
        )

        result = await c.get_event(stream_name, 0)

        assert isinstance(result, messages.Event)
        assert result.event.type == "thing_happened"

        data = result.event.json()
        assert data["thing"] == 1
        assert data["happening"] is True
示例#22
0
async def test_missing_stream(event_loop):

    stream_name = str(uuid.uuid4())

    async with connect(loop=event_loop) as c:

        exc = None

        try:
            await c.get_event(stream_name, 0)
        except Exception as e:
            exc = e

        assert isinstance(exc, exceptions.StreamNotFound)
        assert exc.stream == stream_name
示例#23
0
async def test_readall(event_loop):
    async with connect(loop=event_loop,
                       name="read_all",
                       username="******",
                       password="******") as c:
        stream_name = str(uuid.uuid4())
        await given_a_stream_with_three_events(c, stream_name)

        events_read = 0

        for event in await c.get_all(max_count=3):
            print(event)
            events_read += 1

        assert events_read == 3
示例#24
0
async def test_a_large_event(event_loop):
    stream_name = str(uuid.uuid4())
    async with connect(username="******", password="******") as c:
        write_result = await c.publish(
            stream_name,
            [
                messages.NewEvent("big_json", data=data.CHAIR),
                messages.NewEvent("big_json", data=data.CHAIR),
                messages.NewEvent("big_json", data=data.CHAIR),
            ],
        )
        assert write_result.first_event_number == 0
        read_result = await c.get(stream_name, 0)
        print(read_result)
        assert read_result[0].event.type == "big_json"
示例#25
0
async def test_streaming_read(event_loop):

    stream_name = str(uuid.uuid4())

    async with connect(loop=event_loop, name="streaming-read") as c:

        await given_a_stream_with_three_events(c, stream_name)

        events_read = 0

        async for event in c.iter(stream_name, batch_size=1):
            logging.info("Handling event!")
            events_read += 1
            assert event.type == "pony_jumped"

        assert events_read == 3
示例#26
0
async def test_readall(event_loop):
    async with connect(
        username="******",  # get_all aggregates all streams so it needs systemwide-read perms
        password="******",
        name="iter_all",
    ) as c:
        stream_name = str(uuid.uuid4())
        await given_two_streams_with_two_events(c, stream_name)

        events_read = 0

        for event in await c.get_all(max_count=4):
            print(event)
            events_read += 1

        assert events_read == 4
async def import_stream():
    # Use DI to initialize in real life
    database_name = f"../../../../{DATABASE_NAME}"
    ledger_repository = SqliteLedgerRepository(database_name=database_name)

    repository = SqliteDeclineViewRepository(database_name=database_name)
    projection = DeclineProjection(repository)

    projector = DeclineProjector(projection)

    stream_service = EventStoreStreamService(
        photonpump.connect(username=EVENT_STORE_USERNAME,
                           password=EVENT_STORE_PASSWORD))
    projectionist = DeclineProjectionist(projector=projector,
                                         stream_service=stream_service,
                                         ledger_repository=ledger_repository,
                                         ledger_name=DECLINE_PROJECTION)
    await projectionist.start()
示例#28
0
def main():
  # init connection
  loop = asyncio.get_event_loop()
  async with connect(loop=loop) as c:
    await c.ping()
    # init aggregate store
    store = infrastructure.aggregate_store(connection=c)

  print(command.screening.v1.schedule_screening(
    movie_id="thor",
    screening_id="123",
    starts_at=dateutil.parser.parse("1 Jan 1970"),
    theater_id="vue",
  ))

  await _app_service(

  )
示例#29
0
async def test_read_with_max_count(event_loop):

    stream_name = str(uuid.uuid4())

    async with connect(loop=event_loop) as c:

        await given_a_stream_with_three_events(c, stream_name)

        result = await c.get(stream_name, max_count=1)

        assert isinstance(result, list)
        assert len(result) == 1

        event = result[0]

        assert event.type == "pony_jumped"

        data = event.json()
        assert data["Pony"] == "Derpy Hooves"
示例#30
0
async def do_things():
    async with photonpump.connect() as conn:
        await write_an_event(conn)
        await read_an_event(conn)