Commit c674216c authored by Jason Madden's avatar Jason Madden

test_hub_join_timeout to real test and correct naming.

parent 7fb55e27
...@@ -20,7 +20,9 @@ ...@@ -20,7 +20,9 @@
from __future__ import absolute_import, print_function, division from __future__ import absolute_import, print_function, division
import sys import sys
from time import time
import os.path import os.path
from contextlib import contextmanager
from unittest import TestCase as BaseTestCase from unittest import TestCase as BaseTestCase
from functools import wraps from functools import wraps
...@@ -51,6 +53,23 @@ class TimeAssertMixin(object): ...@@ -51,6 +53,23 @@ class TimeAssertMixin(object):
self.assertLessEqual(time_taken, max_time) self.assertLessEqual(time_taken, max_time)
self.assertGreaterEqual(time_taken, min_time) self.assertGreaterEqual(time_taken, min_time)
@contextmanager
def runs_in_given_time(self, expected, fuzzy=None):
if fuzzy is None:
if sysinfo.EXPECT_POOR_TIMER_RESOLUTION or sysinfo.LIBUV:
# The noted timer jitter issues on appveyor/pypy3
fuzzy = expected * 5.0
else:
fuzzy = expected / 2.0
start = time()
yield
elapsed = time() - start
self.assertTimeWithinRange(elapsed, expected - fuzzy, expected + fuzzy)
def runs_in_no_time(
self,
fuzzy=(0.001 if not sysinfo.EXPECT_POOR_TIMER_RESOLUTION and not sysinfo.LIBUV else 1.0)):
return self.runs_in_given_time(0.0, fuzzy)
def _wrap_timeout(timeout, method): def _wrap_timeout(timeout, method):
......
import functools
import unittest
import gevent
import gevent.core
from gevent.event import Event
from gevent.testing.testcase import TimeAssertMixin
from gevent.testing.timing import SMALL_TICK
# setting up signal does not affect join()
gevent.signal(1, lambda: None) # wouldn't work on windows
def repeated(func, repetitions=2):
@functools.wraps(func)
def f(self):
for _ in range(repetitions):
func(self)
return f
class Test(TimeAssertMixin, unittest.TestCase):
@repeated
def test_callback(self):
# exiting because the spawned greenlet finished execution (spawn (=callback) variant)
x = gevent.spawn(lambda: 5)
with self.runs_in_no_time():
result = gevent.wait(timeout=10)
self.assertTrue(result)
self.assertTrue(x.dead, x)
self.assertEqual(x.value, 5)
@repeated
def test_later(self):
# exiting because the spawned greenlet finished execution (spawn_later (=timer) variant)
x = gevent.spawn_later(SMALL_TICK, lambda: 5)
with self.runs_in_given_time(SMALL_TICK):
result = gevent.wait(timeout=10)
self.assertTrue(result)
self.assertTrue(x.dead, x)
@repeated
def test_timeout(self):
# exiting because of timeout (the spawned greenlet still runs)
x = gevent.spawn_later(10, lambda: 5)
with self.runs_in_given_time(SMALL_TICK):
result = gevent.wait(timeout=SMALL_TICK)
self.assertFalse(result)
self.assertFalse(x.dead, x)
x.kill()
with self.runs_in_no_time():
result = gevent.wait()
self.assertTrue(result)
@repeated
def test_event(self):
# exiting because of event (the spawned greenlet still runs)
x = gevent.spawn_later(10, lambda: 5)
event = Event()
event_set = gevent.spawn_later(SMALL_TICK, event.set)
with self.runs_in_given_time(SMALL_TICK):
result = gevent.wait([event])
self.assertEqual(result, [event])
self.assertFalse(x.dead, x)
self.assertTrue(event_set.dead)
self.assertTrue(event.is_set)
x.kill()
with self.runs_in_no_time():
result = gevent.wait()
self.assertTrue(result)
@repeated
def test_ref_arg(self):
# checking "ref=False" argument
gevent.get_hub().loop.timer(10, ref=False).start(lambda: None)
with self.runs_in_no_time():
result = gevent.wait()
self.assertTrue(result)
@repeated
def test_ref_attribute(self):
# checking "ref=False" attribute
w = gevent.get_hub().loop.timer(10)
w.start(lambda: None)
w.ref = False
with self.runs_in_no_time():
result = gevent.wait()
self.assertTrue(result)
class TestAgain(Test):
"Repeat the same tests"
if __name__ == '__main__':
unittest.main()
from contextlib import contextmanager
import gevent
import gevent.core
from gevent.event import Event
from time import time
from gevent.testing.six import xrange
SMALL = 0.1
FUZZY = SMALL / 2
# setting up signal does not affect join()
gevent.signal(1, lambda: None) # wouldn't work on windows
from gevent.testing import EXPECT_POOR_TIMER_RESOLUTION
EXPECT_POOR_TIMER_RESOLUTION = EXPECT_POOR_TIMER_RESOLUTION or hasattr(gevent.core, 'libuv')
# We observe longer/jittery timeouts running on appveyor or running with libuv
@contextmanager
def expected_time(expected, fuzzy=None):
if fuzzy is None:
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)
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.wait(timeout=SMALL)
assert result is False, repr(result)
assert not x.dead, (x, x._start_event)
x.kill()
with no_time():
result = gevent.wait()
assert result is True
# exiting because of event (the spawned greenlet still runs)
for _ in xrange(2):
x = gevent.spawn_later(10, lambda: 5)
event = Event()
event_set = gevent.spawn_later(SMALL, event.set)
with expected_time(SMALL):
result = gevent.wait([event])
assert result == [event], repr(result)
assert not x.dead, x
assert event_set.dead
assert event.is_set()
x.kill()
with no_time():
result = gevent.wait()
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.wait()
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.wait()
assert result is True
...@@ -5,3 +5,4 @@ test__monkey_scope.py ...@@ -5,3 +5,4 @@ test__monkey_scope.py
test__ares_timeout.py test__ares_timeout.py
test__close_backend_fd.py test__close_backend_fd.py
test__hub_join.py test__hub_join.py
test__hub_join_timeout.py
...@@ -23,3 +23,4 @@ test__iwait.py ...@@ -23,3 +23,4 @@ test__iwait.py
test__ares_timeout.py test__ares_timeout.py
test__close_backend_fd.py test__close_backend_fd.py
test__hub_join.py test__hub_join.py
test__hub_join_timeout.py
...@@ -31,7 +31,7 @@ test__greenlet.py ...@@ -31,7 +31,7 @@ test__greenlet.py
test__greenletset.py test__greenletset.py
# uses socket test__greenness.py # uses socket test__greenness.py
test__hub_join.py test__hub_join.py
test_hub_join_timeout.py test__hub_join_timeout.py
# uses socket test__hub.py # uses socket test__hub.py
test_issue112.py test_issue112.py
test__joinall.py test__joinall.py
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment