summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormathieui <mathieui@mathieui.net>2020-12-04 22:59:34 +0100
committermathieui <mathieui@mathieui.net>2020-12-04 22:59:34 +0100
commit3d1e539d2bdecc9763a5d5be86f53da0638e8189 (patch)
tree2906ad96847a1c82a290eccba1471f0a46180ca6
parente592a46c99888594bfb0bf71da99c88755912a37 (diff)
downloadslixmpp-3d1e539d2bdecc9763a5d5be86f53da0638e8189.tar.gz
slixmpp-3d1e539d2bdecc9763a5d5be86f53da0638e8189.tar.bz2
slixmpp-3d1e539d2bdecc9763a5d5be86f53da0638e8189.tar.xz
slixmpp-3d1e539d2bdecc9763a5d5be86f53da0638e8189.zip
XMLStream: Add a wait_until coroutine
It will set a disposable handler on an event and wait on it with a specific timeout. Useful for integration tests without callback hell.
-rw-r--r--slixmpp/xmlstream/xmlstream.py17
1 files changed, 16 insertions, 1 deletions
diff --git a/slixmpp/xmlstream/xmlstream.py b/slixmpp/xmlstream/xmlstream.py
index af494903..066d84df 100644
--- a/slixmpp/xmlstream/xmlstream.py
+++ b/slixmpp/xmlstream/xmlstream.py
@@ -12,7 +12,7 @@
:license: MIT, see LICENSE for more details
"""
-from typing import Optional, Set, Callable
+from typing import Optional, Set, Callable, Any
import functools
import logging
@@ -1130,3 +1130,18 @@ class XMLStream(asyncio.BaseProtocol):
:param exception: An unhandled exception object.
"""
pass
+
+ async def wait_until(self, event: str, timeout=30) -> Any:
+ """Utility method to wake on the next firing of an event.
+ (Registers a disposable handler on it)
+
+ :param str event: Event to wait on.
+ :param int timeout: Timeout
+ """
+ fut = asyncio.Future()
+ self.add_event_handler(
+ event,
+ fut.set_result,
+ disposable=True,
+ )
+ return await asyncio.wait_for(fut, timeout)