Ejemplo n.º 1
0
    async def test_process_daemon(self):
        p = amp.Process(daemon=False)
        self.assertEqual(p.daemon, False)
        p.daemon = True
        self.assertEqual(p.daemon, True)

        p = amp.Process(daemon=True)
        self.assertEqual(p.daemon, True)
        p.daemon = False
        self.assertEqual(p.daemon, False)
Ejemplo n.º 2
0
    async def test_process_await(self):
        p = amp.Process(target=sleepy, name="test_process")
        await p

        self.assertIsNotNone(p.exitcode)

        p = amp.Process(target=sleepy, name="test_process")
        p.start()
        await p

        self.assertIsNotNone(p.exitcode)
Ejemplo n.º 3
0
    async def test_sync_target(self):
        def dummy():
            pass

        with self.assertRaises(ValueError) as _:
            p = amp.Process(target=dummy, name="test_process", initializer=dummy)
            p.start()
Ejemplo n.º 4
0
    async def test_async_initializer(self):
        async def sleepy():
            await asyncio.sleep(0)

        with self.assertRaises(ValueError) as _:
            p = amp.Process(target=sleepy, name="test_process", initializer=sleepy)
            p.start()
Ejemplo n.º 5
0
    async def test_process_timeout(self):
        async def sleepy():
            await asyncio.sleep(1)

        p = amp.Process(target=sleepy)
        p.start()

        with self.assertRaises(asyncio.TimeoutError):
            await p.join(timeout=0.01)
Ejemplo n.º 6
0
    async def test_process_terminate(self):
        start = time.time()
        p = amp.Process(target=asyncio.sleep, args=(1,), name="test_process")
        p.start()

        p.terminate()
        await p.join()
        self.assertLess(p.exitcode, 0)
        self.assertLess(time.time() - start, 0.6)
Ejemplo n.º 7
0
    async def test_process_join(self):
        p = amp.Process(target=sleepy, name="test_process")

        with self.assertRaisesRegex(ValueError, "must start process"):
            await p.join()

        p.start()
        await p.join()
        self.assertIsNotNone(p.exitcode)
Ejemplo n.º 8
0
    async def test_process(self):
        p = amp.Process(target=sleepy, name="test_process")
        p.start()

        self.assertEqual(p.name, "test_process")
        self.assertTrue(p.pid)
        self.assertTrue(p.is_alive())

        await p.join()
        self.assertFalse(p.is_alive())
Ejemplo n.º 9
0
    async def test_process_terminate(self):
        start = time.time()

        async def terminate(process):
            await asyncio.sleep(0.5)
            process.terminate()

        p = amp.Process(target=asyncio.sleep, args=(1,), name="test_process")
        await asyncio.gather(*[terminate(p), p])
        self.assertLess(time.time() - start, 0.6)
Ejemplo n.º 10
0
    async def test_initializer(self):
        p = amp.Process(target=sleepy,
                        name="test_process",
                        initializer=do_nothing)
        p.start()
        await p.join()

        result = 10
        async with amp.Pool(2, initializer=initializer,
                            initargs=(result, )) as pool:
            self.assertEqual(await pool.apply(get_dummy_constant, args=()),
                             result)
Ejemplo n.º 11
0
    async def test_process_kill(self):
        p = amp.Process(target=sleepy)
        p.start()

        if sys.version_info >= (3, 7):
            p.kill()
            await p.join()
            self.assertLess(p.exitcode, 0)

        else:
            with self.assertRaises(AttributeError):
                p.kill()
            await p.join()
Ejemplo n.º 12
0
    async def test_process_close(self):
        p = amp.Process(target=sleepy)
        p.start()

        if sys.version_info >= (3, 7):
            with self.assertRaises(ValueError):
                self.assertIsNone(p.exitcode)
                p.close()

            await p.join()
            self.assertIsNotNone(p.exitcode)

            p.close()

            with self.assertRaises(ValueError):
                _ = p.exitcode

        else:
            with self.assertRaises(AttributeError):
                p.close()
            await p.join()
Ejemplo n.º 13
0
 async def test_sync_target(self):
     with self.assertRaises(ValueError) as _:
         p = amp.Process(
             target=do_nothing, name="test_process", initializer=do_nothing
         )
         p.start()