Search in sources :

Example 1 with SendMessageContext

use of org.apache.rocketmq.client.hook.SendMessageContext in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.

the class MQClientAPIImplTest method testSendMessageSync_Success.

@Test
public void testSendMessageSync_Success() throws InterruptedException, RemotingException, MQBrokerException {
    doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock mock) throws Throwable {
            RemotingCommand request = mock.getArgument(1);
            return createSuccessResponse(request);
        }
    }).when(remotingClient).invokeSync(anyString(), any(RemotingCommand.class), anyLong());
    SendMessageRequestHeader requestHeader = createSendMessageRequestHeader();
    SendResult sendResult = mqClientAPI.sendMessage(brokerAddr, brokerName, msg, requestHeader, 3 * 1000, CommunicationMode.SYNC, new SendMessageContext(), defaultMQProducerImpl);
    assertThat(sendResult.getSendStatus()).isEqualTo(SendStatus.SEND_OK);
    assertThat(sendResult.getOffsetMsgId()).isEqualTo("123");
    assertThat(sendResult.getQueueOffset()).isEqualTo(123L);
    assertThat(sendResult.getMessageQueue().getQueueId()).isEqualTo(1);
}
Also used : RemotingCommand(org.apache.rocketmq.remoting.protocol.RemotingCommand) Answer(org.mockito.stubbing.Answer) Mockito.doAnswer(org.mockito.Mockito.doAnswer) SendMessageRequestHeader(org.apache.rocketmq.common.protocol.header.SendMessageRequestHeader) InvocationOnMock(org.mockito.invocation.InvocationOnMock) SendResult(org.apache.rocketmq.client.producer.SendResult) SendMessageContext(org.apache.rocketmq.client.hook.SendMessageContext) Test(org.junit.Test)

Example 2 with SendMessageContext

use of org.apache.rocketmq.client.hook.SendMessageContext in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.

the class MQClientAPIImplTest method testSendMessageOneWay_WithException.

@Test
public void testSendMessageOneWay_WithException() throws RemotingException, InterruptedException, MQBrokerException {
    doThrow(new RemotingTimeoutException("Remoting Exception in Test")).when(remotingClient).invokeOneway(anyString(), any(RemotingCommand.class), anyLong());
    try {
        mqClientAPI.sendMessage(brokerAddr, brokerName, msg, new SendMessageRequestHeader(), 3 * 1000, CommunicationMode.ONEWAY, new SendMessageContext(), defaultMQProducerImpl);
        failBecauseExceptionWasNotThrown(RemotingException.class);
    } catch (RemotingException e) {
        assertThat(e).hasMessage("Remoting Exception in Test");
    }
    doThrow(new InterruptedException("Interrupted Exception in Test")).when(remotingClient).invokeOneway(anyString(), any(RemotingCommand.class), anyLong());
    try {
        mqClientAPI.sendMessage(brokerAddr, brokerName, msg, new SendMessageRequestHeader(), 3 * 1000, CommunicationMode.ONEWAY, new SendMessageContext(), defaultMQProducerImpl);
        failBecauseExceptionWasNotThrown(InterruptedException.class);
    } catch (InterruptedException e) {
        assertThat(e).hasMessage("Interrupted Exception in Test");
    }
}
Also used : RemotingCommand(org.apache.rocketmq.remoting.protocol.RemotingCommand) SendMessageRequestHeader(org.apache.rocketmq.common.protocol.header.SendMessageRequestHeader) RemotingTimeoutException(org.apache.rocketmq.remoting.exception.RemotingTimeoutException) SendMessageContext(org.apache.rocketmq.client.hook.SendMessageContext) RemotingException(org.apache.rocketmq.remoting.exception.RemotingException) Test(org.junit.Test)

Example 3 with SendMessageContext

use of org.apache.rocketmq.client.hook.SendMessageContext in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.

the class MQClientAPIImplTest method testSendMessageAsync_Success.

@Test
public void testSendMessageAsync_Success() throws RemotingException, InterruptedException, MQBrokerException {
    doNothing().when(remotingClient).invokeAsync(anyString(), any(RemotingCommand.class), anyLong(), any(InvokeCallback.class));
    SendResult sendResult = mqClientAPI.sendMessage(brokerAddr, brokerName, msg, new SendMessageRequestHeader(), 3 * 1000, CommunicationMode.ASYNC, new SendMessageContext(), defaultMQProducerImpl);
    assertThat(sendResult).isNull();
    doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock mock) throws Throwable {
            InvokeCallback callback = mock.getArgument(3);
            RemotingCommand request = mock.getArgument(1);
            ResponseFuture responseFuture = new ResponseFuture(request.getOpaque(), 3 * 1000, null, null);
            responseFuture.setResponseCommand(createSuccessResponse(request));
            callback.operationComplete(responseFuture);
            return null;
        }
    }).when(remotingClient).invokeAsync(anyString(), any(RemotingCommand.class), anyLong(), any(InvokeCallback.class));
    SendMessageContext sendMessageContext = new SendMessageContext();
    sendMessageContext.setProducer(new DefaultMQProducerImpl(new DefaultMQProducer()));
    mqClientAPI.sendMessage(brokerAddr, brokerName, msg, new SendMessageRequestHeader(), 3 * 1000, CommunicationMode.ASYNC, new SendCallback() {

        @Override
        public void onSuccess(SendResult sendResult) {
            assertThat(sendResult.getSendStatus()).isEqualTo(SendStatus.SEND_OK);
            assertThat(sendResult.getOffsetMsgId()).isEqualTo("123");
            assertThat(sendResult.getQueueOffset()).isEqualTo(123L);
            assertThat(sendResult.getMessageQueue().getQueueId()).isEqualTo(1);
        }

        @Override
        public void onException(Throwable e) {
        }
    }, null, null, 0, sendMessageContext, defaultMQProducerImpl);
}
Also used : InvokeCallback(org.apache.rocketmq.remoting.InvokeCallback) SendMessageRequestHeader(org.apache.rocketmq.common.protocol.header.SendMessageRequestHeader) ResponseFuture(org.apache.rocketmq.remoting.netty.ResponseFuture) DefaultMQProducer(org.apache.rocketmq.client.producer.DefaultMQProducer) RemotingCommand(org.apache.rocketmq.remoting.protocol.RemotingCommand) Answer(org.mockito.stubbing.Answer) Mockito.doAnswer(org.mockito.Mockito.doAnswer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) SendResult(org.apache.rocketmq.client.producer.SendResult) SendMessageContext(org.apache.rocketmq.client.hook.SendMessageContext) DefaultMQProducerImpl(org.apache.rocketmq.client.impl.producer.DefaultMQProducerImpl) SendCallback(org.apache.rocketmq.client.producer.SendCallback) Test(org.junit.Test)

