artiq/artiq/test/sync_struct.py

81 lines
2.5 KiB
Python
Raw Normal View History

2015-05-29 23:17:10 +08:00
import unittest
import asyncio
import numpy as np
2015-05-29 23:17:10 +08:00
from artiq.protocols import sync_struct
test_address = "::1"
test_port = 7777
def write_test_data(test_dict):
test_values = [5, 2.1, None, True, False,
{"a": 5, 2: np.linspace(0, 10, 1)},
(4, 5), (10,), "ab\nx\"'"]
2015-05-29 23:17:10 +08:00
for i in range(10):
test_dict[str(i)] = i
for key, value in enumerate(test_values):
test_dict[key] = value
test_dict[1.5] = 1.5
test_dict["array"] = []
test_dict["array"].append(42)
test_dict["array"].insert(1, 1)
test_dict[100] = 0
test_dict[100] = 1
test_dict[101] = 1
test_dict.pop(101)
test_dict[102] = 1
del test_dict[102]
2015-06-04 10:42:37 +08:00
test_dict["finished"] = True
2015-05-29 23:17:10 +08:00
2015-10-03 19:28:57 +08:00
async def start_server(publisher_future, test_dict_future):
2015-05-29 23:17:10 +08:00
test_dict = sync_struct.Notifier(dict())
publisher = sync_struct.Publisher(
{"test": test_dict})
2015-10-03 19:28:57 +08:00
await publisher.start(test_address, test_port)
2015-05-29 23:17:10 +08:00
publisher_future.set_result(publisher)
test_dict_future.set_result(test_dict)
2015-06-03 16:46:09 +08:00
class SyncStructCase(unittest.TestCase):
2015-05-29 23:17:10 +08:00
def init_test_dict(self, init):
self.test_dict = init
return init
def notify(self, mod):
2015-06-06 00:55:36 +08:00
if ((mod["action"] == "init" and "finished" in mod["struct"])
or (mod["action"] == "setitem" and mod["key"] == "finished")):
self.receiving_done.set()
2015-05-29 23:17:10 +08:00
def setUp(self):
self.loop = asyncio.new_event_loop()
asyncio.set_event_loop(self.loop)
2015-05-29 23:17:10 +08:00
def test_recv(self):
loop = self.loop
self.receiving_done = asyncio.Event()
2015-05-29 23:17:10 +08:00
publisher = asyncio.Future()
test_dict = asyncio.Future()
2015-10-03 14:37:02 +08:00
asyncio.ensure_future(start_server(publisher, test_dict))
2015-05-29 23:17:10 +08:00
loop.run_until_complete(publisher)
loop.run_until_complete(test_dict)
self.publisher = publisher.result()
test_dict = test_dict.result()
test_vector = dict()
2015-10-03 19:28:57 +08:00
write_test_data(test_vector)
2015-05-29 23:17:10 +08:00
2015-10-03 19:28:57 +08:00
write_test_data(test_dict)
self.subscriber = sync_struct.Subscriber("test", self.init_test_dict,
self.notify)
2015-05-29 23:17:10 +08:00
loop.run_until_complete(self.subscriber.connect(test_address,
test_port))
loop.run_until_complete(self.receiving_done.wait())
2015-05-29 23:17:10 +08:00
self.assertEqual(self.test_dict, test_vector)
self.loop.run_until_complete(self.subscriber.close())
self.loop.run_until_complete(self.publisher.stop())
def tearDown(self):
2015-05-29 23:17:10 +08:00
self.loop.close()