def test_setting_loop(self):
        p = objc_asyncio.PyObjCEventLoopPolicy()

        p.get_event_loop()
        loop = objc_asyncio.PyObjCEventLoop()

        p.set_event_loop(loop)
        self.assertIs(p.get_event_loop(), loop)
示例#2
0
    def setUp(self):
        # Ensure the process is killed when a test takes
        # too much time.
        signal.alarm(MAX_TEST_TIME)

        self._old_policy = asyncio.get_event_loop_policy()
        asyncio.set_event_loop_policy(objc_asyncio.PyObjCEventLoopPolicy())
        self.loop = objc_asyncio.PyObjCEventLoop()
        asyncio.set_event_loop(self.loop)
        def thread_main():
            nonlocal exception, result, thread_loop

            thread_loop = objc_asyncio.PyObjCEventLoop()
            p.set_event_loop(thread_loop)

            try:
                result = p.get_event_loop()

            except Exception as exc:
                exception = exc
        def thread_main():
            nonlocal ok, exception
            loop = objc_asyncio.PyObjCEventLoop()
            asyncio.set_event_loop(loop)

            try:
                loop.add_signal_handler(signal.SIGUSR1, lambda: None)
                ok = True

            except Exception as exc:
                exception = exc
示例#5
0
    def test_closing(self):
        with self.subTest("not running"):
            loop = objc_asyncio.PyObjCEventLoop()
            loop.close()

        with self.subTest("running mock"):
            loop = objc_asyncio.PyObjCEventLoop()
            executor = loop._default_executor = unittest.mock.Mock()
            loop.close()

            self.assertIs(loop._default_executor, None)
            executor.shutdown.assert_called_once_with(wait=False)

        with self.subTest("running real"):
            loop = objc_asyncio.PyObjCEventLoop()
            executor = concurrent.futures.ThreadPoolExecutor()

            loop.set_default_executor(executor)

            self.assertIs(loop._default_executor, executor)

            loop.close()
            self.assertIs(loop._default_executor, None)
    def test_closing_regular(self):
        def handler():
            pass

        loop = objc_asyncio.PyObjCEventLoop()
        asyncio.set_event_loop(loop)

        loop.add_signal_handler(signal.SIGUSR1, handler)
        self.assertIn(signal.SIGUSR1, loop._signal_handlers)

        loop.close()

        self.assertNotIn(signal.SIGUSR1, loop._signal_handlers)

        # Closing again should be safe
        loop.close()
    def test_attach_with_work(self):
        watcher = objc_asyncio.KQueueChildWatcher()
        watcher.attach_loop(self.loop)
        self.addCleanup(watcher.close)

        watch_results = set()

        def watch(pid, result):
            watch_results.add((pid, result))

        p1 = subprocess.Popen(["/usr/bin/true"], stdout=subprocess.DEVNULL)
        self.addCleanup(p1.wait)
        watcher.add_child_handler(p1.pid, watch)

        p2 = subprocess.Popen(["/usr/bin/true"], stdout=subprocess.DEVNULL)
        self.addCleanup(p2.wait)
        watcher.add_child_handler(p2.pid, watch)

        self.assertIn(p1.pid, watcher._callbacks)
        self.assertIn(p2.pid, watcher._callbacks)

        self.assertIs(watcher._loop, self.loop)

        loop2 = objc_asyncio.PyObjCEventLoop()
        self.assertIs(watcher._loop, self.loop)

        watcher.attach_loop(loop2)
        self.assertIs(watcher._loop, loop2)

        self.assertIn(p1.pid, watcher._callbacks)
        self.assertIn(p2.pid, watcher._callbacks)

        async def main():
            await asyncio.sleep(0.5)

        loop2.run_until_complete(main())

        self.assertNotIn(p1.pid, watcher._callbacks)
        self.assertNotIn(p2.pid, watcher._callbacks)

        self.assertEqual(watch_results, {(p1.pid, 0), (p2.pid, 0)})
    def test_resetting_child_watcher(self):
        p = objc_asyncio.PyObjCEventLoopPolicy()

        p.get_event_loop()

        watcher = p.get_child_watcher()
        self.assertIsInstance(watcher, objc_asyncio.KQueueChildWatcher)
        self.assertIs(watcher._loop, p.get_event_loop())

        new_watcher = asyncio.SafeChildWatcher()
        p.set_child_watcher(new_watcher)

        self.assertIs(watcher._loop, None)
        self.assertIs(new_watcher._loop, None)

        loop = objc_asyncio.PyObjCEventLoop()
        p.set_event_loop(loop)

        self.assertIs(watcher._loop, None)
        self.assertIs(new_watcher._loop, loop)
        self.assertIs(p.get_event_loop(), loop)
    def test_closing_system_exit(self):
        # Testing at interpreter shutdown cannot be done for real, use
        # mocks.

        def handler():
            pass

        loop = objc_asyncio.PyObjCEventLoop()
        asyncio.set_event_loop(loop)

        loop.add_signal_handler(signal.SIGUSR1, handler)
        self.assertIn(signal.SIGUSR1, loop._signal_handlers)

        with self.assertWarns(ResourceWarning):
            with unittest.mock.patch("sys.is_finalizing", return_value=True):
                with unittest.mock.patch.object(
                        objc_asyncio.PyObjCEventLoop,
                        "remove_signal_handler") as remove_mock:
                    loop.close()

        self.assertNotIn(signal.SIGUSR1, loop._signal_handlers)
        remove_mock.assert_not_called()
示例#10
0
 def thread_main():
     thread_loop = objc_asyncio.PyObjCEventLoop()
     p.set_event_loop(thread_loop)
     p.set_child_watcher(watcher)
示例#11
0
        def thread_main():
            nonlocal watcher

            p.set_event_loop(objc_asyncio.PyObjCEventLoop())

            watcher = p.get_child_watcher()