Skip to content

Commit c7953da

Browse files
committedJun 3, 2015
test: add unittest for sync_struct
1 parent 140239f commit c7953da

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed
 

‎artiq/test/sync_struct.py

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import unittest
2+
import asyncio
3+
4+
from artiq.protocols import sync_struct
5+
6+
test_address = "::1"
7+
test_port = 7777
8+
9+
10+
@asyncio.coroutine
11+
def write_test_data(test_dict):
12+
for i in range(10):
13+
test_dict[str(i)] = i
14+
test_dict["Finished"] = True
Has a conversation. Original line has a conversation.
15+
16+
17+
@asyncio.coroutine
18+
def start_server(publisher_future, test_dict_future):
19+
test_dict = sync_struct.Notifier(dict())
20+
publisher = sync_struct.Publisher(
21+
{"test": test_dict})
22+
yield from publisher.start(test_address, test_port)
23+
publisher_future.set_result(publisher)
24+
test_dict_future.set_result(test_dict)
25+
26+
27+
class RPCCase(unittest.TestCase):
Has conversations. Original line has conversations.
28+
def init_test_dict(self, init):
29+
self.test_dict = init
30+
return init
31+
32+
@asyncio.coroutine
33+
def do_recv(self):
34+
while not hasattr(self, "test_dict")\
35+
or "Finished" not in self.test_dict.keys():
36+
yield from asyncio.sleep(0.5)
Has a conversation. Original line has a conversation.
37+
38+
def test_recv(self):
39+
self.loop = loop = asyncio.new_event_loop()
Has conversations. Original line has conversations.
40+
asyncio.set_event_loop(loop)
41+
publisher = asyncio.Future()
42+
test_dict = asyncio.Future()
43+
asyncio.async(start_server(publisher, test_dict))
44+
loop.run_until_complete(publisher)
45+
loop.run_until_complete(test_dict)
46+
47+
self.publisher = publisher.result()
48+
test_dict = test_dict.result()
49+
test_vector = dict()
50+
loop.run_until_complete(write_test_data(test_vector))
51+
52+
asyncio.async(write_test_data(test_dict))
53+
self.subscriber = sync_struct.Subscriber("test", self.init_test_dict)
54+
loop.run_until_complete(self.subscriber.connect(test_address,
55+
test_port))
56+
loop.run_until_complete(self.do_recv())
57+
self.assertEqual(self.test_dict, test_vector)
58+
59+
def tearDown(self):
60+
self.loop.run_until_complete(self.subscriber.close())
61+
self.loop.run_until_complete(self.publisher.stop())
62+
self.loop.close()

0 commit comments

Comments
 (0)
Please sign in to comment.