def wait(self, timeout): with gevent.Timeout(timeout, False): for index in xrange(1000000): try: gevent_socket.gethostbyname('www.x%s.com' % index) except socket.error: pass raise AssertionError('Timeout was not raised')
def test(self): events = [Event() for _ in xrange(self.N)] asyncs = [AsyncResult() for _ in xrange(self.N)] max_len = len(events) + len(asyncs) sender = gevent.spawn(self._sender, events, asyncs) results = gevent.wait(events + asyncs, count=self.count, timeout=self.timeout) if self.timeout is None: expected_len = max_len else: expected_len = min(max_len, self.timeout / self.period) if self.count is None: assert sender.ready() else: expected_len = min(self.count, expected_len) assert not sender.ready() sender.kill() self.assertEqual(expected_len, len(results), (expected_len, len(results), results))
def test_greenlet_link(self): lst = [] # test that links are executed in the same order as they were added g = gevent.spawn(lst.append, 0) for i in xrange(1, self.count): g.link(appender(lst, i)) g.join() self.assertEqual(lst, list(range(self.count)))
def queue_join_test(self, q): self.cum = 0 for i in (0, 1): threading.Thread(target=self.worker, args=(q, )).start() for i in xrange(100): q.put(i) q.join() self.assertEquals(self.cum, sum(range(100)), "q.join() did not block until all tasks were done") for i in (0, 1): q.put(None) # instruct the threads to close q.join() # verify that you can join twice
def queue_join_test(self, q): self.cum = 0 for i in (0,1): threading.Thread(target=self.worker, args=(q,)).start() for i in xrange(100): q.put(i) q.join() self.assertEquals(self.cum, sum(range(100)), "q.join() did not block until all tasks were done") for i in (0,1): q.put(None) # instruct the threads to close q.join() # verify that you can join twice
def test_enumerate_after_join(self): # Try hard to trigger #1703448: a thread is still returned in # threading.enumerate() after it has been join()ed. enum = threading.enumerate old_interval = sys.getcheckinterval() try: for i in xrange(1, 100): # Try a couple times at each thread-switching interval # to get more interleavings. sys.setcheckinterval(i // 5) t = threading.Thread(target=lambda: None) t.start() t.join() l = enum() self.assertFalse( t in l, "#1703448 triggered after %d trials: %s" % (i, l)) finally: sys.setcheckinterval(old_interval)
def test_enumerate_after_join(self): # Try hard to trigger #1703448: a thread is still returned in # threading.enumerate() after it has been join()ed. enum = threading.enumerate old_interval = sys.getcheckinterval() try: for i in xrange(1, 100): # Try a couple times at each thread-switching interval # to get more interleavings. sys.setcheckinterval(i // 5) t = threading.Thread(target=lambda: None) t.start() t.join() l = enum() self.assertFalse(t in l, "#1703448 triggered after %d trials: %s" % (i, l)) finally: sys.setcheckinterval(old_interval)
def test(self): server = backdoor.BackdoorServer(('127.0.0.1', 0)) server.start() def connect(): conn = create_connection(('127.0.0.1', server.server_port)) try: read_until(conn, '>>> ') conn.sendall(b'2+2\r\n') line = readline(conn) self.assertEqual(line.strip(), '4', repr(line)) finally: conn.close() try: jobs = [gevent.spawn(connect) for _ in xrange(10)] gevent.joinall(jobs, raise_error=True) finally: server.close()
# The noted timer jitter issues on appveyor/pypy3 fuzzy = expected * 5.0 else: fuzzy = expected / 2.0 start = time() yield elapsed = time() - start assert expected - fuzzy <= elapsed <= expected + fuzzy, 'Expected: %r; elapsed: %r; fuzzy %r' % ( expected, elapsed, fuzzy) def no_time(fuzzy=(0.001 if not EXPECT_POOR_TIMER_RESOLUTION else 1.0)): return expected_time(0, fuzzy=fuzzy) for _a in xrange(2): # exiting because the spawned greenlet finished execution (spawn (=callback) variant) for _ in xrange(2): x = gevent.spawn(lambda: 5) with no_time(SMALL): result = gevent.wait(timeout=10) assert result is True, repr(result) assert x.dead, x assert x.value == 5, x # exiting because the spawned greenlet finished execution (spawn_later (=timer) variant) for _ in xrange(2): x = gevent.spawn_later(SMALL, lambda: 5) with expected_time(SMALL): result = gevent.wait(timeout=10)
import sys from _six import xrange if 'runtestcase' in sys.argv[1:]: import gevent import gevent.subprocess gevent.spawn(sys.exit, 'bye') gevent.subprocess.Popen('python -c "1/0"'.split()) gevent.sleep(1) else: import subprocess for _ in xrange(5): out, err = subprocess.Popen([sys.executable, __file__, 'runtestcase'], stderr=subprocess.PIPE).communicate() if b'refs' in err: assert err.startswith(b'bye'), repr(err) else: assert err.strip() == b'bye', repr(err)
if EXPECT_POOR_TIMER_RESOLUTION: # The noted timer jitter issues on appveyor/pypy3 fuzzy = expected * 5.0 else: fuzzy = expected / 2.0 start = time() yield elapsed = time() - start assert expected - fuzzy <= elapsed <= expected + fuzzy, 'Expected: %r; elapsed: %r; fuzzy %r' % (expected, elapsed, fuzzy) def no_time(fuzzy=(0.001 if not EXPECT_POOR_TIMER_RESOLUTION else 1.0)): return expected_time(0, fuzzy=fuzzy) for _a in xrange(2): # exiting because the spawned greenlet finished execution (spawn (=callback) variant) for _ in xrange(2): x = gevent.spawn(lambda: 5) with no_time(SMALL): result = gevent.wait(timeout=10) assert result is True, repr(result) assert x.dead, x assert x.value == 5, x # exiting because the spawned greenlet finished execution (spawn_later (=timer) variant) for _ in xrange(2): x = gevent.spawn_later(SMALL, lambda: 5) with expected_time(SMALL): result = gevent.wait(timeout=10)