def test_subprocesses_readline_without_closure(glib_loop): # needed to ensure events.get_child_watcher() returns the right object import gbulb gbulb.install() @asyncio.coroutine def run(): proc = yield from asyncio.create_subprocess_exec( 'cat', stdin=asyncio.subprocess.PIPE, stdout=asyncio.subprocess.PIPE, loop=glib_loop) try: proc.stdin.write(b'test line\n') yield from proc.stdin.drain() line = yield from asyncio.wait_for(proc.stdout.readline(), timeout=5, loop=glib_loop) assert line == b'test line\n' proc.stdin.close() line = yield from asyncio.wait_for(proc.stdout.readline(), timeout=5, loop=glib_loop) assert line == b'' finally: yield from proc.wait() glib_loop.run_until_complete(run())
def test_wait_signal(glib_loop): import asyncio from gi.repository import GObject from gbulb import wait_signal class TestObject(GObject.GObject): __gsignals__ = {"foo": (GObject.SIGNAL_RUN_LAST, None, (str,))} t = TestObject() def emitter(): yield t.emit("foo", "frozen brains tell no tales") called = False @asyncio.coroutine def waiter(): nonlocal called r = yield from wait_signal(t, "foo", loop=glib_loop) assert r == (t, "frozen brains tell no tales") called = True glib_loop.run_until_complete(asyncio.wait([waiter(), emitter()], timeout=1, loop=glib_loop)) assert called
def test_subprocesses_read_after_closure(glib_loop): import asyncio import subprocess # needed to ensure events.get_child_watcher() returns the right object import gbulb gbulb.install() @asyncio.coroutine def coro(): proc = yield from asyncio.create_subprocess_exec( 'cat', stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE, loop=glib_loop) proc.stdin.write(b'hey\n') yield from proc.stdin.drain() proc.stdin.close() out = yield from proc.stdout.read() assert out == b'hey\n' yield from proc.wait() glib_loop.run_until_complete(coro())
def test_wait_signal(glib_loop): import asyncio from gi.repository import GObject from gbulb import wait_signal class TestObject(GObject.GObject): __gsignals__ = { 'foo': (GObject.SIGNAL_RUN_LAST, None, (str,)), } t = TestObject() def emitter(): yield t.emit('foo', 'frozen brains tell no tales') called = False @asyncio.coroutine def waiter(): nonlocal called r = yield from wait_signal(t, 'foo', loop=glib_loop) assert r == (t, 'frozen brains tell no tales') called = True glib_loop.run_until_complete(asyncio.wait([waiter(), emitter()], timeout=1, loop=glib_loop)) assert called
def test_run_until_complete_early_stop(self, glib_loop): import asyncio @asyncio.coroutine def coro(): glib_loop.call_soon(glib_loop.stop) yield from asyncio.sleep(5) with pytest.raises(RuntimeError): glib_loop.run_until_complete(coro())
def test_signal_handling_with_multiple_invocations(glib_loop): import os import signal glib_loop.call_later(0.01, os.kill, os.getpid(), signal.SIGINT) with pytest.raises(KeyboardInterrupt): glib_loop.run_forever() glib_loop.run_until_complete(asyncio.sleep(0))
def test_sockets(glib_loop): server_done = asyncio.Event(loop=glib_loop) server_success = False @asyncio.coroutine def cb(reader, writer): nonlocal server_success writer.write(b'cool data\n') yield from writer.drain() print('reading') d = yield from reader.readline() print('hrm', d) server_success = d == b'thank you\n' writer.close() server_done.set() @asyncio.coroutine def run(): s = yield from asyncio.start_server(cb, '127.0.0.1', 0, loop=glib_loop) reader, writer = yield from asyncio.open_connection( '127.0.0.1', s.sockets[0].getsockname()[-1], loop=glib_loop) d = yield from reader.readline() assert d == b'cool data\n' writer.write(b'thank you\n') yield from writer.drain() writer.close() yield from server_done.wait() assert server_success glib_loop.run_until_complete(run())
def test_subprocesses(glib_loop): import asyncio import subprocess # needed to ensure events.get_child_watcher() returns the right object import gbulb gbulb.install() @asyncio.coroutine def coro(): proc = yield from asyncio.create_subprocess_exec( 'cat', stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE, loop=glib_loop) proc.stdin.write(b'hey\n') yield from proc.stdin.drain() proc.stdin.close() out = yield from proc.stdout.read() assert out == b'hey\n' glib_loop.run_until_complete(coro())
def test_wait_signal_cancel(glib_loop): import asyncio from gi.repository import GObject from gbulb import wait_signal class TestObject(GObject.GObject): __gsignals__ = {"foo": (GObject.SIGNAL_RUN_LAST, None, (str,))} t = TestObject() def emitter(): yield t.emit("foo", "frozen brains tell no tales") called = False cancelled = False def waiter(): nonlocal cancelled yield r = wait_signal(t, "foo", loop=glib_loop) @r.add_done_callback def caller(r): nonlocal called called = True r.cancel() assert r.cancelled() cancelled = True glib_loop.run_until_complete(asyncio.wait([waiter(), emitter()], timeout=1, loop=glib_loop)) assert cancelled assert called
def test_wait_signal_cancel(glib_loop): import asyncio from gi.repository import GObject from gbulb import wait_signal class TestObject(GObject.GObject): __gsignals__ = { 'foo': (GObject.SIGNAL_RUN_LAST, None, (str,)), } t = TestObject() def emitter(): yield t.emit('foo', 'frozen brains tell no tales') called = False cancelled = False def waiter(): nonlocal cancelled yield r = wait_signal(t, 'foo', loop=glib_loop) @r.add_done_callback def caller(r): nonlocal called called = True r.cancel() assert r.cancelled() cancelled = True glib_loop.run_until_complete(asyncio.wait([waiter(), emitter()], timeout=1, loop=glib_loop)) assert cancelled assert called