@@ -273,6 +273,19 @@ def session(self) -> aiohttp.ClientSession:
273273
274274
275275async def main (coro : Coroutine [Any , Any , None ]) -> None :
276+ """Entrypoint for dispatch function tests, which creates a local Dispatch
277+ server and runs the provided coroutine in the event loop of the server.
278+
279+ This is a low-level primitive that most test programs wouldn't use directly,
280+ and instead would use one of the `function` or `method` decorators.
281+
282+ Args:
283+ coro: The coroutine to run as the entrypoint, the function returns
284+ when the coroutine returns.
285+
286+ Returns:
287+ The value returned by the coroutine.
288+ """
276289 reg = default_registry ()
277290 api = Service ()
278291 app = Dispatch (reg )
@@ -297,10 +310,48 @@ async def main(coro: Coroutine[Any, Any, None]) -> None:
297310
298311
299312def run (coro : Coroutine [Any , Any , None ]) -> None :
313+ """Runs the provided coroutine in the test server's event loop. This
314+ function is a convenience wrapper around the `main` function that runs the
315+ coroutine in the event loop of the test server.
316+
317+ Programs typically don't use this function directly, unless they manage
318+ their own event loop. Most of the time, the `run` function is a more
319+ convenient way to run a dispatch application.
320+
321+ Args:
322+ coro: The coroutine to run as the entrypoint, the function returns
323+ when the coroutine returns.
324+
325+ Returns:
326+ The value returned by the coroutine.
327+ """
300328 return asyncio .run (main (coro ))
301329
302330
303331def function (fn : Callable [[], Coroutine [Any , Any , None ]]) -> Callable [[], None ]:
332+ """This decorator is used to write tests that execute in a local Dispatch
333+ server.
334+
335+ The decorated function would typically be a coroutine that implements the
336+ test and returns when the test is done, for example:
337+
338+ ```python
339+ import dispatch
340+ import dispatch.test
341+
342+ @dispatch.function
343+ def greet(name: str) -> str:
344+ return f"Hello {name}!"
345+
346+ @dispatch.test.function
347+ async def test_greet():
348+ assert await greet("World") == "Hello World!"
349+ ```
350+
351+ The test runs dispatch functions with the full dispatch capability,
352+ including retrying temporary errors, etc...
353+ """
354+
304355 @wraps (fn )
305356 def wrapper ():
306357 return run (fn ())
@@ -309,6 +360,10 @@ def wrapper():
309360
310361
311362def method (fn : Callable [[T ], Coroutine [Any , Any , None ]]) -> Callable [[T ], None ]:
363+ """This decorator is similar to the `function` decorator but is intended to
364+ apply to methods of a class (with a `self` value as first argument).
365+ """
366+
312367 @wraps (fn )
313368 def wrapper (self : T ):
314369 return run (fn (self ))
0 commit comments