Commit 2d8a5636 authored by Jason Madden's avatar Jason Madden

Tweak some timing asserts in test__threadpool.py

parent 0bce68b3
...@@ -58,14 +58,14 @@ class TimeAssertMixin(object): ...@@ -58,14 +58,14 @@ class TimeAssertMixin(object):
self.assertGreaterEqual(time_taken, min_time) self.assertGreaterEqual(time_taken, min_time)
@contextmanager @contextmanager
def runs_in_given_time(self, expected, fuzzy=None): def runs_in_given_time(self, expected, fuzzy=None, min_time=None):
if fuzzy is None: if fuzzy is None:
if sysinfo.EXPECT_POOR_TIMER_RESOLUTION or sysinfo.LIBUV: if sysinfo.EXPECT_POOR_TIMER_RESOLUTION or sysinfo.LIBUV:
# The noted timer jitter issues on appveyor/pypy3 # The noted timer jitter issues on appveyor/pypy3
fuzzy = expected * 5.0 fuzzy = expected * 5.0
else: else:
fuzzy = expected / 2.0 fuzzy = expected / 2.0
min_time = expected - fuzzy min_time = min_time if min_time is not None else expected - fuzzy
max_time = expected + fuzzy max_time = expected + fuzzy
start = perf_counter() start = perf_counter()
yield (min_time, max_time) yield (min_time, max_time)
...@@ -73,8 +73,8 @@ class TimeAssertMixin(object): ...@@ -73,8 +73,8 @@ class TimeAssertMixin(object):
try: try:
self.assertTrue( self.assertTrue(
min_time <= elapsed <= max_time, min_time <= elapsed <= max_time,
'Expected: %r; elapsed: %r; fuzzy %r; clock_info: %s' % ( 'Expected: %r; elapsed: %r; min: %r; max: %r; fuzzy %r; clock_info: %s' % (
expected, elapsed, fuzzy, get_clock_info('perf_counter') expected, elapsed, min_time, max_time, fuzzy, get_clock_info('perf_counter')
)) ))
except AssertionError: except AssertionError:
flaky.reraiseFlakyTestRaceCondition() flaky.reraiseFlakyTestRaceCondition()
......
...@@ -212,7 +212,7 @@ class TestPool(_AbstractPoolTest): ...@@ -212,7 +212,7 @@ class TestPool(_AbstractPoolTest):
self.assertEqual(get(), 49) self.assertEqual(get(), 49)
self.assertTimeoutAlmostEqual(get.elapsed, TIMEOUT1, 1) self.assertTimeoutAlmostEqual(get.elapsed, TIMEOUT1, 1)
gevent.sleep(0) # lets the callback run gevent.sleep(0) # lets the callback run
assert result == [49], result self.assertEqual(result, [49])
def test_async_timeout(self): def test_async_timeout(self):
res = self.pool.apply_async(sqr, (6, TIMEOUT2 + 0.2)) res = self.pool.apply_async(sqr, (6, TIMEOUT2 + 0.2))
...@@ -273,9 +273,10 @@ class TestPool(_AbstractPoolTest): ...@@ -273,9 +273,10 @@ class TestPool(_AbstractPoolTest):
size = self.size or 10 size = self.size or 10
result = self.pool.map_async(sleep, [0.1] * (size * 2)) result = self.pool.map_async(sleep, [0.1] * (size * 2))
gevent.sleep(0.1) gevent.sleep(0.1)
kill = TimingWrapper(self.pool.kill) try:
kill() with self.runs_in_given_time(0.1 * self.size + 0.5, min_time=0):
assert kill.elapsed < 0.1 * self.size + 0.5, kill.elapsed self.pool.kill()
finally:
result.join() result.join()
def sleep(self, x): def sleep(self, x):
...@@ -485,7 +486,7 @@ class TestRef(TestCase): ...@@ -485,7 +486,7 @@ class TestRef(TestCase):
# result = func(Object(), kwarg1=Object()) # result = func(Object(), kwarg1=Object())
# but in a thread pool and see that arguments', result's and func's references are not leaked # but in a thread pool and see that arguments', result's and func's references are not leaked
result = pool.apply(func, (Object(), ), {'kwarg1': Object()}) result = pool.apply(func, (Object(), ), {'kwarg1': Object()})
assert isinstance(result, Object), repr(result) self.assertIsInstance(result, Object)
gevent.sleep(0.1) # XXX should not be needed gevent.sleep(0.1) # XXX should not be needed
refs.append(weakref.ref(func)) refs.append(weakref.ref(func))
......
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