async def test_pub_sub_with_all_topics(server):
    """
    Check client gets event when subscribing via ALL_TOPICS
    """
    # finish trigger
    finish = asyncio.Event()
    # Create a client and subscribe to topics
    async with PubSubClient() as client:

        async def on_event(data, topic):
            assert data == DATA
            finish.set()

        # subscribe for the event
        client.subscribe(ALL_TOPICS, on_event)
        # start listentining
        client.start_client(uri)
        # wait for the client to be ready to receive events
        await client.wait_until_ready()
        # publish events (with sync=False toa void deadlocks waiting on the publish to ourselves)
        published = await client.publish(
            [EVENT_TOPIC], data=DATA, sync=False, notifier_id=gen_uid()
        )
        assert published.result
        # wait for finish trigger
        await asyncio.wait_for(finish.wait(), 5)
async def test_pub_sub_with_all_topics_with_remote_id_on(server):
    """
    same as the basic_test::test_pub_sub_with_all_topics, but this time makes sure that
    the rpc_channel_get_remote_id doesn't break anything.
    """
    # finish trigger
    finish = asyncio.Event()
    # Create a client and subscribe to topics
    async with PubSubClient() as client:

        async def on_event(data, topic):
            assert data == DATA
            finish.set()

        # subscribe for the event
        client.subscribe(ALL_TOPICS, on_event)
        # start listentining
        client.start_client(uri)
        # wait for the client to be ready to receive events
        await client.wait_until_ready()
        # publish events (with sync=False toa void deadlocks waiting on the publish to ourselves)
        published = await client.publish([EVENT_TOPIC],
                                         data=DATA,
                                         sync=False,
                                         notifier_id=gen_uid())
        assert published.result
        # wait for finish trigger
        await asyncio.wait_for(finish.wait(), 5)
Beispiel #3
0
async def server_subscribe_to_topic(server, is_topic_permitted):
    # finish trigger
    finish = asyncio.Event()

    permitted_topics = ["t1", "t2"]
    if is_topic_permitted:
        permitted_topics.append(CLIENT_TOPIC)

    # Create a client and subscribe to topics
    async with PubSubClient(extra_headers={
            "headers": {
                "claims": {
                    "permitted_topics": permitted_topics
                }
            }
    }) as client:

        async def on_event(data, topic):
            assert data == DATA
            finish.set()

        # subscribe for the event
        client.subscribe(CLIENT_TOPIC, on_event)
        # start listentining
        client.start_client(uri)
        # wait for the client to be ready to receive events
        await client.wait_until_ready()
        # trigger the server via an HTTP route
        requests.get(trigger_url)
        # wait for finish trigger
        if is_topic_permitted:
            await asyncio.wait_for(finish.wait(), 5)
        else:
            await asyncio.sleep(5)
            assert not finish.is_set()
async def test_delayed_server_disconnect(delayed_death_server):
    """
    Test reconnecting when a server hangups AFTER connect
    """
    # finish trigger
    finish = asyncio.Event()

    async def on_connect(client, channel):
        try:
            print("Connected")
            # publish events (with sync=False to avoid deadlocks waiting on the publish to ourselves)
            published = await client.publish([EVENT_TOPIC],
                                             data=DATA,
                                             sync=False,
                                             notifier_id=gen_uid())
            assert published.result == True
        except RpcChannelClosedException:
            # expected
            pass

    # Create a client and subscribe to topics
    async with PubSubClient(on_connect=[on_connect]) as client:

        async def on_event(data, topic):
            assert data == DATA
            finish.set()

        # subscribe for the event
        client.subscribe(EVENT_TOPIC, on_event)
        # start listentining
        client.start_client(uri)
        # wait for the client to be ready to receive events
        await client.wait_until_ready()
        # wait for finish trigger
        await asyncio.wait_for(finish.wait(), 5)
