def test_receive(self): c0 = ContextAggregator(0) c0.process_to_set_output(neighbors=[1], timestamp = 0) c1 = ContextAggregator(1) c1.process_to_set_output(neighbors=[0,2], timestamp = 0) c2 = ContextAggregator(2) c2.process_to_set_output(neighbors=[1], timestamp = 0) # First round ## c0 sends contexts to neighbors, s0 contains the contexts in standard form r0_1 = c0.send(neighbor=1, timestamp=0) r1_0 = c1.send(neighbor=0, timestamp=0) r1_2 = c1.send(neighbor=2, timestamp=0) r2_1 = c2.send(neighbor=1, timestamp=0) c1.receive(from_node=0, contexts=r0_1[1], timestamp=0) c0.receive(from_node=1, contexts=r1_0[0], timestamp=0) c2.receive(from_node=1, contexts=r1_2[2], timestamp=0) c1.receive(from_node=2, contexts=r2_1[1], timestamp=0) r1 = c1.get_received_data(from_node=0) self.assertEqual(contexts_to_standard(r1), [[0],[]]) r0 = c0.get_received_data(from_node=1) self.assertEqual(contexts_to_standard(r0), [[1],[]]) r1 = c1.get_received_data(from_node=2) self.assertEqual(contexts_to_standard(r1), [[2],[]]) r2 = c2.get_received_data(from_node=1) self.assertEqual(contexts_to_standard(r2), [[1],[]])
def test_send(self): """ Three nodes are connected in serial 0 <-> 1 <-> 2 The test_for_real_world is to check if each one samples data and send correctly. The sent data is stored in r0,r1,r2 """ c0 = ContextAggregator(0) r = c0.process_to_set_output(neighbors=[1], timestamp = 0) self.assertTrue(same(r, {1: [[0], []]})) c1 = ContextAggregator(1) r = c1.process_to_set_output(neighbors=[0,2], timestamp = 0) self.assertTrue(same(r, {0: [[1], []], 2:[[1],[]]})) c2 = ContextAggregator(2) r = c2.process_to_set_output(neighbors=[1], timestamp = 0) self.assertTrue(same(r, {1: [[2], []]})) # First round ## c0 sends contexts to neighbors, s0 contains the contexts in standard form r0 = c0.send(timestamp=0) s0 = contexts_to_standard(r0[1]) self.assertTrue(same(s0, [[0], []])) r1 = c1.send(neighbor=0, timestamp=0) s1 = contexts_to_standard(r1[0]) self.assertTrue(same(s1, [[1], []])) r1 = c1.send(neighbor=2, timestamp=0) s1 = contexts_to_standard(r1[2]) self.assertTrue(same(s1, [[1], []])) r2 = c2.send(timestamp=0) s2 = contexts_to_standard(r2[1]) self.assertTrue(same(s2, [[2], []]))
def test_run_dataflow_single_only(self): # at timestamp = 0 config = {ContextAggregator.PM:ContextAggregator.SINGLE_ONLY_MODE} c0 = ContextAggregator(id=0, config=config) c1 = ContextAggregator(id=1, config=config) c2 = ContextAggregator(id=2, config=config) # First round ## compute c0.process_to_set_output(neighbors=[1], timestamp = 0) c1.process_to_set_output(neighbors=[0,2], timestamp = 0) c2.process_to_set_output(neighbors=[1], timestamp = 0) ## communication r0_1 = c0.send(neighbor=1, timestamp=0) r1_0 = c1.send(neighbor=0, timestamp=0) r1_2 = c1.send(neighbor=2, timestamp=0) r2_1 = c2.send(neighbor=1, timestamp=0) c1.receive(from_node=0, contexts=r0_1[1], timestamp=0) c0.receive(from_node=1, contexts=r1_0[0], timestamp=0) c2.receive(from_node=1, contexts=r1_2[2], timestamp=0) c1.receive(from_node=2, contexts=r2_1[1], timestamp=0) r1_0 = c1.get_received_data(from_node=0) r0_1 = c0.get_received_data(from_node=1) r1_2 = c1.get_received_data(from_node=2) r2_1 = c2.get_received_data(from_node=1) self.assertTrue(contexts_to_standard(r1_0), [[0],[]]) self.assertTrue(contexts_to_standard(r0_1), [[1],[]]) self.assertTrue(contexts_to_standard(r1_2), [[2],[]]) self.assertTrue(contexts_to_standard(r2_1), [[1],[]]) # Second round ## compute o0 = c0.process_to_set_output(neighbors=[1], timestamp = 0) o1 = c1.process_to_set_output(neighbors=[0,2], timestamp = 0) o2 = c2.process_to_set_output(neighbors=[1], timestamp = 0) self.assertTrue(contexts_to_standard(c0.get_singles(timestamp=0)) == [[0,1],[]]) self.assertTrue(contexts_to_standard(c1.get_singles(timestamp=0)) == [[0,1,2],[]]) self.assertTrue(contexts_to_standard(c2.get_singles(timestamp=0)) == [[1,2],[]]) self.assertTrue(o0 == {1: [[], []]}) self.assertTrue(o1 == {0: [[2], []], 2:[[0], []]}) self.assertTrue(o2 == {1: [[], []]}) ## communication self.assertTrue(c0.is_nothing_to_send()) self.assertTrue(c2.is_nothing_to_send()) r1_0 = c1.send(neighbor=0, timestamp=0) r1_2 = c1.send(neighbor=2, timestamp=0) self.assertTrue(contexts_to_standard(r1_0[0]) == [[2],[]]) self.assertTrue(contexts_to_standard(r1_2[2]) == [[0],[]]) c0.receive(from_node=1, contexts=r1_0[0], timestamp=0) c2.receive(from_node=1, contexts=r1_2[2], timestamp=0) r0_1 = c0.get_received_data(from_node=1) r2_1 = c2.get_received_data(from_node=1) self.assertTrue(contexts_to_standard(r0_1) == [[2],[]]) self.assertTrue(contexts_to_standard(r2_1) == [[0],[]]) # Third iteration o0 = c0.process_to_set_output(neighbors=[1], timestamp = 0) o1 = c1.process_to_set_output(neighbors=[0,2], timestamp = 0) o2 = c2.process_to_set_output(neighbors=[1], timestamp = 0) self.assertTrue(contexts_to_standard(c0.get_singles(timestamp=0)) == [[0,1,2],[]]) self.assertTrue(contexts_to_standard(c1.get_singles(timestamp=0)) == [[0,1,2],[]]) self.assertTrue(contexts_to_standard(c2.get_singles(timestamp=0)) == [[0,1,2],[]]) self.assertTrue(o0 == {1: [[], []]}) #self.assertTrue(o1 == {0: [[], []], 2:[[], []]}) self.assertTrue(o1 == {}) # {0: [[], []], 2:[[], []]}) self.assertTrue(o2 == {1: [[], []]}) self.assertTrue(c0.is_nothing_to_send()) self.assertTrue(c1.is_nothing_to_send()) self.assertTrue(c2.is_nothing_to_send())