-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
repeat.py
64 lines (54 loc) · 1.62 KB
/
repeat.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
63
64
import subprocess
import difflib
import sys
# This file is used to test some race conditions.
# Two race conditions to worry about
#
# Scencario 1: done-then-dispatch
# 1. Parsing thread sets p->done
# 2. learning thread get_example notes end_parsed_examples = used_index
# 3. learning thread early terminates.
# 4. Parsing thread calls dispatch_example.
#
# Scenario 2: dispatch-then-done
# 1. parser thread dispatches example
# 2. learner thread consumes examples
# 3. learner thread hangs on p->example_available
# 4. parser thread sets done to true.
#
# Fix: Use scenario 2 but have done raise the examples_available flag to unblock the learning thread.
count = int(sys.argv[1])
cmd = sys.argv[2:]
counter = 0
next_print = 1
error = 0
expected = None
for counter in range(1, count + 1):
p = subprocess.Popen(
cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True
)
out, err = p.communicate("")
out += err or ""
if expected is None:
expected = out
continue
failed = False
if p.wait() != 0:
failed = True
sys.stderr.write("Return code: %s\n" % p.wait())
if expected != out:
failed = True
sys.stderr.write("\nOutput changed:\n\n")
d = difflib.Differ()
diff = d.compare(expected.split("\n"), out.split("\n"))
sys.stderr.write("\n".join(diff) + "\n")
if failed:
error += 1
if counter >= next_print or failed:
sys.stderr.write("%s failed out of%5s\n" % (error, counter))
if counter >= next_print:
next_print *= 2
if error:
break
if error:
sys.exit(1)