def setUp(self):

        c1 = ComputationNode('c1', 'dummy_type', neighbors=['c2'])
        c2 = ComputationNode('c2', 'dummy_type', neighbors=['c1'])

        self.cg = ComputationGraph(graph_type='test', nodes=[c1, c2])
        self.agents = [AgentDef('a1'), AgentDef('a2')]
def test_receive_one_neighbor_dcop_computation():

    comp_def = ComputationDef(ComputationNode("test", neighbors=["bar"]),
                              AlgorithmDef("fake", {}))
    c = SynchDcopC("test", comp_def)
    c.on_new_cycle = MagicMock()
    c.start()

    # First cycle, c receives a message from its single neighbor,
    # on_cycle_message must be called.
    msg1 = FooMsg(1)
    # This is automatically done when sending from a Synchronous Computation:
    msg1.cycle_id = 0
    c.on_message("bar", msg1, 42)
    assert c.cycle_count == 1
    c.on_new_cycle.assert_any_call({"bar": (msg1, 42)}, 0)

    # Second cycle, c receives a message from its single neighbor,
    # on_cycle_message must be called again.
    c.on_new_cycle.reset_mock()
    msg2 = FooMsg(2)
    msg2.cycle_id = 1
    c.on_message("bar", msg2, 43)
    c.on_new_cycle.assert_any_call({"bar": (msg2, 43)}, 1)
    assert c.cycle_count == 2
def test_node_creation_with_one_neighbor():

    n1 = ComputationNode('n1', neighbors=['n2'])

    assert 'n2' in n1.neighbors
    assert len(n1.links) == 1
    assert list(n1.links)[0].has_node('n2')
def test_node_creation_minimal():
    # name is the only mandatory param:
    n = ComputationNode('n1')
    assert n.name == 'n1'
    assert not n.type
    assert not n.links
    assert not n.neighbors
def test_node_creation_with_several_neighbors():

    n1 = ComputationNode('n1', neighbors=['n2', 'n3', 'n4'])

    assert 'n2' in n1.neighbors
    assert 'n3' in n1.neighbors
    assert 'n4' in n1.neighbors
    assert len(n1.links) == 3
def test_node_creation_with_hyperlinks():

    n1 = ComputationNode('n1', links=[Link(['n2', 'n3']),
                                      Link(['n4'])])

    assert 'n2' in n1.neighbors
    assert 'n3' in n1.neighbors
    assert 'n4' in n1.neighbors
def test_node_simplerepr():
    n1 = ComputationNode('n1', neighbors=['n2', 'n3', 'n4'])

    r1 = simple_repr(n1)

    obtained = from_repr(r1)

    assert n1 == obtained
    assert 'n2' in n1.neighbors
    assert 'n3' in n1.neighbors
    assert 'n4' in n1.neighbors
    assert len(n1.links) == 3
def test_cycle_id_is_added_when_using_post_to_all_neighbors_dcop_computation():

    comp_def = ComputationDef(
        ComputationNode("test", neighbors=["bar", "yup"]),
        AlgorithmDef("fake", {}))
    c = SynchDcopC("test", comp_def)
    c.start()

    c.post_to_all_neighbors(FooMsg(1))

    expected = FooMsg(1)
    expected.cycle_id = 0

    c.message_sender.assert_any_call("test", "yup", expected, None, None)
    c.message_sender.assert_any_call("test", "bar", expected, None, None)
    def setUp(self):

        # A grid-shaped  (3x2) computation graph with 6 computations
        self.l1 = Link(['c1', 'c2'])
        self.l2 = Link(['c2', 'c3'])
        self.l3 = Link(['c1', 'c4'])
        self.l4 = Link(['c2', 'c5'])
        self.l5 = Link(['c3', 'c6'])
        self.l6 = Link(['c4', 'c5'])
        self.l7 = Link(['c5', 'c6'])
        self.links = [
            self.l1, self.l2, self.l3, self.l4, self.l5, self.l6, self.l7
        ]

        nodes = {}
        for i in range(1, 7):
            name = 'c' + str(i)
            nodes[name] = ComputationNode(
                name,
                'test',
                links=[l for l in self.links if l.has_node(name)])

        self.cg = ComputationGraph('test', nodes=nodes.values())
        # setattr(self.cg, 'links', [self.l1, self.l2, self.l3, self.l4,
        #                            self.l5, self.l6, self.l7])
        #
        # 6 agents hosting these computations
        d = Discovery('a1', 'addr1')
        d.register_computation('c1', 'a1', 'addr1', publish=False)
        d.register_computation('c2', 'a2', 'addr2', publish=False)
        d.register_computation('c3', 'a3', 'addr3', publish=False)
        d.register_computation('c4', 'a4', 'addr4', publish=False)
        d.register_computation('c5', 'a5', 'addr5', publish=False)
        d.register_computation('c6', 'a8', 'addr8', publish=False)
        # and the corresponding replica, 2 for each computation
        d.register_replica('c1', 'a2')
        d.register_replica('c1', 'a5')
        d.register_replica('c2', 'a3')
        d.register_replica('c2', 'a6')
        d.register_replica('c3', 'a1')
        d.register_replica('c3', 'a4')
        d.register_replica('c4', 'a2')
        d.register_replica('c4', 'a5')
        d.register_replica('c5', 'a3')
        d.register_replica('c5', 'a6')
        d.register_replica('c6', 'a1')
        d.register_replica('c6', 'a4')
        self.discovery = d
def test_receive_two_neighbors_dcop_computation():

    comp_def = ComputationDef(
        ComputationNode("test", neighbors=["bar", "yup"]),
        AlgorithmDef("fake", {}))
    c = SynchDcopC("test", comp_def)
    c.on_new_cycle = MagicMock()
    c.start()

    # First cycle, c receives a message from both neighbors,
    # on_cycle_message must be called.
    msgbar1 = FooMsg(1)
    msgbar1.cycle_id = 0
    c.on_message("bar", msgbar1, 42)
    msgyup1 = FooMsg(1)
    msgyup1.cycle_id = 0
    c.on_message("yup", msgyup1, 43)

    assert c.cycle_count == 1
    c.on_new_cycle.assert_called_once_with(
        {
            "bar": (msgbar1, 42),
            "yup": (msgyup1, 43)
        }, 0)
def test_node_creation_raises_when_giving_links_neighbors():

    with pytest.raises(ValueError):
        n1 = ComputationNode('n1', links=[Link(['n2'])], neighbors=['n2'])
def test_node_creation_with_links():

    n1 = ComputationNode('n1', links=[Link(['n2'])])

    assert 'n2' in n1.neighbors
    assert list(n1.links)[0].has_node('n2')