|
| 1 | +import unittest |
| 2 | +from qunetsim.components import Host, Network |
| 3 | +from qunetsim.backends import EQSNBackend |
| 4 | +import time |
| 5 | + |
| 6 | +network = Network.get_instance() |
| 7 | +hosts = {} |
| 8 | + |
| 9 | +class TestGetClassicalAnyHost(unittest.TestCase): |
| 10 | + |
| 11 | + MAX_WAIT_TIME = 10 |
| 12 | + |
| 13 | + @classmethod |
| 14 | + def setUpClass(cls): |
| 15 | + global network |
| 16 | + global hosts |
| 17 | + nodes = ["Alice", "Bob"] |
| 18 | + backend = EQSNBackend() |
| 19 | + network.start(nodes=nodes, backend=backend) |
| 20 | + hosts = {'alice': Host('Alice', backend), |
| 21 | + 'bob': Host('Bob', backend)} |
| 22 | + hosts['alice'].add_connection('Bob') |
| 23 | + hosts['bob'].add_connection('Alice') |
| 24 | + hosts['alice'].start() |
| 25 | + hosts['bob'].start() |
| 26 | + for h in hosts.values(): |
| 27 | + network.add_host(h) |
| 28 | + |
| 29 | + def setUp(self) -> None: |
| 30 | + hosts['bob']._classical_messages.empty() |
| 31 | + hosts['alice']._classical_messages.empty() |
| 32 | + |
| 33 | + @classmethod |
| 34 | + def tearDownClass(cls): |
| 35 | + global network |
| 36 | + global hosts |
| 37 | + network.stop(stop_hosts=True) |
| 38 | + |
| 39 | + |
| 40 | + |
| 41 | + def test_get_all_with_wait_time(self): |
| 42 | + def listen_with_wait_time(s): |
| 43 | + time.sleep(2) |
| 44 | + msgs = hosts['bob'].get_classical_any_host(seq_num=None, wait=self.MAX_WAIT_TIME) |
| 45 | + self.assertEqual([x.content for x in msgs],['3','2','1']) |
| 46 | + |
| 47 | + def send_some_with_delay(s): |
| 48 | + hosts['alice'].send_classical(hosts['bob'].host_id,str(1),await_ack=True) |
| 49 | + hosts['alice'].send_classical(hosts['bob'].host_id,str(2),await_ack=True) |
| 50 | + time.sleep(5) |
| 51 | + hosts['alice'].send_classical(hosts['bob'].host_id,str(3),await_ack=True) |
| 52 | + |
| 53 | + t1 = hosts['bob'].run_protocol(listen_with_wait_time) |
| 54 | + t2 = hosts['alice'].run_protocol(send_some_with_delay) |
| 55 | + |
| 56 | + t1.join() |
| 57 | + t2.join() |
| 58 | + |
| 59 | + def test_get_seq_with_wait_time(self): |
| 60 | + def listen_with_wait_time(s): |
| 61 | + time.sleep(2) |
| 62 | + msg = hosts['bob'].get_classical_any_host(seq_num=2, wait=self.MAX_WAIT_TIME) |
| 63 | + self.assertEqual(msg.content,'3') |
| 64 | + |
| 65 | + def send_some_with_delay(s): |
| 66 | + hosts['alice'].send_classical(hosts['bob'].host_id,str(1),await_ack=True) |
| 67 | + hosts['alice'].send_classical(hosts['bob'].host_id,str(2),await_ack=True) |
| 68 | + time.sleep(5) |
| 69 | + hosts['alice'].send_classical(hosts['bob'].host_id,str(3),await_ack=True) |
| 70 | + |
| 71 | + t1 = hosts['bob'].run_protocol(listen_with_wait_time) |
| 72 | + t2 = hosts['alice'].run_protocol(send_some_with_delay) |
| 73 | + |
| 74 | + t1.join() |
| 75 | + t2.join() |
| 76 | + |
| 77 | + def test_get_seq_with_wait_time_none_value(self): |
| 78 | + def listen_with_wait_time(s): |
| 79 | + time.sleep(2) |
| 80 | + msg = hosts['bob'].get_classical_any_host(seq_num=3, wait=self.MAX_WAIT_TIME) |
| 81 | + self.assertEqual(msg,None)#seq_num not present |
| 82 | + |
| 83 | + def send_some_with_delay(s): |
| 84 | + hosts['alice'].send_classical(hosts['bob'].host_id,str(1),await_ack=True) |
| 85 | + hosts['alice'].send_classical(hosts['bob'].host_id,str(2),await_ack=True) |
| 86 | + time.sleep(5) |
| 87 | + hosts['alice'].send_classical(hosts['bob'].host_id,str(3),await_ack=True) |
| 88 | + |
| 89 | + t1 = hosts['bob'].run_protocol(listen_with_wait_time) |
| 90 | + t2 = hosts['alice'].run_protocol(send_some_with_delay) |
| 91 | + |
| 92 | + t1.join() |
| 93 | + t2.join() |
| 94 | + |
| 95 | + def test_get_all_with_wait_time_empty_arr(self): |
| 96 | + def listen_with_wait_time(s): |
| 97 | + msgs = hosts['bob'].get_classical_any_host(None, wait=self.MAX_WAIT_TIME) |
| 98 | + self.assertEqual(msgs,[]) |
| 99 | + |
| 100 | + def send_after_max_wait(s): |
| 101 | + time.sleep(self.MAX_WAIT_TIME+2) |
| 102 | + hosts['alice'].send_classical(hosts['bob'].host_id,str(1),await_ack=True) |
| 103 | + |
| 104 | + t1 = hosts['bob'].run_protocol(listen_with_wait_time) |
| 105 | + t2 = hosts['alice'].run_protocol(send_after_max_wait) |
| 106 | + |
| 107 | + t1.join() |
| 108 | + t2.join() |
| 109 | + |
| 110 | + def test_get_all_no_wait_time(self): |
| 111 | + # no msgs yet |
| 112 | + self.assertEqual(hosts['bob']._classical_messages.last_msg_added_to_host, None) |
| 113 | + rec_msgs = hosts['bob'].get_classical_any_host(None, wait=0) |
| 114 | + self.assertEqual(hosts['bob']._classical_messages.last_msg_added_to_host, None) |
| 115 | + self.assertEqual(len(rec_msgs), 0) |
| 116 | + |
| 117 | + # with some msgs |
| 118 | + for c in range(5): |
| 119 | + hosts['alice'].send_classical(hosts['bob'].host_id, str(c), await_ack=True) |
| 120 | + rec_msgs = hosts['bob'].get_classical_any_host(None, wait=0) |
| 121 | + self.assertEqual(hosts['bob']._classical_messages.last_msg_added_to_host, 'Alice') |
| 122 | + self.assertEqual(len(rec_msgs), 5) |
| 123 | + |
| 124 | + def test_get_seq_no_wait_time(self): |
| 125 | + # no msgs yet |
| 126 | + self.assertEqual(hosts['bob']._classical_messages.last_msg_added_to_host, None) |
| 127 | + rec_msg = hosts['bob'].get_classical_any_host(0, wait=0) |
| 128 | + self.assertEqual(hosts['bob']._classical_messages.last_msg_added_to_host, None) |
| 129 | + self.assertEqual(rec_msg, None) |
| 130 | + |
| 131 | + # with some msgs |
| 132 | + for c in range(5): |
| 133 | + hosts['alice'].send_classical(hosts['bob'].host_id, str(c), await_ack=True) |
| 134 | + rec_msg = hosts['bob'].get_classical_any_host(4, wait=0) |
| 135 | + self.assertEqual(hosts['bob']._classical_messages.last_msg_added_to_host, 'Alice') |
| 136 | + self.assertEqual(rec_msg.content, '4') |
| 137 | + |
| 138 | + def test_wait_data_type(self): |
| 139 | + self.assertRaises(Exception, hosts['bob'].get_classical_any_host, None, "1") |
| 140 | + |
| 141 | + |
0 commit comments