-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtester.py
62 lines (50 loc) · 1.8 KB
/
tester.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
from math import isclose
from os import environ
from socket import create_connection
from subprocess import Popen, PIPE
from sys import executable
from time import sleep, time
# Do it that way to minimize overhead when running testee
from testee import SLEEP_DURATION
COMMAND = ["./timeskew.exe", executable, "testee.py"]
TOLERANCE = 0.1
EXPECTED = "expected.log"
PORT = 40000
print(f'{SLEEP_DURATION=}')
print(f'{TOLERANCE=}')
def run_testee_with_env(**env: str) -> Popen:
return Popen(COMMAND, stdin=PIPE, stdout=PIPE, env={**env, **environ})
def test_relative_time(p: Popen, time_ratio: float) -> None:
sleep(.1)
p.stdin.write(b'\n')
p.stdin.flush()
start = time()
assert p.wait() == 0
real_elapsed = time() - start
fake_elapsed = float(p.stdout.read().decode())
print(f'{real_elapsed=}')
print(f'{fake_elapsed=}')
# Check whether the duration is consistent in real and skewed time
assert isclose(real_elapsed, SLEEP_DURATION / time_ratio, rel_tol=TOLERANCE)
assert isclose(fake_elapsed, SLEEP_DURATION, rel_tol=TOLERANCE)
print('Test with no time acceleration')
p = run_testee_with_env()
test_relative_time(p, 1)
print('Test time acceleration with environment-variables')
p = run_testee_with_env(TIMESKEW="10 1")
test_relative_time(p, 10)
print('Test time slow-down with environment-variables')
p = run_testee_with_env(TIMESKEW="1 10")
test_relative_time(p, .1)
print('Test time acceleration with port')
p = run_testee_with_env(TIMESKEW_PORT=str(PORT))
sleep(.1)
with create_connection(('127.0.0.1', PORT)) as sock:
sock.sendall(b"10 1\n")
test_relative_time(p, 10)
print('Test time slow-down with port')
p = run_testee_with_env(TIMESKEW_PORT=str(PORT))
sleep(.1)
with create_connection(('127.0.0.1', PORT)) as sock:
sock.sendall(b"1 10\n")
test_relative_time(p, .1)