Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
s920243400
Rocketmq
提交
9d8bd590
R
Rocketmq
项目概览
s920243400
/
Rocketmq
与 Fork 源项目一致
Fork自
Apache RocketMQ / Rocketmq
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
Rocketmq
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
9d8bd590
编写于
1月 19, 2017
作者:
Y
yukon
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[ROCKETMQ-52] Add unit tests for DefaultMQPushConsumer
上级
acf9bc37
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
182 addition
and
2 deletion
+182
-2
client/src/test/java/org/apache/rocketmq/client/consumer/DefaultMQPullConsumerTest.java
...e/rocketmq/client/consumer/DefaultMQPullConsumerTest.java
+1
-1
client/src/test/java/org/apache/rocketmq/client/consumer/DefaultMQPushConsumerTest.java
...e/rocketmq/client/consumer/DefaultMQPushConsumerTest.java
+180
-0
client/src/test/java/org/apache/rocketmq/client/producer/DefaultMQProducerTest.java
...pache/rocketmq/client/producer/DefaultMQProducerTest.java
+1
-1
未找到文件。
client/src/test/java/org/apache/rocketmq/client/consumer/DefaultMQPullConsumerTest.java
浏览文件 @
9d8bd590
...
...
@@ -146,7 +146,7 @@ public class DefaultMQPullConsumerTest {
});
}
private
PullResultExt
createPullResult
(
PullMessageRequestHeader
requestHeader
,
PullStatus
pullStatus
,
List
<
MessageExt
>
messageExtList
)
{
private
PullResultExt
createPullResult
(
PullMessageRequestHeader
requestHeader
,
PullStatus
pullStatus
,
List
<
MessageExt
>
messageExtList
)
throws
Exception
{
return
new
PullResultExt
(
pullStatus
,
requestHeader
.
getQueueOffset
()
+
messageExtList
.
size
(),
123
,
2048
,
messageExtList
,
0
,
new
byte
[]
{});
}
}
\ No newline at end of file
client/src/test/java/org/apache/rocketmq/client/consumer/DefaultMQPushConsumerTest.java
0 → 100644
浏览文件 @
9d8bd590
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
org.apache.rocketmq.client.consumer
;
import
java.io.ByteArrayOutputStream
;
import
java.lang.reflect.Field
;
import
java.net.InetSocketAddress
;
import
java.util.Collections
;
import
java.util.List
;
import
java.util.concurrent.CountDownLatch
;
import
org.apache.rocketmq.client.ClientConfig
;
import
org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext
;
import
org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus
;
import
org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently
;
import
org.apache.rocketmq.client.exception.MQBrokerException
;
import
org.apache.rocketmq.client.impl.CommunicationMode
;
import
org.apache.rocketmq.client.impl.FindBrokerResult
;
import
org.apache.rocketmq.client.impl.MQClientAPIImpl
;
import
org.apache.rocketmq.client.impl.MQClientManager
;
import
org.apache.rocketmq.client.impl.consumer.ConsumeMessageConcurrentlyService
;
import
org.apache.rocketmq.client.impl.consumer.DefaultMQPushConsumerImpl
;
import
org.apache.rocketmq.client.impl.consumer.ProcessQueue
;
import
org.apache.rocketmq.client.impl.consumer.PullAPIWrapper
;
import
org.apache.rocketmq.client.impl.consumer.PullMessageService
;
import
org.apache.rocketmq.client.impl.consumer.PullRequest
;
import
org.apache.rocketmq.client.impl.consumer.PullResultExt
;
import
org.apache.rocketmq.client.impl.factory.MQClientInstance
;
import
org.apache.rocketmq.common.message.MessageClientExt
;
import
org.apache.rocketmq.common.message.MessageDecoder
;
import
org.apache.rocketmq.common.message.MessageExt
;
import
org.apache.rocketmq.common.message.MessageQueue
;
import
org.apache.rocketmq.common.protocol.header.PullMessageRequestHeader
;
import
org.apache.rocketmq.remoting.exception.RemotingException
;
import
org.junit.After
;
import
org.junit.Before
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.mockito.Mock
;
import
org.mockito.Spy
;
import
org.mockito.invocation.InvocationOnMock
;
import
org.mockito.junit.MockitoJUnitRunner
;
import
org.mockito.stubbing.Answer
;
import
static
org
.
apache
.
rocketmq
.
client
.
producer
.
DefaultMQProducerTest
.
createTopicRoute
;
import
static
org
.
assertj
.
core
.
api
.
Assertions
.
assertThat
;
import
static
org
.
mockito
.
ArgumentMatchers
.
any
;
import
static
org
.
mockito
.
ArgumentMatchers
.
anyBoolean
;
import
static
org
.
mockito
.
ArgumentMatchers
.
anyLong
;
import
static
org
.
mockito
.
ArgumentMatchers
.
anyString
;
import
static
org
.
mockito
.
ArgumentMatchers
.
nullable
;
import
static
org
.
mockito
.
Mockito
.
doAnswer
;
import
static
org
.
mockito
.
Mockito
.
spy
;
import
static
org
.
mockito
.
Mockito
.
when
;
@RunWith
(
MockitoJUnitRunner
.
class
)
public
class
DefaultMQPushConsumerTest
{
private
String
consumerGroup
;
private
String
topic
=
"FooBar"
;
private
String
brokerName
=
"BrokerA"
;
@Spy
private
MQClientInstance
mQClientFactory
=
MQClientManager
.
getInstance
().
getAndCreateMQClientInstance
(
new
ClientConfig
());
@Mock
private
MQClientAPIImpl
mQClientAPIImpl
;
private
PullAPIWrapper
pullAPIWrapper
;
private
DefaultMQPushConsumer
pushConsumer
;
@Before
public
void
init
()
throws
Exception
{
consumerGroup
=
"FooBarGroup"
+
System
.
currentTimeMillis
();
pullAPIWrapper
=
spy
(
new
PullAPIWrapper
(
mQClientFactory
,
consumerGroup
,
false
));
pushConsumer
=
new
DefaultMQPushConsumer
(
consumerGroup
);
pushConsumer
.
setNamesrvAddr
(
"127.0.0.1:9876"
);
pushConsumer
.
setPullInterval
(
60
*
1000
);
pushConsumer
.
registerMessageListener
(
new
MessageListenerConcurrently
()
{
@Override
public
ConsumeConcurrentlyStatus
consumeMessage
(
List
<
MessageExt
>
msgs
,
ConsumeConcurrentlyContext
context
)
{
return
null
;
}
});
pushConsumer
.
subscribe
(
topic
,
"*"
);
pushConsumer
.
start
();
DefaultMQPushConsumerImpl
pushConsumerImpl
=
pushConsumer
.
getDefaultMQPushConsumerImpl
();
Field
field
=
DefaultMQPushConsumerImpl
.
class
.
getDeclaredField
(
"mQClientFactory"
);
field
.
setAccessible
(
true
);
field
.
set
(
pushConsumerImpl
,
mQClientFactory
);
field
=
MQClientInstance
.
class
.
getDeclaredField
(
"mQClientAPIImpl"
);
field
.
setAccessible
(
true
);
field
.
set
(
mQClientFactory
,
mQClientAPIImpl
);
field
=
DefaultMQPushConsumerImpl
.
class
.
getDeclaredField
(
"pullAPIWrapper"
);
field
.
setAccessible
(
true
);
field
.
set
(
pushConsumerImpl
,
pullAPIWrapper
);
when
(
mQClientFactory
.
findBrokerAddressInSubscribe
(
anyString
(),
anyLong
(),
anyBoolean
())).
thenReturn
(
new
FindBrokerResult
(
"127.0.0.1:10911"
,
false
));
mQClientFactory
.
registerConsumer
(
consumerGroup
,
pushConsumerImpl
);
mQClientFactory
.
start
();
when
(
mQClientAPIImpl
.
getTopicRouteInfoFromNameServer
(
anyString
(),
anyLong
())).
thenReturn
(
createTopicRoute
());
}
@After
public
void
terminate
()
{
pushConsumer
.
shutdown
();
}
@Test
public
void
testPullMessage_Success
()
throws
InterruptedException
,
RemotingException
,
MQBrokerException
{
final
CountDownLatch
countDownLatch
=
new
CountDownLatch
(
1
);
final
MessageExt
[]
messageExts
=
new
MessageExt
[
1
];
pushConsumer
.
getDefaultMQPushConsumerImpl
().
setConsumeMessageService
(
new
ConsumeMessageConcurrentlyService
(
pushConsumer
.
getDefaultMQPushConsumerImpl
(),
new
MessageListenerConcurrently
()
{
@Override
public
ConsumeConcurrentlyStatus
consumeMessage
(
List
<
MessageExt
>
msgs
,
ConsumeConcurrentlyContext
context
)
{
messageExts
[
0
]
=
msgs
.
get
(
0
);
countDownLatch
.
countDown
();
return
null
;
}
}));
doAnswer
(
new
Answer
()
{
@Override
public
Object
answer
(
InvocationOnMock
mock
)
throws
Throwable
{
PullMessageRequestHeader
requestHeader
=
mock
.
getArgument
(
1
);
MessageClientExt
messageClientExt
=
new
MessageClientExt
();
messageClientExt
.
setTopic
(
topic
);
messageClientExt
.
setQueueId
(
0
);
messageClientExt
.
setMsgId
(
"123"
);
messageClientExt
.
setBody
(
new
byte
[]
{
'a'
});
messageClientExt
.
setOffsetMsgId
(
"234"
);
messageClientExt
.
setBornHost
(
new
InetSocketAddress
(
8080
));
messageClientExt
.
setStoreHost
(
new
InetSocketAddress
(
8080
));
PullResult
pullResult
=
createPullResult
(
requestHeader
,
PullStatus
.
FOUND
,
Collections
.<
MessageExt
>
singletonList
(
messageClientExt
));
((
PullCallback
)
mock
.
getArgument
(
4
)).
onSuccess
(
pullResult
);
return
null
;
}
}).
when
(
mQClientAPIImpl
).
pullMessage
(
anyString
(),
any
(
PullMessageRequestHeader
.
class
),
anyLong
(),
any
(
CommunicationMode
.
class
),
nullable
(
PullCallback
.
class
));
PullMessageService
pullMessageService
=
mQClientFactory
.
getPullMessageService
();
pullMessageService
.
executePullRequestImmediately
(
createPullRequest
());
countDownLatch
.
await
();
assertThat
(
messageExts
[
0
].
getTopic
()).
isEqualTo
(
topic
);
assertThat
(
messageExts
[
0
].
getBody
()).
isEqualTo
(
new
byte
[]
{
'a'
});
}
private
PullRequest
createPullRequest
()
{
PullRequest
pullRequest
=
new
PullRequest
();
pullRequest
.
setConsumerGroup
(
consumerGroup
);
pullRequest
.
setNextOffset
(
1024
);
MessageQueue
messageQueue
=
new
MessageQueue
();
messageQueue
.
setBrokerName
(
brokerName
);
messageQueue
.
setQueueId
(
0
);
messageQueue
.
setTopic
(
topic
);
pullRequest
.
setMessageQueue
(
messageQueue
);
pullRequest
.
setProcessQueue
(
new
ProcessQueue
());
return
pullRequest
;
}
private
PullResultExt
createPullResult
(
PullMessageRequestHeader
requestHeader
,
PullStatus
pullStatus
,
List
<
MessageExt
>
messageExtList
)
throws
Exception
{
ByteArrayOutputStream
outputStream
=
new
ByteArrayOutputStream
();
for
(
MessageExt
messageExt
:
messageExtList
)
{
outputStream
.
write
(
MessageDecoder
.
encode
(
messageExt
,
false
));
}
return
new
PullResultExt
(
pullStatus
,
requestHeader
.
getQueueOffset
()
+
messageExtList
.
size
(),
123
,
2048
,
messageExtList
,
0
,
outputStream
.
toByteArray
());
}
}
\ No newline at end of file
client/src/test/java/org/apache/rocketmq/client/producer/DefaultMQProducerTest.java
浏览文件 @
9d8bd590
...
...
@@ -190,7 +190,7 @@ public class DefaultMQProducerTest {
}
}
p
rivate
TopicRouteData
createTopicRoute
()
{
p
ublic
static
TopicRouteData
createTopicRoute
()
{
TopicRouteData
topicRouteData
=
new
TopicRouteData
();
topicRouteData
.
setFilterServerTable
(
new
HashMap
<
String
,
List
<
String
>>());
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录