forked from projectmesa/mesa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_tornado.py
39 lines (31 loc) · 1.24 KB
/
test_tornado.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
from tornado.testing import AsyncHTTPTestCase
import tornado
from mesa import Model
from mesa.visualization.ModularVisualization import ModularServer
import json
class TestServer(AsyncHTTPTestCase):
def get_app(self):
app = ModularServer(Model, [])
return app
def test_homepage(self):
response = self.fetch("/")
assert response.error is None
@tornado.testing.gen_test
def test_websocket(self):
ws_url = "ws://localhost:" + str(self.get_http_port()) + "/ws"
ws_client = yield tornado.websocket.websocket_connect(ws_url)
# Now we can run a test on the WebSocket.
response = yield ws_client.read_message()
msg = json.loads(response)
assert msg["type"] == "model_params"
ws_client.write_message('{"type": "get_step"}')
response = yield ws_client.read_message()
msg = json.loads(response)
assert msg["type"] == "viz_state"
ws_client.write_message('{"type": "reset"}')
response = yield ws_client.read_message()
msg = json.loads(response)
assert msg["type"] == "viz_state"
ws_client.write_message("Unknown message!")
response = yield ws_client.read_message()
assert response is None