Skip to content

Commit 55a63f5

Browse files
bluetechpgjones
authored andcommitted
Reform compliance/ with Black and isort
For consistency with the rest of the project.
1 parent 18756d5 commit 55a63f5

File tree

4 files changed

+130
-74
lines changed

4 files changed

+130
-74
lines changed

compliance/run-autobahn-tests.py

Lines changed: 78 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -3,74 +3,80 @@
33

44
from __future__ import print_function
55

6-
import sys
7-
import os.path
86
import argparse
7+
import copy
98
import errno
10-
import subprocess
119
import json
10+
import os.path
1211
import socket
12+
import subprocess
13+
import sys
1314
import time
14-
import copy
1515

1616
PORT = 8642
1717

1818
CLIENT_CONFIG = {
19-
"options": {"failByDrop": False},
20-
"outdir": "./reports/servers",
21-
22-
"servers": [
23-
{
24-
"agent": "wsproto",
25-
"url": "ws://localhost:{}".format(PORT),
26-
"options": {"version": 18},
27-
},
28-
],
29-
30-
"cases": ["*"],
31-
"exclude-cases": [],
32-
"exclude-agent-cases": {},
19+
"options": {"failByDrop": False},
20+
"outdir": "./reports/servers",
21+
"servers": [
22+
{
23+
"agent": "wsproto",
24+
"url": "ws://localhost:{}".format(PORT),
25+
"options": {"version": 18},
26+
}
27+
],
28+
"cases": ["*"],
29+
"exclude-cases": [],
30+
"exclude-agent-cases": {},
3331
}
3432

3533
SERVER_CONFIG = {
36-
"url": "ws://localhost:{}".format(PORT),
37-
38-
"options": {"failByDrop": False},
39-
"outdir": "./reports/clients",
40-
"webport": 8080,
41-
42-
"cases": ["*"],
43-
"exclude-cases": [],
44-
"exclude-agent-cases": {}
34+
"url": "ws://localhost:{}".format(PORT),
35+
"options": {"failByDrop": False},
36+
"outdir": "./reports/clients",
37+
"webport": 8080,
38+
"cases": ["*"],
39+
"exclude-cases": [],
40+
"exclude-agent-cases": {},
4541
}
4642

4743
CASES = {
4844
"all": ["*"],
49-
"fast":
45+
"fast": [
5046
# The core functionality tests
51-
["{}.*".format(i) for i in range(1, 12)]
47+
*["{}.*".format(i) for i in range(1, 12)],
5248
# Compression tests -- in each section, the tests get progressively
5349
# slower until they're taking 10s of seconds apiece. And it's
5450
# mostly stress tests, without much extra coverage to show for
5551
# it. (Weird trick: autobahntestsuite treats these as regexps
5652
# except that . is quoted and * becomes .*)
57-
+ ["12.*.[1234]$", "13.*.[1234]$"]
53+
"12.*.[1234]$",
54+
"13.*.[1234]$",
5855
# At one point these were catching a unique bug that none of the
5956
# above were -- they're relatively quick and involve
6057
# fragmentation.
61-
+ ["12.1.11", "12.1.12", "13.1.11", "13.1.12"],
58+
"12.1.11",
59+
"12.1.12",
60+
"13.1.11",
61+
"13.1.12",
62+
],
6263
}
6364

65+
6466
def say(*args):
6567
print("run-autobahn-tests.py:", *args)
6668

69+
6770
def setup_venv():
6871
if not os.path.exists("autobahntestsuite-venv"):
6972
say("Creating Python 2.7 environment and installing autobahntestsuite")
7073
subprocess.check_call(
71-
["virtualenv", "-p", "python2.7", "autobahntestsuite-venv"])
74+
["virtualenv", "-p", "python2.7", "autobahntestsuite-venv"]
75+
)
7276
subprocess.check_call(
73-
["autobahntestsuite-venv/bin/pip", "install", "autobahntestsuite>=0.8.0"])
77+
["autobahntestsuite-venv/bin/pip", "install", "autobahntestsuite>=0.8.0"]
78+
)
79+
7480

7581
def wait_for_listener(port):
7682
while True:
@@ -87,13 +93,20 @@ def wait_for_listener(port):
8793
finally:
8894
sock.close()
8995

96+
9097
def coverage(command, coverage_settings):
9198
if not coverage_settings["enabled"]:
9299
return [sys.executable] + command
93100

94-
return ([sys.executable, "-m", "coverage", "run",
95-
"--include", coverage_settings["wsproto-path"]]
96-
+ command)
101+
return [
102+
sys.executable,
103+
"-m",
104+
"coverage",
105+
"run",
106+
"--include",
107+
coverage_settings["wsproto-path"],
108+
] + command
109+
97110