Example 4 with SendMessageContext

use of org.apache.rocketmq.client.hook.SendMessageContext in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.

the class MQClientAPIImplTest method testSendMessageSync_WithException.

@Test
public void testSendMessageSync_WithException() throws InterruptedException, RemotingException, MQBrokerException {
    doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock mock) throws Throwable {
            RemotingCommand request = mock.getArgument(1);
            RemotingCommand response = RemotingCommand.createResponseCommand(SendMessageResponseHeader.class);
            response.setCode(ResponseCode.SYSTEM_ERROR);
            response.setOpaque(request.getOpaque());
            response.setRemark("Broker is broken.");
            return response;
        }
    }).when(remotingClient).invokeSync(anyString(), any(RemotingCommand.class), anyLong());
    SendMessageRequestHeader requestHeader = createSendMessageRequestHeader();
    try {
        mqClientAPI.sendMessage(brokerAddr, brokerName, msg, requestHeader, 3 * 1000, CommunicationMode.SYNC, new SendMessageContext(), defaultMQProducerImpl);
        failBecauseExceptionWasNotThrown(MQBrokerException.class);
    } catch (MQBrokerException e) {
        assertThat(e).hasMessageContaining("Broker is broken.");
    }
}
Also used : RemotingCommand(org.apache.rocketmq.remoting.protocol.RemotingCommand) SendMessageResponseHeader(org.apache.rocketmq.common.protocol.header.SendMessageResponseHeader) Answer(org.mockito.stubbing.Answer) Mockito.doAnswer(org.mockito.Mockito.doAnswer) SendMessageRequestHeader(org.apache.rocketmq.common.protocol.header.SendMessageRequestHeader) InvocationOnMock(org.mockito.invocation.InvocationOnMock) MQBrokerException(org.apache.rocketmq.client.exception.MQBrokerException) SendMessageContext(org.apache.rocketmq.client.hook.SendMessageContext) Test(org.junit.Test)

Example 5 with SendMessageContext

use of org.apache.rocketmq.client.hook.SendMessageContext in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.

the class MQClientAPIImplTest method testSendMessageOneWay_Success.

@Test
public void testSendMessageOneWay_Success() throws RemotingException, InterruptedException, MQBrokerException {
    doNothing().when(remotingClient).invokeOneway(anyString(), any(RemotingCommand.class), anyLong());
    SendResult sendResult = mqClientAPI.sendMessage(brokerAddr, brokerName, msg, new SendMessageRequestHeader(), 3 * 1000, CommunicationMode.ONEWAY, new SendMessageContext(), defaultMQProducerImpl);
    assertThat(sendResult).isNull();
}
Also used : RemotingCommand(org.apache.rocketmq.remoting.protocol.RemotingCommand) SendMessageRequestHeader(org.apache.rocketmq.common.protocol.header.SendMessageRequestHeader) SendResult(org.apache.rocketmq.client.producer.SendResult) SendMessageContext(org.apache.rocketmq.client.hook.SendMessageContext) Test(org.junit.Test)

Aggregations

SendMessageContext (org.apache.rocketmq.client.hook.SendMessageContext)16 SendMessageRequestHeader (org.apache.rocketmq.common.protocol.header.SendMessageRequestHeader)14 Test (org.junit.Test)14 RemotingCommand (org.apache.rocketmq.remoting.protocol.RemotingCommand)12 SendResult (org.apache.rocketmq.client.producer.SendResult)8 RemotingException (org.apache.rocketmq.remoting.exception.RemotingException)6 Mockito.doAnswer (org.mockito.Mockito.doAnswer)6 InvocationOnMock (org.mockito.invocation.InvocationOnMock)6 Answer (org.mockito.stubbing.Answer)6 MQBrokerException (org.apache.rocketmq.client.exception.MQBrokerException)4 InvokeCallback (org.apache.rocketmq.remoting.InvokeCallback)4 RemotingTimeoutException (org.apache.rocketmq.remoting.exception.RemotingTimeoutException)4 CountDownLatch (java.util.concurrent.CountDownLatch)2 MQClientException (org.apache.rocketmq.client.exception.MQClientException)2 CheckForbiddenContext (org.apache.rocketmq.client.hook.CheckForbiddenContext)2 SendMessageHook (org.apache.rocketmq.client.hook.SendMessageHook)2 DefaultMQProducerImpl (org.apache.rocketmq.client.impl.producer.DefaultMQProducerImpl)2 DefaultMQProducer (org.apache.rocketmq.client.producer.DefaultMQProducer)2 SendCallback (org.apache.rocketmq.client.producer.SendCallback)2 TransactionSendResult (org.apache.rocketmq.client.producer.TransactionSendResult)2