Skip to content

Commit f136bb3

Browse files
author
jparisu
committed
fix time in waiter tests
Signed-off-by: jparisu <javierparis@eprosima.com>
1 parent 930e7b2 commit f136bb3

3 files changed

Lines changed: 21 additions & 6 deletions

File tree

py_utils/test/py_utils/unittest/wait/test_BooleanWaitHander.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525

2626
TEST_SLEEP_TIME = 0.5
27-
TEST_RESIDUAL_TIME = 0.1
27+
TEST_RESIDUAL_TIME = TEST_SLEEP_TIME / 5
2828

2929

3030
def test_ctor_trivial():
@@ -48,13 +48,18 @@ def condition_met_waiter_function(waiter):
4848
time.sleep(TEST_SLEEP_TIME)
4949
waiter.open()
5050
t = threading.Thread(target=condition_met_waiter_function, args=(waiter,))
51-
t.start()
5251

5352
# Create a Timer to check is actually waiting
5453
timer = Timer()
5554

55+
# Execute thread AFTER start timer (otherwise it could happen that elapsed time is lower)
56+
t.start()
57+
5658
# Wait till condition met
5759
waiter.wait()
5860

61+
# Get time elapsed waiting
5962
elapsed = timer.elapsed()
63+
64+
# Check that such time is higher than wait time, and lower than it plus a residual time
6065
assert elapsed >= TEST_SLEEP_TIME and elapsed <= TEST_SLEEP_TIME + TEST_RESIDUAL_TIME

py_utils/test/py_utils/unittest/wait/test_IntWaitHander.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525

2626
TEST_SLEEP_TIME = 0.5
27-
TEST_RESIDUAL_TIME = 0.1
27+
TEST_RESIDUAL_TIME = TEST_SLEEP_TIME / 5
2828

2929

3030
def test_ctor_trivial():
@@ -56,15 +56,20 @@ def condition_met_waiter_function(waiter):
5656
waiter.decrease(3)
5757

5858
t = threading.Thread(target=condition_met_waiter_function, args=(waiter,))
59-
t.start()
6059

6160
# Create a Timer to check is actually waiting
6261
timer = Timer()
6362

63+
# Execute thread AFTER start timer (otherwise it could happen that elapsed time is lower)
64+
t.start()
65+
6466
# Wait till condition met
6567
waiter.wait_greater_equal(1)
6668
waiter.wait_greater(2)
6769
waiter.wait_equal(0)
6870

71+
# Get time elapsed waiting
6972
elapsed = timer.elapsed()
73+
74+
# Check that such time is in range expected
7075
assert elapsed >= TEST_SLEEP_TIME * 3 and elapsed <= TEST_SLEEP_TIME * 3 + TEST_RESIDUAL_TIME

py_utils/test/py_utils/unittest/wait/test_WaitHander.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525

2626
TEST_SLEEP_TIME = 0.5
27-
TEST_RESIDUAL_TIME = 0.1
27+
TEST_RESIDUAL_TIME = TEST_SLEEP_TIME / 5
2828

2929

3030
def test_ctor_trivial():
@@ -42,16 +42,21 @@ def condition_met_waiter_function(waiter):
4242
time.sleep(TEST_SLEEP_TIME)
4343
waiter.set_value(1)
4444
t = threading.Thread(target=condition_met_waiter_function, args=(waiter,))
45-
t.start()
4645

4746
# Create a Timer to check is actually waiting
4847
timer = Timer()
4948

49+
# Execute thread AFTER start timer (otherwise it could happen that elapsed time is lower)
50+
t.start()
51+
5052
# Wait till condition met
5153
awaken_reason = waiter.wait(lambda v: v == 1)
5254
assert awaken_reason == AwakeReason.condition_met
5355

56+
# Get time elapsed waiting
5457
elapsed = timer.elapsed()
58+
59+
# Check that such time is in range expected
5560
assert elapsed >= TEST_SLEEP_TIME and elapsed <= TEST_SLEEP_TIME + TEST_RESIDUAL_TIME
5661

5762

0 commit comments

Comments
 (0)