Fix noparallel test on slow cpu

This commit is contained in:
shortcutme 2019-01-20 19:07:48 +01:00
parent 44dc3035a5
commit 9e5be7ffcf
No known key found for this signature in database
GPG key ID: 5B63BAE6CB9613AE

View file

@ -3,27 +3,28 @@ import time
import util import util
import gevent import gevent
class ExampleClass(object): class ExampleClass(object):
def __init__(self): def __init__(self):
self.counted = 0 self.counted = 0
@util.Noparallel() @util.Noparallel()
def countBlocking(self, num=5): def countBlocking(self, num=5):
for i in range(1, num+1): for i in range(1, num + 1):
time.sleep(0.01) time.sleep(0.01)
self.counted += 1 self.counted += 1
return "counted:%s" % i return "counted:%s" % i
@util.Noparallel(queue=True, ignore_class=True) @util.Noparallel(queue=True, ignore_class=True)
def countQueue(self, num=5): def countQueue(self, num=5):
for i in range(1, num+1): for i in range(1, num + 1):
time.sleep(0.01) time.sleep(0.1)
self.counted += 1 self.counted += 1
return "counted:%s" % i return "counted:%s" % i
@util.Noparallel(blocking=False) @util.Noparallel(blocking=False)
def countNoblocking(self, num=5): def countNoblocking(self, num=5):
for i in range(1, num+1): for i in range(1, num + 1):
time.sleep(0.01) time.sleep(0.01)
self.counted += 1 self.counted += 1
return "counted:%s" % i return "counted:%s" % i
@ -43,7 +44,7 @@ class TestNoparallel:
] ]
assert obj2.countBlocking() == "counted:5" # The call is ignored as obj2.countBlocking already counting, but block until its finishes assert obj2.countBlocking() == "counted:5" # The call is ignored as obj2.countBlocking already counting, but block until its finishes
gevent.joinall(threads) gevent.joinall(threads)
assert [thread.value for thread in threads] == ["counted:5","counted:5","counted:5","counted:5"] # Check the return value for every call assert [thread.value for thread in threads] == ["counted:5", "counted:5", "counted:5", "counted:5"] # Check the return value for every call
obj2.countBlocking() # Allow to call again as obj2.countBlocking finished obj2.countBlocking() # Allow to call again as obj2.countBlocking finished
assert obj1.counted == 5 assert obj1.counted == 5
@ -72,24 +73,21 @@ class TestNoparallel:
gevent.spawn(obj1.countQueue, num=10) gevent.spawn(obj1.countQueue, num=10)
gevent.spawn(obj1.countQueue, num=10) gevent.spawn(obj1.countQueue, num=10)
time.sleep(0.3) time.sleep(3.0)
assert obj1.counted == 20 # No multi-queue supported assert obj1.counted == 20 # No multi-queue supported
obj2 = ExampleClass() obj2 = ExampleClass()
gevent.spawn(obj2.countQueue, num=10) gevent.spawn(obj2.countQueue, num=10)
gevent.spawn(obj2.countQueue, num=10) gevent.spawn(obj2.countQueue, num=10)
time.sleep(0.15) # Call 1 finished, call 2 still working time.sleep(1.5) # Call 1 finished, call 2 still working
assert 10 < obj2.counted < 20 assert 10 < obj2.counted < 20
gevent.spawn(obj2.countQueue, num=10) gevent.spawn(obj2.countQueue, num=10)
time.sleep(0.20) time.sleep(2.0)
assert obj2.counted == 30 assert obj2.counted == 30
def testQueueOverload(self): def testQueueOverload(self):
obj1 = ExampleClass() obj1 = ExampleClass()
@ -119,4 +117,4 @@ class TestNoparallel:
assert obj1.counted + obj2.counted == 10 assert obj1.counted + obj2.counted == 10
taken = time.time() - s taken = time.time() - s
assert 0.12 > taken >= 0.1 # 2 * 0.05s count = ~0.1s assert 1.1 > taken >= 1.0 # 2 * 0.5s count = ~1s