async def test_immediate_server_disconnect(server):
    """
    Test reconnecting when a server hangups on connect
    """
    # finish trigger
    finish = asyncio.Event()
    # Create a client and subscribe to topics
    async with PubSubClient() as client:

        async def on_event(data, topic):
            assert data == DATA
            finish.set()

        # subscribe for the event
        client.subscribe(EVENT_TOPIC, on_event)
        # start listentining
        client.start_client(uri)
        # wait for the client to be ready to receive events
        await client.wait_until_ready()
        # publish events (with sync=False toa void deadlocks waiting on the publish to ourselves)
        published = await client.publish([EVENT_TOPIC],
                                         data=DATA,
                                         sync=False,
                                         notifier_id=gen_uid())
        assert published.result == True
        # wait for finish trigger
        await asyncio.wait_for(finish.wait(), 5)
        async def wait_for_ready():
            Webhooks.client = PubSubClient(
                [WEBHOOK_TOPIC_ALL], callback=Webhooks._on_webhook
            )

            ws_url = WEBHOOKS_URL
            if ws_url.startswith("http"):
                ws_url = "ws" + ws_url[4:]

            Webhooks.client.start_client(ws_url + "/pubsub")
            await Webhooks.client.wait_until_ready()
Beispiel #7
0
async def main():
    # Create a client and subscribe to topics
    client = PubSubClient(["guns", "germs"], callback=on_events)

    async def on_steel(data, topic):
        print("running callback steel!")
        print("Got data", data)
        asyncio.create_task(client.disconnect())

    client.subscribe("steel", on_steel)
    client.start_client(f"ws://localhost:{PORT}/pubsub")
    await client.wait_until_done()
Beispiel #8
0
 async def actual():
     # Wait for other client to wake up before publishing to it
     CLIENT_START_SYNC.wait(5)
     # Create a client and subscribe to topics
     client = PubSubClient()
     client.start_client(uri)
     # wait for the client to be ready
     await client.wait_until_ready()
     # publish event
     logger.info("Publishing event")
     published = await client.publish([EVENT_TOPIC], data=DATA)
     assert published.result == True
Beispiel #9
0
 async def run():
     # trigger an update
     entries = [DataSourceEntry(url=DATA_URL)]
     update = DataUpdate(reason="Test", entries=entries)
     async with PubSubClient(server_uri=UPDATES_URL,
                             methods_class=TenantAwareRpcEventClientMethods,
                             extra_headers=[
                                 get_authorization_header(
                                     opal_client_config.CLIENT_TOKEN)
                             ]) as client:
         # Channel must be ready before we can publish on it
         await asyncio.wait_for(client.wait_until_ready(), 5)
         logging.info("Publishing data event")
         await client.publish(DATA_TOPICS, data=update)
async def main():
    # Create a client and subscribe to topics
    client = PubSubClient(["ALL_WEBHOOKS"], callback=on_events)
    """
    # You can also register it using the commented code below
    async def on_data(data, topic):
        print(f"{topic}:\n", data)

    [client.subscribe(topic, on_data) for topic in topics]
    """

    client.start_client(f"ws://{URL}:{3010}/pubsub")
    print("Started")
    await client.wait_until_done()
Beispiel #11
0
 async def _subscriber(self):
     """
     Coroutine meant to be spunoff with create_task to listen in
     the background for data events and pass them to the data_fetcher
     """
     logger.info("Subscribing to topics: {topics}",
                 topics=self._data_topics)
     self._client = PubSubClient(
         self._data_topics,
         self._update_policy_data_callback,
         methods_class=TenantAwareRpcEventClientMethods,
         on_connect=[self.on_connect],
         extra_headers=self._extra_headers,
         keep_alive=opal_client_config.KEEP_ALIVE_INTERVAL,
         server_uri=self._server_url)
     async with self._client:
         await self._client.wait_until_done()
Beispiel #12
0
 async def _subscriber(self):
     """
     Coroutine meant to be spunoff with create_task to listen in
     the background for policy update events and pass them to the
     update_policy() callback (which will fetch the relevant policy
     bundle from the server and update the policy store).
     """
     logger.info("Subscribing to topics: {topics}", topics=self._topics)
     self._client = PubSubClient(
         topics=self._topics,
         callback=self._update_policy_callback,
         on_connect=[self._on_connect],
         on_disconnect=[self._on_disconnect],
         extra_headers=self._extra_headers,
         keep_alive=opal_client_config.KEEP_ALIVE_INTERVAL,
         server_uri=self._server_url)
     async with self._client:
         await self._client.wait_until_done()
