Beispiel #1
0
 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')
Beispiel #2
0
    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)))
Beispiel #3
0
 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:
         self.assertTrue(sender.ready(), sender)
     else:
         expected_len = min(self.count, expected_len)
         self.assertFalse(sender.ready(), sender)
         sender.kill()
     self.assertEqual(expected_len, len(results),
                      (expected_len, len(results), results))
Beispiel #4
0
 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)
Beispiel #5
0
 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
     import warnings
     with warnings.catch_warnings():
         warnings.simplefilter('ignore', DeprecationWarning)
         # get/set checkinterval are deprecated in Python 3
         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)
Beispiel #6
0
            # 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)