98111
def summarize(report_path):
99112
with open(os.path.join(report_path, "index.json")) as f:
@@ -103,31 +116,36 @@ def summarize(report_path):
103116
PASS = {"OK", "INFORMATIONAL"}
104117
for test_name, results in sorted(result_summary.items()):
105118
total += 1
106-
if (results["behavior"] not in PASS
107-
or results["behaviorClose"] not in PASS):
119+
if results["behavior"] not in PASS or results["behaviorClose"] not in PASS:
108120
say("FAIL:", test_name, results)
109121
say("Details:")
110122
with open(os.path.join(report_path, results["reportfile"])) as f:
111123
print(f.read())
112124
failed += 1
113125

114-
speed_ordered = sorted(result_summary.items(),
115-
key=lambda kv: -kv[1]["duration"])
126+
speed_ordered = sorted(result_summary.items(), key=lambda kv: -kv[1]["duration"])
116127
say("Slowest tests:")
117128
for test_name, results in speed_ordered[:5]:
118129
say(" {}: {} seconds".format(test_name, results["duration"] / 1000))
119130

120131
return failed, total
121132

133+
122134
def run_client_tests(cases, coverage_settings):
123135
say("Starting autobahntestsuite server")
124136
server_config = copy.deepcopy(SERVER_CONFIG)
125137
server_config["cases"] = cases
126138
with open("auto-tests-server-config.json", "w") as f:
127139
json.dump(server_config, f)
128140
server = subprocess.Popen(
129-
["autobahntestsuite-venv/bin/wstest", "-m", "fuzzingserver",
130-
"-s", "auto-tests-server-config.json"])
141+
[
142+
"autobahntestsuite-venv/bin/wstest",
143+
"-m",
144+
"fuzzingserver",
145+
"-s",
146+
"auto-tests-server-config.json",
147+
]
148+
)
131149
say("Waiting for server to start")
132150
wait_for_listener(PORT)
133151
try:
@@ -143,6 +161,7 @@ def run_client_tests(cases, coverage_settings):
143161

144162
return summarize("reports/clients")
145163

164+
146165
def run_server_tests(cases, coverage_settings):
147166
say("Starting wsproto test server")
148167
server = subprocess.Popen(coverage(["./test_server.py"], coverage_settings))
@@ -156,8 +175,14 @@ def run_server_tests(cases, coverage_settings):
156175
json.dump(client_config, f)
157176
say("Starting autobahntestsuite client")
158177
subprocess.check_call(
159-
["autobahntestsuite-venv/bin/wstest", "-m", "fuzzingclient",
160-
"-s", "auto-tests-client-config.json"])
178+
[
179+
"autobahntestsuite-venv/bin/wstest",
180+
"-m",
181+
"fuzzingclient",
182+
"-s",
183+
"auto-tests-client-config.json",
184+
]
185+
)
161186
finally:
162187
say("Stopping server...")
163188
# Connection on this port triggers a shutdown
@@ -168,13 +193,12 @@ def run_server_tests(cases, coverage_settings):
168193

169194
return summarize("reports/servers")
170195

196+
171197
def main():
172198
if not os.path.exists("test_client.py"):
173199
say("Run me from the compliance/ directory")
174200
sys.exit(2)
175-
coverage_settings = {
176-
"coveragerc": "../.coveragerc",
177-
}
201+
coverage_settings = {"coveragerc": "../.coveragerc"}
178202
try:
179203
import wsproto
180204
except ImportError:
@@ -188,16 +212,16 @@ def main():
188212
parser.add_argument("MODE", help="'client' or 'server'")
189213
# can do e.g.
190214
# --cases='["1.*"]'
191-
parser.add_argument("--cases",
192-
help="'fast' or 'all' or a JSON list",
193-
default="fast")
215+
parser.add_argument(
216+
"--cases", help="'fast' or 'all' or a JSON list", default="fast"
217+
)
194218
parser.add_argument("--cov", help="enable coverage", action="store_true")
195219

196220
args = parser.parse_args()
197221

198222
coverage_settings["enabled"] = args.cov
199223
cases = args.cases
200-
#pylint: disable=consider-using-get
224+
# pylint: disable=consider-using-get
201225
if cases in CASES:
202226
cases = CASES[cases]
203227
else:
@@ -213,8 +237,9 @@ def main():
213237
say("Unrecognized mode, try 'client' or 'server'")
214238
sys.exit(2)
215239

216-
say("in {} mode: failed {} out of {} total"
217-
.format(args.MODE.upper(), failed, total))
240+
say(
241+
"in {} mode: failed {} out of {} total".format(args.MODE.upper(), failed, total)
242+
)
218243

219244
if failed:
220245
say("Test failed")

compliance/test_client.py

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,25 @@
44

