示例#1
0
    def test_several_messages_between_two(self, http_comms):
        comm1, comm2 = http_comms

        comm1.discovery.register_computation('c1', 'a2', ('127.0.0.1', 10002))
        comm2.discovery.register_computation('c2', 'a1', ('127.0.0.1', 10001))

        comm1.send_msg(
            'a1', 'a2',
            ComputationMessage('c1', 'c2', Message('test', 'test1'), MSG_ALGO))
        comm1.send_msg\
            ('a1', 'a2',
             ComputationMessage('c1', 'c2', Message('test', 'test2'), MSG_ALGO))
        comm1.send_msg(
            'a1', 'a2',
            ComputationMessage('c1', 'c2', Message('test', 'test3'), MSG_MGT))
        comm1.send_msg(
            'a1', 'a2',
            ComputationMessage('c1', 'c2', Message('test', 'test4'), MSG_ALGO))

        comm2.messaging.post_msg.assert_has_calls([
            call('c1', 'c2', Message('test', 'test1'), MSG_ALGO),
            call('c1', 'c2', Message('test', 'test2'), MSG_ALGO),
            call('c1', 'c2', Message('test', 'test3'), MSG_MGT),
            call('c1', 'c2', Message('test', 'test4'), MSG_ALGO),
        ])
示例#2
0
    def test_msg_to_unknown_agent_ignore_mode(self, http_comms):
        comm1, comm2 = http_comms

        # on a1, do NOT register a2, and still try to send a message to it
        # Default mode is ignore : always returns True
        assert comm1.send_msg(
            'a1', 'a2',
            ComputationMessage('c1', 'c2', Message('a1', 't1'), MSG_ALGO))
示例#3
0
 def test_msg_to_unknown_agent_fail_mode(self, http_comms):
     comm1, comm2 = http_comms
     # on a1, do NOT register a2, and still try to send a message to it
     with pytest.raises(UnknownAgent):
         comm1.send_msg('a1',
                        'a2',
                        ComputationMessage('c1', 'c2', Message('a1', 't1'),
                                           MSG_ALGO),
                        on_error='fail')
示例#4
0
    def test_msg_to_unreachable_agent_ignore_mode(self, http_comms):
        comm1, comm2 = http_comms

        # on a1, register a2 with the wrong port number
        comm1.discovery.register_computation('c2', 'a2', ('127.0.0.1', 10006))
        comm2.discovery.register_computation('c1', 'a1', ('127.0.0.1', 10001))

        assert comm1.send_msg(
            'a1', 'a2',
            ComputationMessage('c1', 'c2', Message('a1', 't'), MSG_ALGO))
示例#5
0
    def test_http_handler_several_messages(self, httpd):

        t = Thread(name='http_thread', target=httpd.serve_forever)
        t.start()
        requests.post('http://127.0.0.1:8001/test',
                      json={'key': 'value'},
                      timeout=0.5)
        requests.post('http://127.0.0.1:8001/test2',
                      headers={'sender-agent': 'zero'},
                      json={'key': 'value2'},
                      timeout=0.5)
        requests.post('http://127.0.0.1:8001/test3',
                      headers={
                          'sender-agent': 'sender',
                          'dest-agent': 'dest',
                          'type': '15'
                      },
                      json={'key': 'value3'},
                      timeout=0.5)

        sleep(0.5)

        httpd.comm.on_post_message.assert_has_calls([
            call(
                '/test', None, None,
                ComputationMessage(src_comp=None,
                                   dest_comp=None,
                                   msg={'key': 'value'},
                                   msg_type=MSG_ALGO)),
            call(
                '/test2', 'zero', None,
                ComputationMessage(src_comp=None,
                                   dest_comp=None,
                                   msg={'key': 'value2'},
                                   msg_type=MSG_ALGO)),
            call(
                '/test3', 'sender', 'dest',
                ComputationMessage(src_comp=None,
                                   dest_comp=None,
                                   msg={'key': 'value3'},
                                   msg_type=15)),
        ])
