def test_init_constructor_default_loop(self): try: events.set_event_loop(self.loop) f = futures.Future() self.assertIs(f._loop, self.loop) finally: events.set_event_loop(None)
def test_ctor_noloop(self): try: events.set_event_loop(self.loop) ev = locks.Event() self.assertIs(ev._loop, self.loop) finally: events.set_event_loop(None)
def test_ctor_noloop(self): try: events.set_event_loop(self.loop) sem = locks.Semaphore() self.assertIs(sem._loop, self.loop) finally: events.set_event_loop(None)
def test_ctor_noloop(self): try: events.set_event_loop(self.loop) q = queues.Queue() self.assertIs(q._loop, self.loop) finally: events.set_event_loop(None)
def test_ctor_noloop(self): try: events.set_event_loop(self.loop) cond = locks.Condition() self.assertIs(cond._loop, self.loop) finally: events.set_event_loop(None)
def test_wait_for_with_global_loop(self): def gen(): when = yield self.assertAlmostEqual(0.2, when) when = yield 0 self.assertAlmostEqual(0.01, when) yield 0.01 loop = test_utils.TestLoop(gen) self.addCleanup(loop.close) @tasks.coroutine def foo(): yield from tasks.sleep(0.2, loop=loop) return 'done' events.set_event_loop(loop) try: fut = tasks.Task(foo(), loop=loop) with self.assertRaises(futures.TimeoutError): loop.run_until_complete(tasks.wait_for(fut, 0.01)) finally: events.set_event_loop(None) self.assertAlmostEqual(0.01, loop.time()) self.assertFalse(fut.done()) # move forward to close generator loop.advance_time(10) loop.run_until_complete(fut)
def test_wait_with_global_loop(self): def gen(): when = yield self.assertAlmostEqual(0.01, when) when = yield 0 self.assertAlmostEqual(0.015, when) yield 0.015 loop = test_utils.TestLoop(gen) self.addCleanup(loop.close) a = tasks.Task(tasks.sleep(0.01, loop=loop), loop=loop) b = tasks.Task(tasks.sleep(0.015, loop=loop), loop=loop) @tasks.coroutine def foo(): done, pending = yield from tasks.wait([b, a]) self.assertEqual(done, set([a, b])) self.assertEqual(pending, set()) return 42 events.set_event_loop(loop) try: res = loop.run_until_complete( tasks.Task(foo(), loop=loop)) finally: events.set_event_loop(None) self.assertEqual(res, 42)
def test_ctor_noloop(self): try: events.set_event_loop(self.loop) lock = locks.Lock() self.assertIs(lock._loop, self.loop) finally: events.set_event_loop(None)
def test_wait_with_global_loop(self): def gen(): when = yield self.assertAlmostEqual(0.01, when) when = yield 0 self.assertAlmostEqual(0.015, when) yield 0.015 loop = test_utils.TestLoop(gen) self.addCleanup(loop.close) a = tasks.Task(tasks.sleep(0.01, loop=loop), loop=loop) b = tasks.Task(tasks.sleep(0.015, loop=loop), loop=loop) @tasks.coroutine def foo(): done, pending = yield from tasks.wait([b, a]) self.assertEqual(done, set([a, b])) self.assertEqual(pending, set()) return 42 events.set_event_loop(loop) try: res = loop.run_until_complete(tasks.Task(foo(), loop=loop)) finally: events.set_event_loop(None) self.assertEqual(res, 42)
def test_ctor_noloop(self): try: events.set_event_loop(self.loop) ev = locks.EventWaiter() self.assertIs(ev._loop, self.loop) finally: events.set_event_loop(None)
def _check_empty_sequence(self, seq_or_iter): events.set_event_loop(self.one_loop) self.addCleanup(events.set_event_loop, None) fut = tasks.gather(*seq_or_iter) self.assertIsInstance(fut, futures.Future) self.assertIs(fut._loop, self.one_loop) self._run_loop(self.one_loop) self.assertTrue(fut.done()) self.assertEqual(fut.result(), []) fut = tasks.gather(*seq_or_iter, loop=self.other_loop) self.assertIs(fut._loop, self.other_loop)
def test_open_connection_no_loop_ssl(self): with test_utils.run_test_server(self.loop, use_ssl=True) as httpd: try: events.set_event_loop(self.loop) f = streams.open_connection(*httpd.address, ssl=True) reader, writer = self.loop.run_until_complete(f) finally: events.set_event_loop(None) writer.write(b'GET / HTTP/1.0\r\n\r\n') f = reader.read() data = self.loop.run_until_complete(f) self.assertTrue(data.endswith(b'\r\n\r\nTest message'))
def test_task_decorator_func(self): @tasks.task def notmuch(): return 'ko' try: events.set_event_loop(self.loop) t = notmuch() finally: events.set_event_loop(None) self.assertIsInstance(t, tasks.Task) self.loop.run_until_complete(t) self.assertTrue(t.done()) self.assertEqual(t.result(), 'ko')
def setUp(self): super().setUp() self.loop = self.create_event_loop() events.set_event_loop(self.loop)
def setUp(self): super().setUp() events.set_event_loop(self.one_loop)
def setUp(self): self.loop = base_events.BaseEventLoop() self.loop._selector = unittest.mock.Mock() self.loop._selector.registered_count.return_value = 1 events.set_event_loop(None)
print(len(self.done), 'completed tasks,', len(self.tasks), 'still pending, todo', len(self.todo)) def main(): loop = tulip.get_event_loop() c = Crawler(sys.argv[1], loop) c.run() try: loop.add_signal_handler(signal.SIGINT, loop.stop) except RuntimeError: pass loop.run_forever() print('todo:', len(c.todo)) print('busy:', len(c.busy)) print('done:', len(c.done), '; ok:', sum(c.done.values())) print('tasks:', len(c.tasks)) if __name__ == '__main__': if '--iocp' in sys.argv: from tulip import events, windows_events sys.argv.remove('--iocp') logging.info('using iocp') el = windows_events.ProactorEventLoop() events.set_event_loop(el) main()
def tearDown(self): events.set_event_loop(None) super().tearDown()
def setUp(self): self.loop = test_utils.TestLoop() events.set_event_loop(None)
def setUp(self): self.loop = unix_events.SelectorEventLoop() events.set_event_loop(self.loop)
def setUp(self): self.loop = unix_events.SelectorEventLoop() events.set_event_loop(None)
def setUp(self): self.loop = events.new_event_loop() events.set_event_loop(None)
def setUp(self): self.loop = base_events.BaseEventLoop() self.loop._selector = unittest.mock.Mock() events.set_event_loop(None)
#!/usr/bin/env python3 import sys import tulip import tulip.http def curl(url): response = yield from tulip.http.request('GET', url) print(repr(response)) data = yield from response.read() print(data.decode('utf-8', 'replace')) if __name__ == '__main__': if '--iocp' in sys.argv: from tulip import events, windows_events sys.argv.remove('--iocp') el = windows_events.ProactorEventLoop() events.set_event_loop(el) loop = tulip.get_event_loop() loop.run_until_complete(curl(sys.argv[1]))