Example #1
0
def _check_connection(pid, process_list, timeout):
    LOG.debug("start check_connection")
    msg = messaging.Messaging()
    msg_list = msg.receive_all_msg(timeout_limit=timeout,
                                   msg_limit_count=len(process_list))

    pid_list = []
    for message in msg_list:
        if message.get('pid'):
            pid_list.append(message.get('pid'))

    actives = []
    inactives = []
    for process in process_list:
        if pid_list and process.pid in pid_list:
            rackpipe.Pipe.share(pid, process.pid)
            msg.send_msg(target=process.pid, message="start")
            actives.append(process)
            pid_list.remove(process.pid)
        else:
            RACK_CTX.client.processes.delete(RACK_CTX.gid, process.pid)
            inactives.append(process)

    LOG.debug("_check_connection active processes count: %s", len(actives))
    LOG.debug("_check_connection inactive processes count: %s", len(inactives))

    if not actives:
        msg = "No child process is active."
        raise Exception(msg)

    return actives, inactives
    def test_send_msg_no_message(self):
        msg = rack_ipc.Messaging()
        target = 'test_pid'
        msg.send_msg(target)
        routing_key = self.mock_RACK_CTX.gid + '.' + target
        send_dict = {'pid': self.mock_RACK_CTX.pid}
        send_msg = cPickle.dumps(send_dict)

        self.mock_channel.\
            basic_publish.assert_called_with(exchange=self.mock_RACK_CTX.gid,
                                             routing_key=routing_key,
                                             body=send_msg)
    def test_declare_queue(self):
        queue_name = 'test_queue_name'
        msg = rack_ipc.Messaging()
        msg.declare_queue(queue_name)

        self.mock_channel.\
            exchange_declare.assert_called_with(exchange=self.mock_RACK_CTX.gid,
                                                type='topic')
        self.mock_channel.queue_declare.assert_called_with(queue=queue_name)
        r_key = self.mock_RACK_CTX.gid + '.' + queue_name
        self.mock_channel.queue_bind.assert_called_with(exchange=self.mock_RACK_CTX.gid,
                                                        queue=queue_name,
                                                        routing_key=r_key)
    def test_receive_msg(self, mock_receive):
        timeout_limit = 123
        msg = rack_ipc.Messaging()
        message = msg.receive_msg(timeout_limit=timeout_limit)

        self.mock_connection.add_timeout.\
            assert_called_with(deadline=timeout_limit,
                               callback_method=mock_receive().time_out)
        self.mock_channel.\
            basic_consume.assert_called_with(mock_receive().get_msg,
                                             queue=self.mock_RACK_CTX.pid,
                                             no_ack=False)
        self.mock_channel.start_consuming.assert_called_with()
        self.assertEqual(message, mock_receive().message)
    def test_receive_get_all_msg(self):
        ch = Mock()
        method = Mock()
        properties = Mock()
        receive_msg = 'receive_msg'
        body = cPickle.dumps(receive_msg)
        ch_object = {'delivery_tag': 'delivery_tag'}
        method.configure_mock(**ch_object)

        msg = rack_ipc.Messaging()
        receive = msg.Receive()
        receive.get_all_msg(ch, method, properties, body)

        ch.basic_ack.assert_called_with(delivery_tag=ch_object['delivery_tag'])
        self.assertEqual(receive.message_list[0], receive_msg)
    def test_receive_get_all_msg_count_limit(self):
        ch = Mock()
        method = Mock()
        properties = Mock()
        message_list = [{'pid': 'child_pid1'},
                        {'pid': 'child_pid2'}]
        expected_message_list = copy.deepcopy(message_list)
        receive_msg = {'pid': 'child_pid3'}
        expected_message_list.append(receive_msg)
        body = cPickle.dumps(receive_msg)
        ch_object = {'delivery_tag': 'delivery_tag'}
        method.configure_mock(**ch_object)
        msg = rack_ipc.Messaging()
        receive = msg.Receive()
        receive.message_list = message_list
        receive.msg_count_limit = 3

        receive.get_all_msg(ch, method, properties, body)

        ch.basic_ack.assert_called_with(delivery_tag=ch_object['delivery_tag'])
        ch.stop_consuming.assert_called_with()
        self.assertEqual(receive.message_list, expected_message_list)
 def test_create_connection_ipc_endpoint(self, mock_pika_connection_param):
     ipc_ip = 'ipc_ip'
     self.mock_RACK_CTX.ipc_endpoint = ipc_ip
     rack_ipc.Messaging()
     mock_pika_connection_param.assert_called_with(ipc_ip)
 def test_create_connection(self):
     p = patch('pika.ConnectionParameters', autospec=True)
     self.addCleanup(p.stop)
     mock_pika_connection_param = p.start()
     rack_ipc.Messaging()
     mock_pika_connection_param.assert_called_with(self.mock_RACK_CTX.proxy_ip)
 def test_receive_timeout(self):
     msg = rack_ipc.Messaging()
     receive = msg.Receive()
     receive.channel = self.mock_channel
     receive.time_out()
     self.mock_channel.stop_consuming.assert_called_with()