Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
s920243400
Rocketmq
提交
26878e9c
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看板
提交
26878e9c
编写于
5月 14, 2019
作者:
每一步都要留下深脚印
提交者:
dinglei
5月 14, 2019
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[RIP-10] Add test cases for QueryMsgByUniqueKeySubCommand (#945)
上级
c7bff8aa
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
250 addition
and
3 deletion
+250
-3
tools/src/main/java/org/apache/rocketmq/tools/command/message/QueryMsgByUniqueKeySubCommand.java
.../tools/command/message/QueryMsgByUniqueKeySubCommand.java
+20
-3
tools/src/test/java/org/apache/rocketmq/tools/command/message/QueryMsgByUniqueKeySubCommandTest.java
...ls/command/message/QueryMsgByUniqueKeySubCommandTest.java
+230
-0
未找到文件。
tools/src/main/java/org/apache/rocketmq/tools/command/message/QueryMsgByUniqueKeySubCommand.java
浏览文件 @
26878e9c
...
...
@@ -39,6 +39,24 @@ import org.apache.rocketmq.tools.command.SubCommandException;
public
class
QueryMsgByUniqueKeySubCommand
implements
SubCommand
{
private
DefaultMQAdminExt
defaultMQAdminExt
;
private
DefaultMQAdminExt
createMQAdminExt
(
RPCHook
rpcHook
)
throws
SubCommandException
{
if
(
this
.
defaultMQAdminExt
!=
null
)
{
return
defaultMQAdminExt
;
}
else
{
defaultMQAdminExt
=
new
DefaultMQAdminExt
(
rpcHook
);
defaultMQAdminExt
.
setInstanceName
(
Long
.
toString
(
System
.
currentTimeMillis
()));
try
{
defaultMQAdminExt
.
start
();
}
catch
(
Exception
e
)
{
throw
new
SubCommandException
(
this
.
getClass
().
getSimpleName
()
+
" command failed"
,
e
);
}
return
defaultMQAdminExt
;
}
}
public
static
void
queryById
(
final
DefaultMQAdminExt
admin
,
final
String
topic
,
final
String
msgId
)
throws
MQClientException
,
RemotingException
,
MQBrokerException
,
InterruptedException
,
IOException
{
...
...
@@ -182,11 +200,10 @@ public class QueryMsgByUniqueKeySubCommand implements SubCommand {
@Override
public
void
execute
(
CommandLine
commandLine
,
Options
options
,
RPCHook
rpcHook
)
throws
SubCommandException
{
DefaultMQAdminExt
defaultMQAdminExt
=
new
DefaultMQAdminExt
(
rpcHook
);
defaultMQAdminExt
.
setInstanceName
(
Long
.
toString
(
System
.
currentTimeMillis
()));
try
{
defaultMQAdminExt
.
start
();
defaultMQAdminExt
=
createMQAdminExt
(
rpcHook
);
final
String
msgId
=
commandLine
.
getOptionValue
(
'i'
).
trim
();
final
String
topic
=
commandLine
.
getOptionValue
(
't'
).
trim
();
...
...
tools/src/test/java/org/apache/rocketmq/tools/command/message/QueryMsgByUniqueKeySubCommandTest.java
0 → 100644
浏览文件 @
26878e9c
/*
* 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.tools.command.message
;
import
org.apache.commons.cli.CommandLine
;
import
org.apache.commons.cli.Options
;
import
org.apache.commons.cli.PosixParser
;
import
org.apache.rocketmq.client.ClientConfig
;
import
org.apache.rocketmq.client.exception.MQBrokerException
;
import
org.apache.rocketmq.client.exception.MQClientException
;
import
org.apache.rocketmq.client.impl.MQAdminImpl
;
import
org.apache.rocketmq.client.impl.MQClientAPIImpl
;
import
org.apache.rocketmq.client.impl.MQClientManager
;
import
org.apache.rocketmq.client.impl.factory.MQClientInstance
;
import
org.apache.rocketmq.common.MixAll
;
import
org.apache.rocketmq.common.admin.ConsumeStats
;
import
org.apache.rocketmq.common.admin.OffsetWrapper
;
import
org.apache.rocketmq.common.message.MessageExt
;
import
org.apache.rocketmq.common.message.MessageQueue
;
import
org.apache.rocketmq.common.protocol.body.*
;
import
org.apache.rocketmq.common.protocol.heartbeat.ConsumeType
;
import
org.apache.rocketmq.common.protocol.route.BrokerData
;
import
org.apache.rocketmq.common.protocol.route.TopicRouteData
;
import
org.apache.rocketmq.remoting.exception.RemotingConnectException
;
import
org.apache.rocketmq.remoting.exception.RemotingException
;
import
org.apache.rocketmq.remoting.exception.RemotingSendRequestException
;
import
org.apache.rocketmq.remoting.exception.RemotingTimeoutException
;
import
org.apache.rocketmq.remoting.protocol.LanguageCode
;
import
org.apache.rocketmq.srvutil.ServerUtil
;
import
org.apache.rocketmq.tools.admin.DefaultMQAdminExt
;
import
org.apache.rocketmq.tools.admin.DefaultMQAdminExtImpl
;
import
org.apache.rocketmq.tools.command.SubCommandException
;
import
org.junit.Before
;
import
org.junit.Test
;
import
java.lang.reflect.Field
;
import
java.net.InetSocketAddress
;
import
java.util.*
;
import
static
org
.
mockito
.
ArgumentMatchers
.*;
import
static
org
.
mockito
.
Mockito
.
mock
;
import
static
org
.
mockito
.
Mockito
.
when
;
public
class
QueryMsgByUniqueKeySubCommandTest
{
private
static
QueryMsgByUniqueKeySubCommand
cmd
=
new
QueryMsgByUniqueKeySubCommand
();
private
static
DefaultMQAdminExt
defaultMQAdminExt
;
private
static
DefaultMQAdminExtImpl
defaultMQAdminExtImpl
;
private
static
MQClientInstance
mqClientInstance
=
MQClientManager
.
getInstance
().
getAndCreateMQClientInstance
(
new
ClientConfig
());
private
static
MQClientAPIImpl
mQClientAPIImpl
;
private
static
MQAdminImpl
mQAdminImpl
;
@Before
public
void
before
()
throws
NoSuchFieldException
,
IllegalAccessException
,
InterruptedException
,
RemotingException
,
MQClientException
,
MQBrokerException
{
mQClientAPIImpl
=
mock
(
MQClientAPIImpl
.
class
);
mQAdminImpl
=
mock
(
MQAdminImpl
.
class
);
defaultMQAdminExt
=
new
DefaultMQAdminExt
();
defaultMQAdminExtImpl
=
new
DefaultMQAdminExtImpl
(
defaultMQAdminExt
,
1000
);
Field
field
=
DefaultMQAdminExtImpl
.
class
.
getDeclaredField
(
"mqClientInstance"
);
field
.
setAccessible
(
true
);
field
.
set
(
defaultMQAdminExtImpl
,
mqClientInstance
);
field
=
MQClientInstance
.
class
.
getDeclaredField
(
"mQClientAPIImpl"
);
field
.
setAccessible
(
true
);
field
.
set
(
mqClientInstance
,
mQClientAPIImpl
);
field
=
MQClientInstance
.
class
.
getDeclaredField
(
"mQAdminImpl"
);
field
.
setAccessible
(
true
);
field
.
set
(
mqClientInstance
,
mQAdminImpl
);
field
=
DefaultMQAdminExt
.
class
.
getDeclaredField
(
"defaultMQAdminExtImpl"
);
field
.
setAccessible
(
true
);
field
.
set
(
defaultMQAdminExt
,
defaultMQAdminExtImpl
);
ConsumeMessageDirectlyResult
result
=
new
ConsumeMessageDirectlyResult
();
result
.
setConsumeResult
(
CMResult
.
CR_SUCCESS
);
result
.
setRemark
(
"customRemark_122333444"
);
when
(
mQClientAPIImpl
.
consumeMessageDirectly
(
anyString
(),
anyString
(),
anyString
(),
anyString
(),
anyLong
())).
thenReturn
(
result
);
MessageExt
retMsgExt
=
new
MessageExt
();
retMsgExt
.
setMsgId
(
"0A3A54F7BF7D18B4AAC28A3FA2CF0000"
);
retMsgExt
.
setBody
(
"this is message ext body"
.
getBytes
());
retMsgExt
.
setTopic
(
"testTopic"
);
retMsgExt
.
setTags
(
"testTags"
);
retMsgExt
.
setStoreHost
(
new
InetSocketAddress
(
"127.0.0.1"
,
8899
));
retMsgExt
.
setBornHost
(
new
InetSocketAddress
(
"127.0.0.1"
,
7788
));
retMsgExt
.
setQueueId
(
1
);
retMsgExt
.
setQueueOffset
(
12L
);
retMsgExt
.
setCommitLogOffset
(
123
);
retMsgExt
.
setReconsumeTimes
(
2
);
retMsgExt
.
setBornTimestamp
(
System
.
currentTimeMillis
());
retMsgExt
.
setStoreTimestamp
(
System
.
currentTimeMillis
());
when
(
mQAdminImpl
.
viewMessage
(
anyString
())).
thenReturn
(
retMsgExt
);
when
(
mQAdminImpl
.
queryMessageByUniqKey
(
anyString
(),
anyString
())).
thenReturn
(
retMsgExt
);
TopicRouteData
topicRouteData
=
new
TopicRouteData
();
List
<
BrokerData
>
brokerDataList
=
new
ArrayList
<
BrokerData
>();
BrokerData
brokerData
=
new
BrokerData
();
HashMap
<
Long
,
String
>
brokerAddrs
=
new
HashMap
<
Long
,
String
>();
brokerAddrs
.
put
(
MixAll
.
MASTER_ID
,
"127.0.0.1:9876"
);
brokerData
.
setBrokerAddrs
(
brokerAddrs
);
brokerDataList
.
add
(
brokerData
);
topicRouteData
.
setBrokerDatas
(
brokerDataList
);
when
(
mQClientAPIImpl
.
getTopicRouteInfoFromNameServer
(
anyString
(),
anyLong
())).
thenReturn
(
topicRouteData
);
GroupList
groupList
=
new
GroupList
();
HashSet
<
String
>
groupSets
=
new
HashSet
<
String
>();
groupSets
.
add
(
"testGroup"
);
groupList
.
setGroupList
(
groupSets
);
when
(
mQClientAPIImpl
.
queryTopicConsumeByWho
(
anyString
(),
anyString
(),
anyLong
())).
thenReturn
(
groupList
);
ConsumeStats
consumeStats
=
new
ConsumeStats
();
consumeStats
.
setConsumeTps
(
100
*
10000
);
HashMap
<
MessageQueue
,
OffsetWrapper
>
offsetTable
=
new
HashMap
<
MessageQueue
,
OffsetWrapper
>();
MessageQueue
messageQueue
=
new
MessageQueue
();
messageQueue
.
setBrokerName
(
"messageQueue BrokerName testing"
);
messageQueue
.
setTopic
(
"messageQueue topic"
);
messageQueue
.
setQueueId
(
1
);
OffsetWrapper
offsetWrapper
=
new
OffsetWrapper
();
offsetWrapper
.
setBrokerOffset
(
100
);
offsetWrapper
.
setConsumerOffset
(
200
);
offsetWrapper
.
setLastTimestamp
(
System
.
currentTimeMillis
());
offsetTable
.
put
(
messageQueue
,
offsetWrapper
);
consumeStats
.
setOffsetTable
(
offsetTable
);
when
(
mQClientAPIImpl
.
getConsumeStats
(
anyString
(),
anyString
(),
(
String
)
isNull
(),
anyLong
())).
thenReturn
(
consumeStats
);
ClusterInfo
clusterInfo
=
new
ClusterInfo
();
HashMap
<
String
,
BrokerData
>
brokerAddrTable
=
new
HashMap
<
String
,
BrokerData
>();
brokerAddrTable
.
put
(
"key"
,
brokerData
);
clusterInfo
.
setBrokerAddrTable
(
brokerAddrTable
);
HashMap
<
String
,
Set
<
String
>>
clusterAddrTable
=
new
HashMap
<
String
,
Set
<
String
>>();
Set
<
String
>
addrSet
=
new
HashSet
<
String
>();
addrSet
.
add
(
"127.0.0.1:9876"
);
clusterAddrTable
.
put
(
"key"
,
addrSet
);
clusterInfo
.
setClusterAddrTable
(
clusterAddrTable
);
when
(
mQClientAPIImpl
.
getBrokerClusterInfo
(
anyLong
())).
thenReturn
(
clusterInfo
);
field
=
QueryMsgByUniqueKeySubCommand
.
class
.
getDeclaredField
(
"defaultMQAdminExt"
);
field
.
setAccessible
(
true
);
field
.
set
(
cmd
,
defaultMQAdminExt
);
}
@Test
public
void
testExecuteConsumeActively
()
throws
SubCommandException
,
InterruptedException
,
MQBrokerException
,
RemotingTimeoutException
,
RemotingSendRequestException
,
RemotingConnectException
{
ConsumerConnection
consumerConnection
=
new
ConsumerConnection
();
consumerConnection
.
setConsumeType
(
ConsumeType
.
CONSUME_ACTIVELY
);
HashSet
<
Connection
>
connectionSet
=
new
HashSet
<>();
Connection
conn
=
new
Connection
();
conn
.
setClientId
(
"clientIdTest"
);
conn
.
setClientAddr
(
"clientAddrTest"
);
conn
.
setLanguage
(
LanguageCode
.
JAVA
);
conn
.
setVersion
(
1
);
connectionSet
.
add
(
conn
);
consumerConnection
.
setConnectionSet
(
connectionSet
);
when
(
mQClientAPIImpl
.
getConsumerConnectionList
(
anyString
(),
anyString
(),
anyLong
())).
thenReturn
(
consumerConnection
);
Options
options
=
ServerUtil
.
buildCommandlineOptions
(
new
Options
());
String
[]
args
=
new
String
[]{
"-t myTopicTest"
,
"-i msgId"
};
CommandLine
commandLine
=
ServerUtil
.
parseCmdLine
(
"mqadmin "
,
args
,
cmd
.
buildCommandlineOptions
(
options
),
new
PosixParser
());
cmd
.
execute
(
commandLine
,
options
,
null
);
}
@Test
public
void
testExecuteConsumePassively
()
throws
SubCommandException
,
InterruptedException
,
MQBrokerException
,
RemotingTimeoutException
,
RemotingSendRequestException
,
RemotingConnectException
{
ConsumerConnection
consumerConnection
=
new
ConsumerConnection
();
consumerConnection
.
setConsumeType
(
ConsumeType
.
CONSUME_PASSIVELY
);
HashSet
<
Connection
>
connectionSet
=
new
HashSet
<>();
Connection
conn
=
new
Connection
();
conn
.
setClientId
(
"clientIdTestStr"
);
conn
.
setClientAddr
(
"clientAddrTestStr"
);
conn
.
setLanguage
(
LanguageCode
.
JAVA
);
conn
.
setVersion
(
2
);
connectionSet
.
add
(
conn
);
consumerConnection
.
setConnectionSet
(
connectionSet
);
when
(
mQClientAPIImpl
.
getConsumerConnectionList
(
anyString
(),
anyString
(),
anyLong
())).
thenReturn
(
consumerConnection
);
Options
options
=
ServerUtil
.
buildCommandlineOptions
(
new
Options
());
String
[]
args
=
new
String
[]{
"-t myTopicTest"
,
"-i 7F000001000004D20000000000000066"
};
CommandLine
commandLine
=
ServerUtil
.
parseCmdLine
(
"mqadmin "
,
args
,
cmd
.
buildCommandlineOptions
(
options
),
new
PosixParser
());
cmd
.
execute
(
commandLine
,
options
,
null
);
}
@Test
public
void
testExecuteWithConsumerGroupAndClientId
()
throws
SubCommandException
{
Options
options
=
ServerUtil
.
buildCommandlineOptions
(
new
Options
());
String
[]
args
=
new
String
[]{
"-t myTopicTest"
,
"-i 0A3A54F7BF7D18B4AAC28A3FA2CF0000"
,
"-g producerGroupName"
,
"-d clientId"
};
CommandLine
commandLine
=
ServerUtil
.
parseCmdLine
(
"mqadmin "
,
args
,
cmd
.
buildCommandlineOptions
(
options
),
new
PosixParser
());
cmd
.
execute
(
commandLine
,
options
,
null
);
System
.
out
.
println
();
System
.
out
.
println
(
"commandName="
+
cmd
.
commandName
());
System
.
out
.
println
(
"commandDesc="
+
cmd
.
commandDesc
());
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录