示例#6
0
    def test_msg_to_unreachable_agent_fail_mode(self, http_comms):
        comm1, comm2 = http_comms
        # on a1, register a2 with the wrong port number
        comm1.discovery.register_computation('c2', 'a2', ('127.0.0.1', 10006))

        comm2.discovery.register_computation('c1', 'a1', ('127.0.0.1', 10001))

        with pytest.raises(UnreachableAgent):
            comm1.send_msg('a1',
                           'a2',
                           ComputationMessage('c1', 'c2', Message('a1', '1'),
                                              MSG_ALGO),
                           on_error='fail')
示例#7
0
    def test_one_message_between_two(self, http_comms):
        comm1, comm2 = http_comms

        comm1.discovery.register_computation('c2', 'a2', ('127.0.0.1', 10002))
        comm2.discovery.register_computation('c1', 'a1', ('127.0.0.1', 10001))

        comm1.send_msg(
            'a1', 'a2',
            ComputationMessage('c1', 'c2', Message('test', 'test'), MSG_ALGO))

        comm2.messaging.post_msg.assert_called_with('c1', 'c2',
                                                    Message('test', 'test'),
                                                    MSG_ALGO)
示例#8
0
    def test_msg_to_unknown_computation_ignore_mode(self, http_comms):
        comm1, comm2 = http_comms
        comm1.discovery.register_computation('c2', 'a2', ('127.0.0.1', 10002))

        comm2.discovery.register_computation('c1', 'a1', ('127.0.0.1', 10001))

        def raise_unknown(*args):
            raise UnknownComputation('test')

        comm2.messaging.post_msg = MagicMock(side_effect=raise_unknown)

        # Default mode is ignore : always returns True
        assert comm1.send_msg(
            'a1', 'a2',
            ComputationMessage('c1', 'c2', Message('a1', 'test1'), MSG_ALGO))
示例#9
0
    def test_http_handler_one_message(self, httpd):

        t = Thread(name='http_thread', target=httpd.serve_forever)
        t.start()
        requests.post('http://127.0.0.1:8001/test',
                      json={'key': 'value'},
                      timeout=0.5)

        sleep(0.5)

        httpd.comm.on_post_message.assert_called_once_with(
            '/test', None, None,
            ComputationMessage(src_comp=None,
                               dest_comp=None,
                               msg={'key': 'value'},
                               msg_type=MSG_ALGO))
示例#10
0
    def test_msg_to_unknown_computation_fail_mode(self, http_comms):
        comm1, comm2 = http_comms
        comm1.discovery.register_computation('c2', 'a2', ('127.0.0.1', 10002))

        comm2.discovery.register_computation('c1', 'a1', ('127.0.0.1', 10001))

        def raise_unknown(*args):
            raise UnknownComputation('test')

        comm2.messaging.post_msg = MagicMock(side_effect=raise_unknown)

        with pytest.raises(UnknownComputation):
            comm1.send_msg('a1',
                           'a2',
                           ComputationMessage('c1', 'c2', Message('a1', 't1'),
                                              MSG_ALGO),
                           on_error='fail')
示例#11
0
    def test_msg_to_computation_hosted_on_another_agent(self, local_messaging):

        local_messaging.discovery.register_computation('c1', 'a1')
        local_messaging.discovery.register_computation('c2', 'a2', 'addr2')
        local_messaging._comm.send_msg = MagicMock()

        msg = MagicMock()
        local_messaging.post_msg('c1', 'c2', msg)

        # Check that the msg was passed to the communication layer
        local_messaging._comm.send_msg.assert_called_with('a1',
                                                          'a2',
                                                          ComputationMessage(
                                                              'c1', 'c2', msg,
                                                              ANY),
                                                          on_error=ANY)

        # Check it's not in the local queue
        full_msg, _ = local_messaging.next_msg()
        assert full_msg is None