Skip to content
This repository was archived by the owner on Sep 17, 2024. It is now read-only.

Commit c8d4b0b

Browse files
committed
Add back await_message for use when it makes sense
See function doc comment for when it's not appropriate. Signed-off-by: Daniel Bluhm <[email protected]>
1 parent 940a630 commit c8d4b0b

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

aries_staticagent/static_connection.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,23 @@ async def send_and_await_reply_async(
333333
)
334334
return await asyncio.wait_for(next_message, timeout)
335335

336+
async def await_message(
337+
self,
338+
*,
339+
type_: str = None,
340+
condition: Callable[[Message], bool] = None,
341+
timeout: int = None):
342+
"""
343+
Wait for a message.
344+
345+
Note that it's possible for a message to arrive just before or during
346+
the setup of this function. If it's likely that a message will arrive
347+
as a result of an action taken prior to calling await_message, use the
348+
`next` context manager instead.
349+
"""
350+
with self.next(type_, cond=condition) as next_message:
351+
return await asyncio.wait_for(next_message, timeout)
352+
336353
def send(self, *args, **kwargs):
337354
"""Blocking wrapper around send_async."""
338355
loop = asyncio.get_event_loop()

tests/test_send.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""Test StaticConection send method."""
2+
import asyncio
23
import copy
34
from functools import partial
45
from asyncio import wait_for
@@ -250,6 +251,14 @@ async def test_multiple_next_fulfilled_sequentially(alice, bob):
250251
assert first == second
251252

252253

254+
@pytest.mark.asyncio
255+
async def test_next_with_type_and_cond_raises_error(alice):
256+
"""Test value error raised when both type and condition specified."""
257+
with pytest.raises(ValueError):
258+
with alice.next(MESSAGE.type, lambda msg: True):
259+
pass
260+
261+
253262
@pytest.mark.asyncio
254263
async def test_send_and_await_reply(alice_gen, bob, send):
255264
"""Test holding and awaiting messages."""
@@ -258,6 +267,16 @@ async def test_send_and_await_reply(alice_gen, bob, send):
258267
assert response == RESPONSE
259268

260269

270+
@pytest.mark.asyncio
271+
async def test_await_message(alice, bob):
272+
"""Test awaiting a message."""
273+
waiting_task = asyncio.ensure_future(alice.await_message())
274+
await asyncio.sleep(.1)
275+
await alice.handle(bob.pack(MESSAGE))
276+
message = await waiting_task
277+
assert message == MESSAGE
278+
279+
261280
def test_blocking_send(alice_gen, bob, send):
262281
"""Test blocking send"""
263282
alice = alice_gen(send=send)

0 commit comments

Comments
 (0)