55
from wsproto import WSConnection
66
from wsproto.connection import CLIENT
7-
from wsproto.events import AcceptConnection, CloseConnection, Ping, Request, TextMessage, Message
7+
from wsproto.events import (
8+
AcceptConnection,
9+
CloseConnection,
10+
Message,
11+
Ping,
12+
Request,
13+
TextMessage,
14+
)
815
from wsproto.extensions import PerMessageDeflate
916
from wsproto.frame_protocol import CloseReason
1017

11-
SERVER = 'ws://127.0.0.1:8642'
12-
AGENT = 'wsproto'
18+
SERVER = "ws://127.0.0.1:8642"
19+
AGENT = "wsproto"
1320

1421
CONNECTION_EXCEPTIONS = (ConnectionError, OSError)
1522

23+
1624
def get_case_count(server):
17-
uri = urlparse(server + '/getCaseCount')
25+
uri = urlparse(server + "/getCaseCount")
1826
connection = WSConnection(CLIENT)
1927
sock = socket.socket()
2028
sock.connect((uri.hostname, uri.port or 80))
@@ -32,7 +40,9 @@ def get_case_count(server):
3240
data += event.data
3341
if event.message_finished:
3442
case_count = json.loads(data)
35-
out_data += connection.send(CloseConnection(code=CloseReason.NORMAL_CLOSURE))
43+
out_data += connection.send(
44+
CloseConnection(code=CloseReason.NORMAL_CLOSURE)
45+
)
3646
try:
3747
sock.sendall(out_data)
3848
except CONNECTION_EXCEPTIONS:
@@ -41,17 +51,21 @@ def get_case_count(server):
4151
sock.close()
4252
return case_count
4353

54+
4455
def run_case(server, case, agent):
45-
uri = urlparse(server + '/runCase?case=%d&agent=%s' % (case, agent))
56+
uri = urlparse(server + "/runCase?case=%d&agent=%s" % (case, agent))
4657
connection = WSConnection(CLIENT)
4758
sock = socket.socket()
4859
sock.connect((uri.hostname, uri.port or 80))
4960

5061
sock.sendall(
51-
connection.send(Request(
52-
host=uri.netloc, target='%s?%s' % (uri.path, uri.query),
53-
extensions=[PerMessageDeflate()],
54-
))
62+
connection.send(
63+
Request(
64+
host=uri.netloc,
65+
target="%s?%s" % (uri.path, uri.query),
66+
extensions=[PerMessageDeflate()],
67+
)
68+
)
5569
)
5670
closed = False
5771

@@ -64,7 +78,9 @@ def run_case(server, case, agent):
6478
out_data = b""
6579
for event in connection.events():
6680
if isinstance(event, Message):
67-
out_data += connection.send(Message(data=event.data, message_finished=event.message_finished))
81+
out_data += connection.send(
82+
Message(data=event.data, message_finished=event.message_finished)
83+
)
6884
elif isinstance(event, Ping):
6985
out_data += connection.send(event.response())
7086
elif isinstance(event, CloseConnection):
@@ -80,14 +96,17 @@ def run_case(server, case, agent):
8096
closed = True
8197
break
8298

99+
83100
def update_reports(server, agent):
84-
uri = urlparse(server + '/updateReports?agent=%s' % agent)
101+
uri = urlparse(server + "/updateReports?agent=%s" % agent)
85102
connection = WSConnection(CLIENT)
86103
sock = socket.socket()
87104
sock.connect((uri.hostname, uri.port or 80))
88105

89106
sock.sendall(
90-
connection.send(Request(host=uri.netloc, target='%s?%s' % (uri.path, uri.query)))
107+
connection.send(
108+
Request(host=uri.netloc, target="%s?%s" % (uri.path, uri.query))
109+
)
91110
)
92111
closed = False
93112

@@ -96,14 +115,17 @@ def update_reports(server, agent):
96115
connection.receive_data(data)
97116
for event in connection.events():
98117
if isinstance(event, AcceptConnection):
99-
sock.sendall(connection.send(CloseConnection(code=CloseReason.NORMAL_CLOSURE)))
118+
sock.sendall(
119+
connection.send(CloseConnection(code=CloseReason.NORMAL_CLOSURE))
120+
)
100121
try:
101122
sock.close()
102123
except CONNECTION_EXCEPTIONS:
103124
pass
104125
finally:
105126
closed = True
106127

128+
107129
CASE = None
108130
# 1.1.1 = 1
109131
# 2.1 = 17
@@ -114,6 +136,7 @@ def update_reports(server, agent):
114136
# 12.1.1 = 304
115137
# 13.1.1 = 394
116138

139+
117140
def run_tests(server, agent):
118141
case_count = get_case_count(server)
119142
if CASE is not None:
@@ -126,5 +149,6 @@ def run_tests(server, agent):
126149
print("\nRan %d cases." % case_count)
127150
update_reports(server, agent)
128151

129-
if __name__ == '__main__':
152+
153+
if __name__ == "__main__":
130154
run_tests(SERVER, AGENT)

0 commit comments

Comments
 (0)