def test_time_sleep(self): # A real blocking function from time import sleep # No time given, we detect the failure to switch immediately with self.assertRaises(util._FailedToSwitch) as exc: with util.assert_switches(): sleep(0.001) message = str(exc.exception) self.assertIn('To any greenlet in', message) # Supply a max blocking allowed and exceed it with self.assertRaises(util._FailedToSwitch): with util.assert_switches(0.001): sleep(0.1) # Supply a max blocking allowed, and exit before that happens, # but don't switch to the hub as requested with self.assertRaises(util._FailedToSwitch) as exc: with util.assert_switches(0.001, hub_only=True): sleep(0) message = str(exc.exception) self.assertIn('To the hub in', message) self.assertIn('(max allowed 0.0010 seconds)', message) # Supply a max blocking allowed, and exit before that happens, # and allow any switch (or no switch). # Note that we need to use a relatively long duration; # sleep(0) on Windows can actually take a substantial amount of time # sometimes (more than 0.001s) with util.assert_switches(1.0, hub_only=False): sleep(0)
def test_time_sleep(self): # A real blocking function from time import sleep # No time given, we detect the failure to switch immediately with self.assertRaises(util._FailedToSwitch) as exc: with util.assert_switches(): sleep(0.001) message = str(exc.exception) self.assertIn('To any greenlet in', message) # Supply a max blocking allowed and exceed it with self.assertRaises(util._FailedToSwitch): with util.assert_switches(0.001): sleep(0.1) # Supply a max blocking allowed, and exit before that happens, # but don't switch to the hub as requested with self.assertRaises(util._FailedToSwitch) as exc: with util.assert_switches(0.001, hub_only=True): sleep(0) message = str(exc.exception) self.assertIn('To the hub in', message) self.assertIn('(max allowed 0.0010 seconds)', message) # Supply a max blocking allowed, and exit before that happens, # and allow any switch (or no switch). # Note that we need to use a relatively long duration; # sleep(0) on Windows can actually take a substantial amount of time # sometimes (more than 0.001s) with util.assert_switches(1.0, hub_only=False): sleep(0)
def _test_mp_queues(self, p, r_q, w_q): async_counter = [0] def count(): while True: idle() async_counter[0] += 1 sleep(0.001) task = spawn(count) p.start() self.assertTrue(p.pid > 0) with assert_switches(): w_q.put("master", timeout=5) with assert_switches(): self.assertEqual(r_q.get(timeout=5), "test_queues") with assert_switches(): start = clock() p.join(15) end = clock() logger.info("Waited for child to die for %f" % (end - start)) task.kill() # This is to ensure a greenlet flip sleep(0.001) logger.info(f"checking {p} is alive") self.assertFalse(p.is_alive()) self.assertEqual(p.exitcode, 10) logger.info("Async counter counted to %d" % async_counter[0]) self.assertGreater(async_counter[0], 0)
def test_queues(r_q, w_q): def count(): while True: sleep(0.01) task = spawn(count) task.start() with assert_switches(): sleep(1) with assert_switches(): logger.info(r_q.get(timeout=5)) with assert_switches(): sleep(1) with assert_switches(): w_q.put(test_queues.__name__, timeout=5) with assert_switches(): sleep(1) task.kill() logger.info("exiting") sys.exit(10)
def test_no_switches_no_function(self): # No blocking time given, no switch performed: exception with self.assertRaises(util._FailedToSwitch): with util.assert_switches(): pass # blocking time given, for all greenlets, no switch performed: nothing with util.assert_switches(max_blocking_time=1, hub_only=False): pass
def test_no_switches_no_function(self): # No blocking time given, no switch performed: exception with self.assertRaises(util._FailedToSwitch): with util.assert_switches(): pass # blocking time given, for all greenlets, no switch performed: nothing with util.assert_switches(max_blocking_time=1, hub_only=False): pass
def test_nested(self): from greenlet import gettrace with util.assert_switches() as outer: self.assertEqual(gettrace(), outer.tracer) self.assertIsNotNone(outer.tracer.active_greenlet) with util.assert_switches() as inner: self.assertEqual(gettrace(), inner.tracer) self.assertEqual(inner.tracer.previous_trace_function, outer.tracer) inner.tracer('switch', (self, self)) self.assertIs(self, inner.tracer.active_greenlet) self.assertIs(self, outer.tracer.active_greenlet) self.assertEqual(gettrace(), outer.tracer)
def test_nested(self): from greenlet import gettrace with util.assert_switches() as outer: self.assertEqual(gettrace(), outer.tracer) self.assertIsNotNone(outer.tracer.active_greenlet) with util.assert_switches() as inner: self.assertEqual(gettrace(), inner.tracer) self.assertEqual(inner.tracer.previous_trace_function, outer.tracer) inner.tracer('switch', (self, self)) self.assertIs(self, inner.tracer.active_greenlet) self.assertIs(self, outer.tracer.active_greenlet) self.assertEqual(gettrace(), outer.tracer)
def test_time_sleep(self): # A real blocking function from time import sleep with self.assertRaises(util._FailedToSwitch): with util.assert_switches(): sleep(0.001) # Supply a max allowed and exceed it with self.assertRaises(util._FailedToSwitch): with util.assert_switches(0.001): sleep(0.1) # Stay within it, but don't switch to the hub with self.assertRaises(util._FailedToSwitch): with util.assert_switches(0.001, hub_only=True): sleep(0) # Stay within it, and we only watch for any switch with util.assert_switches(0.001, hub_only=False): sleep(0)
def test_no_args(): if not monkey.saved[GEVENT_SAVED_MODULE_SETTINGS].get("geventmp"): raise RuntimeError("GeventMP patch has not run!") logger.info(test_no_args.__name__) def count(): while True: sleep(0.01) task = spawn(count) task.start() with assert_switches(): sleep(1) task.kill() logger.info("exiting") sys.exit(10)
def test_exception_not_supressed(self): with self.assertRaises(NameError): with util.assert_switches(): raise NameError()
def test_exception_not_supressed(self): with self.assertRaises(NameError): with util.assert_switches(): raise NameError()