Ejemplo n.º 1
0
    def test_add_writer_multiple_calls(self, glib_loop):
        import os

        rfd, wfd = os.pipe()

        timeout_occurred = False

        expected_i = 10
        i = 0

        def callback():
            nonlocal i
            i += 1

            if i == expected_i:
                glib_loop.stop()

        def timeout():
            nonlocal timeout_occurred
            timeout_occurred = True
            glib_loop.stop()

        try:
            glib_loop.add_writer(wfd, callback)
            glib_loop.call_later(0.1, timeout)
            glib_loop.run_forever()
        finally:
            os.close(rfd)
            os.close(wfd)

        assert not timeout_occurred
        assert i == expected_i
Ejemplo n.º 2
0
    def test_add_writer_multiple_calls(self, glib_loop):
        import os
        rfd, wfd = os.pipe()

        timeout_occurred = False

        expected_i = 10
        i = 0

        def callback():
            nonlocal i
            i += 1

            if i == expected_i:
                glib_loop.stop()

        def timeout():
            nonlocal timeout_occurred
            timeout_occurred = True
            glib_loop.stop()

        try:
            glib_loop.add_writer(wfd, callback)
            glib_loop.call_later(0.1, timeout)
            glib_loop.run_forever()
        finally:
            os.close(rfd)
            os.close(wfd)

        assert not timeout_occurred
        assert i == expected_i
Ejemplo n.º 3
0
    def test_run_forever_recursion(self, glib_loop):
        def play_it_again_sam():
            with pytest.raises(RuntimeError):
                glib_loop.run_forever()

        h = glib_loop.call_soon(play_it_again_sam)
        glib_loop.call_soon(glib_loop.stop)
        glib_loop.run_forever()
Ejemplo n.º 4
0
def test_default_signal_handling(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()
Ejemplo n.º 5
0
def test_default_signal_handling(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()
Ejemplo n.º 6
0
    def test_run_forever_recursion(self, glib_loop):
        def play_it_again_sam():
            with pytest.raises(RuntimeError):
                glib_loop.run_forever()

        h = glib_loop.call_soon(play_it_again_sam)
        glib_loop.call_soon(glib_loop.stop)
        glib_loop.run_forever()
Ejemplo n.º 7
0
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))
Ejemplo n.º 8
0
        def run_test(fd):
            import asyncio
            from gi.repository import GLib

            fut = asyncio.Future(loop=glib_loop)
            fut.add_done_callback(lambda r: remover(fd))
            glib_loop.add_writer(fd, callback, fut)
            glib_loop.call_later(0.1, timeout)
            glib_loop.run_forever()

            assert not timeout_occurred
            assert removed
Ejemplo n.º 9
0
    def test_call_soon_threadsafe(self, glib_loop):
        called = False

        def handler():
            nonlocal called
            called = True
            glib_loop.stop()

        glib_loop.call_soon_threadsafe(handler)
        glib_loop.run_forever()

        assert called, "call_soon_threadsafe handler didnt fire"
Ejemplo n.º 10
0
    def test_call_soon_threadsafe(self, glib_loop):
        called = False

        def handler():
            nonlocal called
            called = True
            glib_loop.stop()

        glib_loop.call_soon_threadsafe(handler)
        glib_loop.run_forever()

        assert called, 'call_soon_threadsafe handler didnt fire'
Ejemplo n.º 11
0
        def run_test(fd):
            import asyncio
            from gi.repository import GLib

            fut = asyncio.Future(loop=glib_loop)
            fut.add_done_callback(lambda r: remover(fd))
            glib_loop.add_writer(fd, callback, fut)
            glib_loop.call_later(0.1, timeout)
            glib_loop.run_forever()

            assert not timeout_occurred
            assert removed
Ejemplo n.º 12
0
    def test_call_soon_priority_order(self, glib_loop):
        items = []

        def handler(i):
            items.append(i)

        for i in range(10):
            glib_loop.call_soon(handler, i)
        glib_loop.call_soon(glib_loop.stop)

        glib_loop.run_forever()

        assert items
        assert items == sorted(items)