Beispiel #13
0
async def server_publish_to_topic(server, is_topic_permitted):
    # Create a client and subscribe to topics
    async with PubSubClient(extra_headers={
            "headers": {
                "claims": {
                    "permitted_topics": ["t1", "t2"]
                }
            }
    }) as client:
        # start listentining
        client.start_client(uri)
        # wait for the client to be ready to receive events
        await client.wait_until_ready()

        result = await client.publish(["t1" if is_topic_permitted else "t3"],
                                      data=DATA,
                                      sync=True)
        assert result.result == is_topic_permitted
async def test_subscribe_http_trigger(server):
    # finish trigger
    finish = asyncio.Event()
    # Create a client and subscribe to topics
    async with PubSubClient() as client:
        async def on_event(data, topic):
            assert data == DATA
            finish.set()
        # subscribe for the event
        client.subscribe(EVENT_TOPIC, on_event)
        # start listentining
        client.start_client(uri)
        # wait for the client to be ready to receive events 
        await client.wait_until_ready()
        # trigger the server via an HTTP route
        requests.get(trigger_url)
        # wait for finish trigger
        await asyncio.wait_for(finish.wait(),5)
async def test_getting_remote_id(server):
    """
    tests that the server managed to get the client's channel id successfully.
    """
    # finish trigger
    finish = asyncio.Event()
    remote_id_yes = asyncio.Event()

    # Create a client and subscribe to topics
    async with PubSubClient() as client:

        async def on_event(data, topic):
            assert data == DATA
            finish.set()

        async def on_answer(data, topic):
            assert data.get("answer", None) == "yes"
            remote_id_yes.set()

        # subscribe for the event
        client.subscribe(EVENT_TOPIC, on_event)
        client.subscribe(REMOTE_ID_ANSWER_TOPIC, on_answer)
        # start listentining
        client.start_client(uri)
        # wait for the client to be ready to receive events
        await client.wait_until_ready()
        # trigger the server via an HTTP route
        requests.get(trigger_url)
        # wait for finish trigger
        await asyncio.wait_for(finish.wait(), 5)
        # sleep so that the server can finish getting the remote id
        await asyncio.sleep(1)
        # ask the server if he got the remote id
        # will trigger the REMOTE_ID_ANSWER_TOPIC topic and the on_answer() callback
        requests.get(ask_remote_id_url)
        await asyncio.wait_for(remote_id_yes.wait(), 5)
        # the client can also try to get it's remote id
        # super ugly but it's working:
        my_remote_id = await client._rpc_channel._get_other_channel_id()
        assert my_remote_id is not None
Beispiel #16
0
async def test_pub_sub_multi_client(server, pub_client):
    # finish trigger
    finish = asyncio.Event()
    # Create a client and subscribe to topics
    async with PubSubClient() as client:

        async def on_event(data, topic):
            assert data == DATA
            assert topic == EVENT_TOPIC
            finish.set()

        # subscribe for the event
        logger.info("Subscribing for events")
        client.subscribe(EVENT_TOPIC, on_event)
        # start listentining
        client.start_client(uri)
        await client.wait_until_ready()
        # Let the other client know we're ready
        logger.info("First client is ready")
        CLIENT_START_SYNC.set()
        # wait for finish trigger
        await asyncio.wait_for(finish.wait(), 10)
async def test_subscribe_http_trigger_with_remote_id_on(server):
    """
    same as the basic_test::test_subscribe_http_trigger, but this time makes sure that
    the rpc_channel_get_remote_id doesn't break anything.
    """
    # finish trigger
    finish = asyncio.Event()
    # Create a client and subscribe to topics
    async with PubSubClient() as client:

        async def on_event(data, topic):
            assert data == DATA
            finish.set()

        # subscribe for the event
        client.subscribe(EVENT_TOPIC, on_event)
        # start listentining
        client.start_client(uri)
        # wait for the client to be ready to receive events
        await client.wait_until_ready()
        # trigger the server via an HTTP route
        requests.get(trigger_url)
        # wait for finish trigger
        await asyncio.wait_for(finish.wait(), 5)