Commits

Denis Bilenko  committed 92a9743

extend test_hub_join_timeout.py to check "ref" property

  • Participants
  • Parent commits 65ccdaf

Comments (0)

Files changed (1)

File greentest/test_hub_join_timeout.py

+from __future__ import with_statement
+from contextlib import contextmanager
 import gevent
 from time import time
 
 SMALL = 0.1
 FUZZY = SMALL / 2
 
+# setting up signal does not affect join()
+gevent.signal(1, lambda : None) # wouldn't work on windows
+
+
+@contextmanager
+def expected_time(expected, fuzzy=None):
+    if fuzzy is None:
+        fuzzy = expected / 2.
+    start = time()
+    yield
+    elapsed = time() - start
+    assert expected - fuzzy <= elapsed <= expected + fuzzy, 'Expected: %r; elapsed: %r' % (expected, elapsed)
+
+
+def no_time(fuzzy=0.001):
+    return expected_time(0, fuzzy=fuzzy)
+
 
 for _a in xrange(2):
 
-    for _b in xrange(2):
-        gevent.spawn_later(SMALL, lambda: 5)
-        start = time()
-        result = gevent.get_hub().join(timeout=10)
+    # 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.get_hub().join(timeout=10)
         assert result is True, repr(result)
-        delay = time() - start
-        assert SMALL - FUZZY <= delay <= SMALL + FUZZY, delay
+        assert x.dead, x
+        assert x.value == 5, x
 
-    for _c in xrange(2):
-        gevent.spawn_later(10, lambda: 5)
-        start = time()
-        result = gevent.get_hub().join(timeout=SMALL)
+    # 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.get_hub().join(timeout=10)
+        assert result is True, repr(result)
+        assert x.dead, x
+
+    # exiting because of timeout (the spawned greenlet still runs)
+    for _ in xrange(2):
+        x = gevent.spawn_later(10, lambda: 5)
+        with expected_time(SMALL):
+            result = gevent.get_hub().join(timeout=SMALL)
         assert result is None, repr(result)
-        delay = time() - start
-        assert SMALL - FUZZY <= delay <= SMALL + FUZZY, delay
+        assert not x.dead, x
+        x.kill()
+        with no_time():
+            result = gevent.get_hub().join()
+        assert result is True
+
+    # checking "ref=False" argument
+    for _ in xrange(2):
+        gevent.get_hub().loop.timer(10, ref=False).start(lambda : None)
+        with no_time():
+            result = gevent.get_hub().join()
+        assert result is True
+
+    # checking "ref=False" attribute
+    for _d in xrange(2):
+        w = gevent.get_hub().loop.timer(10)
+        w.start(lambda : None)
+        w.ref = False
+        with no_time():
+            result = gevent.get_hub().join()
+        assert result is True