def setUp(self): # obligatory and optional ports g = DeltaGraph() out_port_obl = OutPort( 'out', Int(), InPort(None, Int(), None, 0), RealNode(g, [], name='node_name'), ) out_port_opt = OutPort( 'out', Int(), InPort(None, Optional(Int()), None, 0), RealNode(g, [], name='node_name'), ) # 4 types of queues self.delta_queue_obl = DeltaQueue(out_port_obl) self.delta_queue_opt = DeltaQueue(out_port_opt) self.const_queue_obl = ConstQueue(out_port_obl) self.const_queue_opt = ConstQueue(out_port_opt) # test messages self.msg1 = QueueMessage(1) self.msg2 = QueueMessage(2) self.msg_with_none = QueueMessage(None) self.msg_unpackable = QueueMessage("abcde") # these messages should be received self.msg1_answer = QueueMessage(1) self.msg2_answer = QueueMessage(2) self.msg_with_none_answer = QueueMessage(None)
def test_neq_type_diff(self): g1 = DeltaGraph() n1 = RealNode(g1, [], OrderedDict([('a', int)]), OrderedDict()) in_p_1 = InPort('a', int, n1, 0) g2 = DeltaGraph() n2 = RealNode(g2, [], OrderedDict([('a', float)]), OrderedDict()) in_p_2 = InPort('a', float, n2, 0) self.assertNotEqual(in_p_1, in_p_2)
def test_eq(self): g1 = DeltaGraph() n1 = RealNode(g1, [], OrderedDict([('a', int)]), OrderedDict()) in_p_1 = InPort('a', int, n1, 0) self.assertEqual(in_p_1, in_p_1) g2 = DeltaGraph() n2 = RealNode(g2, [], OrderedDict([('a', int)]), OrderedDict()) in_p_2 = InPort('a', int, n2, 0) self.assertEqual(in_p_1, in_p_2)
def test_neq_dest_diff(self): g1 = DeltaGraph() n1 = RealNode(g1, [], OrderedDict(), OrderedDict([('out_a', int)])) dest_n_1 = RealNode(g1, [], OrderedDict([('a', int)]), OrderedDict()) dest_1 = InPort('a', int, dest_n_1, 0) out_p_1 = OutPort('out_a', int, dest_1, n1) g2 = DeltaGraph() n2 = RealNode(g2, [], OrderedDict(), OrderedDict([('out_a', int)])) dest_n_2 = RealNode(g2, [], OrderedDict([('a', int)]), OrderedDict()) dest_2 = InPort('b', int, dest_n_2, 0) out_p_2 = OutPort('out_a', int, dest_2, n2) self.assertNotEqual(out_p_1, out_p_2)
def test_select_body_on_construction(self): """Test if single node bodies get their body auto-selected. Multi-body nodes do not undergo auto-selection. """ graph = DeltaGraph() body_1 = PythonBody() test_node = RealNode(graph, [body_1]) self.assertEqual(test_node.body, body_1) body_2 = PythonBody() test_node_2 = RealNode(graph, [body_1, body_2]) with self.assertRaises(ValueError): test_node_2.body
def test_in_port_capnp_wiring_direct(self): """In port has limit on port size.""" n = RealNode(DeltaGraph(), [], name='node_name') in_port = InPort("index", as_delta_type(int), n, 1) wire = dotdf_capnp.Wire.new_message() nodes = [dotdf_capnp.Node.new_message()] nodes[0].name = n.full_name nodes[0].init("inPorts", 1) nodes[0].inPorts[0].name = "index" in_port.capnp_wiring(nodes, wire) self.assertEqual(wire.destNode, 0) self.assertEqual(wire.destInPort, 0) self.assertEqual(wire.direct, True)
def test_selection_overide(self): """Test no override occurs without override == True """ graph = DeltaGraph() body_1 = PythonBody(tags=["preferred_1"]) body_2 = PythonBody(tags=["preferred_2"]) test_node = RealNode(graph, [body_1, body_2]) test_node.select_body(preferred=["preferred_1"]) self.assertEqual(test_node.body, body_1) test_node.select_body(preferred=["preferred_2"], override=False) self.assertEqual(test_node.body, body_1) test_node.select_body(preferred=["preferred_2"]) self.assertEqual(test_node.body, body_2)
def test_no_bodies_node(self): """Test if no-body nodes throw errors on body access """ graph = DeltaGraph() test_node = RealNode(graph, []) with self.assertRaises(ValueError): test_node.select_body() test_node.body body_1 = PythonBody(tags=["exclude_me"]) body_2 = PythonBody(tags=["exclude_me"]) test_node = RealNode(graph, [body_1, body_2]) with self.assertRaises(ValueError): test_node.select_body(exclusions=["exclude_me"]) test_node.body
def test_in_port_capnp_wiring(self): """Generate wiring.""" n = RealNode(DeltaGraph(), [], name='node_name') in_port = InPort("index", as_delta_type(int), n, 0) wire = dotdf_capnp.Wire.new_message() nodes = [dotdf_capnp.Node.new_message() for _ in range(3)] nodes[0].name = "fake_name" nodes[1].name = "fake_name" nodes[2].name = n.full_name nodes[2].init("inPorts", 3) nodes[2].inPorts[0].name = "fake_name" nodes[2].inPorts[1].name = "index" nodes[2].inPorts[2].name = "fake_name" in_port.capnp_wiring(nodes, wire) self.assertEqual(wire.destNode, 2) self.assertEqual(wire.destInPort, 1) self.assertEqual(wire.direct, False)
def test_class_as_tag(self): """Test classes can be used as tags """ graph = DeltaGraph() body_1 = PythonBody() body_2 = PyFuncBody(lambda: False) test_node = RealNode(graph, [body_1, body_2]) test_node.select_body(preferred=[PyFuncBody]) self.assertEqual(test_node.body, body_2) with self.assertRaises(ValueError): test_node.select_body(exclusions=[PythonBody]) test_node.body
def test_selection_exclusion_preference(self): """Test that selection works correctly with respect to body order, exclusions and preferences """ graph = DeltaGraph() body_1 = PythonBody(tags=["exclude_me", "prefer_me"]) body_2 = PythonBody() body_3 = PythonBody(tags=["prefer_me"]) test_node = RealNode(graph, [body_1, body_2, body_3]) test_node.select_body(exclusions=["exclude_me"]) self.assertEqual(test_node.body, body_2) test_node.select_body(exclusions=["exclude_me"], preferred=["prefer_me"]) self.assertEqual(test_node.body, body_3)
def broken_adder(node: RealNode): a = node.receive('a') b = node.receive('b') node.send(a+b+1)
def broken_adder(node: RealNode): node.receive('a') node.receive('b') raise DeltaRuntimeExit