Ejemplo n.º 13
0
    def test_call_soon_priority_order(self, glib_loop):
        items = []

        def handler(i):
            items.append(i)

        for i in range(10):
            glib_loop.call_soon(handler, i)
        glib_loop.call_soon(glib_loop.stop)

        glib_loop.run_forever()

        assert items
        assert items == sorted(items)
Ejemplo n.º 14
0
    def test_add_reader(self, glib_loop):
        import os
        rfd, wfd = os.pipe()

        called = False

        def callback(*args):
            nonlocal called
            called = True
            glib_loop.stop()

        os.close(rfd)
        os.close(wfd)

        glib_loop.add_reader(rfd, callback)
        glib_loop.run_forever()
Ejemplo n.º 15
0
    def test_add_reader(self, glib_loop):
        import os
        rfd, wfd = os.pipe()

        called = False

        def callback(*args):
            nonlocal called
            called = True
            glib_loop.stop()

        os.close(rfd)
        os.close(wfd)

        glib_loop.add_reader(rfd, callback)
        glib_loop.run_forever()
Ejemplo n.º 16
0
    def test_add_writer(self, glib_loop):
        import os
        rfd, wfd = os.pipe()

        called = False

        def callback(*args):
            nonlocal called
            called = True
            glib_loop.stop()

        glib_loop.add_writer(wfd, callback)
        glib_loop.run_forever()
        os.close(rfd)
        os.close(wfd)

        assert called, 'callback handler didnt fire'
Ejemplo n.º 17
0
    def test_add_signal_handler(self, glib_loop):
        import os
        import signal

        called = False

        def handler():
            nonlocal called
            called = True
            glib_loop.stop()

        glib_loop.add_signal_handler(signal.SIGHUP, handler)
        assert signal.SIGHUP in glib_loop._sighandlers

        glib_loop.call_later(0.01, os.kill, os.getpid(), signal.SIGHUP)
        glib_loop.run_forever()

        assert called, "signal handler didnt fire"
Ejemplo n.º 18
0
    def test_call_at(self, glib_loop):
        called = False

        def handler():
            nonlocal called
            called = True

            now = glib_loop.time()
            glib_loop.stop()

            assert now >= s + 1

        s = glib_loop.time()

        glib_loop.call_at(s+1, handler)
        glib_loop.run_forever()

        assert called, 'call_at handler didnt fire'
Ejemplo n.º 19
0
    def test_add_signal_handler(self, glib_loop):
        import os
        import signal

        called = False

        def handler():
            nonlocal called
            called = True
            glib_loop.stop()

        glib_loop.add_signal_handler(signal.SIGHUP, handler)
        assert signal.SIGHUP in glib_loop._sighandlers

        glib_loop.call_later(0.01, os.kill, os.getpid(), signal.SIGHUP)
        glib_loop.run_forever()

        assert called, 'signal handler didnt fire'
Ejemplo n.º 20
0
    def test_call_at(self, glib_loop):
        called = False

        def handler():
            nonlocal called
            called = True

            now = glib_loop.time()
            glib_loop.stop()

            assert now >= s + 1

        s = glib_loop.time()

        glib_loop.call_at(s + 1, handler)
        glib_loop.run_forever()

        assert called, 'call_at handler didnt fire'
Ejemplo n.º 21
0
    def test_call_at(self, glib_loop):
        called = False

        def handler():
            nonlocal called
            called = True

            now = glib_loop.time()
            glib_loop.stop()

            print(now, s)
            import math

            assert math.isclose(now, s, abs_tol=0.1)

        s = glib_loop.time()

        glib_loop.call_at(s + 0.1, handler)
        glib_loop.run_forever()

        assert called, "call_at handler didnt fire"
Ejemplo n.º 22
0
 def play_it_again_sam():
     with pytest.raises(RuntimeError):
         glib_loop.run_forever()
Ejemplo n.º 23
0
 def play_it_again_sam():
     with pytest.raises(RuntimeError):
         glib_loop.run_forever()