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
async def try_to_get_remote_id(channel: RpcChannel): logger.info(f"trying to get remote channel id") channel_other_channel_id = await channel.get_other_channel_id() logger.info(f"finished getting remote channel id") if channel_other_channel_id is not None: remote_id_ok.set() logger.info(f"remote channel id: {channel_other_channel_id}") logger.info(f"local channel id: {channel_other_channel_id}")
async def on_connect(channel: RpcChannel): if counter.value == 0: # Immediate death if disconnect_delay == 0: logger.info("Disconnect once") await channel.socket.close() # Delayed death else: async def disconn(): await asyncio.sleep(disconnect_delay) logger.info("Disconnect once") await channel.socket.close() asyncio.create_task(disconn()) counter.value = 1
def setup_publishing_client(): """ this client will publish an event to the main-test client """ 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 logger.info("Starting async publishing client") asyncio.get_event_loop().run_until_complete(actual())
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 event_callback(subscription: Subscription, data): logger.info(f"Got topic {subscription.topic}")
async def event_callback(subscription: Subscription, data): logger.info( f"Got topic {subscription.topic} - re-publishing as {CLIENT_TOPIC}" ) asyncio.create_task(endpoint.publish([CLIENT_TOPIC], data))
async def trigger_events(): logger.info("Triggered via HTTP route - publishing event") # Publish an event - to the our own server callback / which will trigger another event for the client # Since we are calling back (RPC) to the client- this would deadlock if we wait on it asyncio.create_task(endpoint.publish([SERVER_TOPIC], data=DATA)) return "triggered"
async def disconn(): await asyncio.sleep(disconnect_delay) logger.info("Disconnect once") await channel.socket.close()
async def trigger_events(): logger.info("Triggered via HTTP route - publishing event") # Publish an event named 'steel' # Since we are calling back (RPC) to the client- this would deadlock if we wait on it asyncio.create_task(endpoint.publish([EVENT_TOPIC], data=DATA)) return "triggered"
async def on_connect(channel: RpcChannel): logger.info(f"Connected to remote channel") asyncio.create_task(try_to_get_remote_id(channel))
async def trigger_events(): logger.info("Got asked if i have the remote id") answer = "yes" if remote_id_event.is_set() else "no" asyncio.create_task( endpoint.publish([REMOTE_ID_ANSWER_TOPIC], {"answer": answer})) return {"answer": answer}