Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Apache RocketMQ
Rocketmq
提交
67751d81
R
Rocketmq
项目概览
Apache RocketMQ
/
Rocketmq
上一次同步 大约 3 年
通知
268
Star
16139
Fork
68
代码
文件
提交
分支
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看板
提交
67751d81
编写于
8月 22, 2019
作者:
K
King
提交者:
Heng Du
8月 22, 2019
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add unit test for lite pull consumer (#1410)
* Add unit test for lite pull consumer. * Add synchronized to poll function.
上级
2ab9e850
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
178 addition
and
32 deletion
+178
-32
client/src/main/java/org/apache/rocketmq/client/impl/consumer/DefaultLitePullConsumerImpl.java
...tmq/client/impl/consumer/DefaultLitePullConsumerImpl.java
+2
-2
client/src/test/java/org/apache/rocketmq/client/consumer/DefaultLitePullConsumerTest.java
...rocketmq/client/consumer/DefaultLitePullConsumerTest.java
+176
-30
未找到文件。
client/src/main/java/org/apache/rocketmq/client/impl/consumer/DefaultLitePullConsumerImpl.java
浏览文件 @
67751d81
...
...
@@ -327,7 +327,7 @@ public class DefaultLitePullConsumerImpl implements MQConsumerInner {
log
.
error
(
"ScheduledTask fetchMessageQueuesAndCompare exception"
,
e
);
}
}
},
1000
*
2
0
,
this
.
getDefaultLitePullConsumer
().
getTopicMetadataCheckIntervalMillis
(),
TimeUnit
.
MILLISECONDS
);
},
1000
*
1
0
,
this
.
getDefaultLitePullConsumer
().
getTopicMetadataCheckIntervalMillis
(),
TimeUnit
.
MILLISECONDS
);
}
private
void
operateAfterRunning
()
throws
MQClientException
{
...
...
@@ -491,7 +491,7 @@ public class DefaultLitePullConsumerImpl implements MQConsumerInner {
}
}
public
List
<
MessageExt
>
poll
(
long
timeout
)
{
public
synchronized
List
<
MessageExt
>
poll
(
long
timeout
)
{
try
{
checkServiceState
();
if
(
timeout
<
0
)
...
...
client/src/test/java/org/apache/rocketmq/client/consumer/DefaultLitePullConsumerTest.java
浏览文件 @
67751d81
...
...
@@ -40,6 +40,7 @@ import org.apache.rocketmq.client.impl.consumer.PullResultExt;
import
org.apache.rocketmq.client.impl.consumer.RebalanceImpl
;
import
org.apache.rocketmq.client.impl.consumer.RebalanceService
;
import
org.apache.rocketmq.client.impl.factory.MQClientInstance
;
import
org.apache.rocketmq.common.MixAll
;
import
org.apache.rocketmq.common.message.MessageClientExt
;
import
org.apache.rocketmq.common.message.MessageDecoder
;
import
org.apache.rocketmq.common.message.MessageExt
;
...
...
@@ -83,6 +84,7 @@ public class DefaultLitePullConsumerTest {
private
String
consumerGroup
=
"LitePullConsumerGroup"
;
private
String
topic
=
"LitePullConsumerTest"
;
private
String
brokerName
=
"BrokerA"
;
private
boolean
flag
=
false
;
@Before
public
void
init
()
throws
Exception
{
...
...
@@ -155,7 +157,7 @@ public class DefaultLitePullConsumerTest {
}
@Test
public
void
testFetchMess
e
ageQueues_FetchMessageQueuesBeforeStart
()
throws
Exception
{
public
void
testFetchMessageQueues_FetchMessageQueuesBeforeStart
()
throws
Exception
{
DefaultLitePullConsumer
litePullConsumer
=
createNotStartLitePullConsumer
();
try
{
litePullConsumer
.
fetchMessageQueues
(
topic
);
...
...
@@ -167,6 +169,22 @@ public class DefaultLitePullConsumerTest {
}
}
@Test
public
void
testSeek_SeekOffsetSuccess
()
throws
Exception
{
DefaultLitePullConsumer
litePullConsumer
=
createStartLitePullConsumer
();
when
(
mQAdminImpl
.
minOffset
(
any
(
MessageQueue
.
class
))).
thenReturn
(
0L
);
when
(
mQAdminImpl
.
maxOffset
(
any
(
MessageQueue
.
class
))).
thenReturn
(
500L
);
MessageQueue
messageQueue
=
createMessageQueue
();
litePullConsumer
.
assign
(
Collections
.
singletonList
(
messageQueue
));
long
offset
=
litePullConsumer
.
committed
(
messageQueue
);
litePullConsumer
.
seek
(
messageQueue
,
offset
);
Field
field
=
DefaultLitePullConsumerImpl
.
class
.
getDeclaredField
(
"assignedMessageQueue"
);
field
.
setAccessible
(
true
);
AssignedMessageQueue
assignedMessageQueue
=
(
AssignedMessageQueue
)
field
.
get
(
litePullConsumerImpl
);
assertEquals
(
assignedMessageQueue
.
getSeekOffset
(
messageQueue
),
offset
);
litePullConsumer
.
shutdown
();
}
@Test
public
void
testSeek_SeekOffsetIllegal
()
throws
Exception
{
DefaultLitePullConsumer
litePullConsumer
=
createStartLitePullConsumer
();
...
...
@@ -190,21 +208,6 @@ public class DefaultLitePullConsumerTest {
litePullConsumer
.
shutdown
();
}
@Test
public
void
testSeek_SeekOffsetSuccess
()
throws
Exception
{
DefaultLitePullConsumer
litePullConsumer
=
createStartLitePullConsumer
();
when
(
mQAdminImpl
.
minOffset
(
any
(
MessageQueue
.
class
))).
thenReturn
(
0L
);
when
(
mQAdminImpl
.
maxOffset
(
any
(
MessageQueue
.
class
))).
thenReturn
(
100L
);
MessageQueue
messageQueue
=
createMessageQueue
();
litePullConsumer
.
assign
(
Collections
.
singletonList
(
messageQueue
));
litePullConsumer
.
seek
(
messageQueue
,
50
);
Field
field
=
DefaultLitePullConsumerImpl
.
class
.
getDeclaredField
(
"assignedMessageQueue"
);
field
.
setAccessible
(
true
);
AssignedMessageQueue
assignedMessageQueue
=
(
AssignedMessageQueue
)
field
.
get
(
litePullConsumerImpl
);
assertEquals
(
assignedMessageQueue
.
getSeekOffset
(
messageQueue
),
50
);
litePullConsumer
.
shutdown
();
}
@Test
public
void
testSeek_MessageQueueNotInAssignList
()
throws
Exception
{
DefaultLitePullConsumer
litePullConsumer
=
createStartLitePullConsumer
();
...
...
@@ -216,6 +219,34 @@ public class DefaultLitePullConsumerTest {
}
finally
{
litePullConsumer
.
shutdown
();
}
litePullConsumer
=
createSubscribeLitePullConsumer
();
try
{
litePullConsumer
.
seek
(
createMessageQueue
(),
0
);
failBecauseExceptionWasNotThrown
(
MQClientException
.
class
);
}
catch
(
MQClientException
e
)
{
assertThat
(
e
).
hasMessageContaining
(
"The message queue is not in assigned list, may be rebalancing"
);
}
finally
{
litePullConsumer
.
shutdown
();
}
}
@Test
public
void
testOffsetForTimestamp_FailedAndSuccess
()
throws
Exception
{
MessageQueue
messageQueue
=
createMessageQueue
();
DefaultLitePullConsumer
litePullConsumer
=
createNotStartLitePullConsumer
();
try
{
litePullConsumer
.
offsetForTimestamp
(
messageQueue
,
123456L
);
failBecauseExceptionWasNotThrown
(
IllegalStateException
.
class
);
}
catch
(
IllegalStateException
e
)
{
assertThat
(
e
).
hasMessageContaining
(
"The consumer not running, please start it first."
);
}
finally
{
litePullConsumer
.
shutdown
();
}
doReturn
(
123L
).
when
(
mQAdminImpl
).
searchOffset
(
any
(
MessageQueue
.
class
),
anyLong
());
litePullConsumer
=
createStartLitePullConsumer
();
long
offset
=
litePullConsumer
.
offsetForTimestamp
(
messageQueue
,
123456L
);
assertThat
(
offset
).
isEqualTo
(
123L
);
}
@Test
...
...
@@ -238,12 +269,120 @@ public class DefaultLitePullConsumerTest {
}
}
private
MessageQueue
createMessageQueue
()
{
MessageQueue
messageQueue
=
new
MessageQueue
();
messageQueue
.
setBrokerName
(
brokerName
);
messageQueue
.
setQueueId
(
0
);
messageQueue
.
setTopic
(
topic
);
return
messageQueue
;
@Test
public
void
testRegisterTopicMessageQueueChangeListener_Success
()
throws
Exception
{
flag
=
false
;
DefaultLitePullConsumer
litePullConsumer
=
createStartLitePullConsumer
();
doReturn
(
Collections
.
emptySet
()).
when
(
mQAdminImpl
).
fetchSubscribeMessageQueues
(
anyString
());
litePullConsumer
.
setTopicMetadataCheckIntervalMillis
(
10
);
litePullConsumer
.
registerTopicMessageQueueChangeListener
(
topic
,
new
TopicMessageQueueChangeListener
()
{
@Override
public
void
onChanged
(
String
topic
,
Set
<
MessageQueue
>
messageQueues
)
{
flag
=
true
;
}
});
Set
<
MessageQueue
>
set
=
new
HashSet
<
MessageQueue
>();
set
.
add
(
createMessageQueue
());
doReturn
(
set
).
when
(
mQAdminImpl
).
fetchSubscribeMessageQueues
(
anyString
());
Thread
.
sleep
(
11
*
1000
);
assertThat
(
flag
).
isTrue
();
}
@Test
public
void
testFlowControl_Success
()
throws
Exception
{
DefaultLitePullConsumer
litePullConsumer
=
createStartLitePullConsumer
();
try
{
MessageQueue
messageQueue
=
createMessageQueue
();
litePullConsumer
.
setPullThresholdForAll
(-
1
);
litePullConsumer
.
assign
(
Collections
.
singletonList
(
messageQueue
));
litePullConsumer
.
setPollTimeoutMillis
(
500
);
List
<
MessageExt
>
result
=
litePullConsumer
.
poll
();
assertThat
(
result
).
isEmpty
();
}
finally
{
litePullConsumer
.
shutdown
();
}
litePullConsumer
=
createStartLitePullConsumer
();
try
{
MessageQueue
messageQueue
=
createMessageQueue
();
litePullConsumer
.
setPullThresholdForQueue
(-
1
);
litePullConsumer
.
assign
(
Collections
.
singletonList
(
messageQueue
));
litePullConsumer
.
setPollTimeoutMillis
(
500
);
List
<
MessageExt
>
result
=
litePullConsumer
.
poll
();
assertThat
(
result
).
isEmpty
();
}
finally
{
litePullConsumer
.
shutdown
();
}
litePullConsumer
=
createStartLitePullConsumer
();
try
{
MessageQueue
messageQueue
=
createMessageQueue
();
litePullConsumer
.
setPullThresholdSizeForQueue
(-
1
);
litePullConsumer
.
assign
(
Collections
.
singletonList
(
messageQueue
));
litePullConsumer
.
setPollTimeoutMillis
(
500
);
List
<
MessageExt
>
result
=
litePullConsumer
.
poll
();
assertThat
(
result
).
isEmpty
();
}
finally
{
litePullConsumer
.
shutdown
();
}
litePullConsumer
=
createStartLitePullConsumer
();
try
{
MessageQueue
messageQueue
=
createMessageQueue
();
litePullConsumer
.
setConsumeMaxSpan
(-
1
);
litePullConsumer
.
assign
(
Collections
.
singletonList
(
messageQueue
));
litePullConsumer
.
setPollTimeoutMillis
(
500
);
List
<
MessageExt
>
result
=
litePullConsumer
.
poll
();
assertThat
(
result
).
isEmpty
();
}
finally
{
litePullConsumer
.
shutdown
();
}
}
@Test
public
void
testCheckConfig_Exception
()
{
DefaultLitePullConsumer
litePullConsumer
=
new
DefaultLitePullConsumer
(
MixAll
.
DEFAULT_CONSUMER_GROUP
);
try
{
litePullConsumer
.
start
();
failBecauseExceptionWasNotThrown
(
MQClientException
.
class
);
}
catch
(
MQClientException
e
)
{
assertThat
(
e
).
hasMessageContaining
(
"consumerGroup can not equal"
);
}
finally
{
litePullConsumer
.
shutdown
();
}
litePullConsumer
=
new
DefaultLitePullConsumer
(
consumerGroup
+
System
.
currentTimeMillis
());
litePullConsumer
.
setMessageModel
(
null
);
try
{
litePullConsumer
.
start
();
failBecauseExceptionWasNotThrown
(
MQClientException
.
class
);
}
catch
(
MQClientException
e
)
{
assertThat
(
e
).
hasMessageContaining
(
"messageModel is null"
);
}
finally
{
litePullConsumer
.
shutdown
();
}
litePullConsumer
=
new
DefaultLitePullConsumer
(
consumerGroup
+
System
.
currentTimeMillis
());
litePullConsumer
.
setAllocateMessageQueueStrategy
(
null
);
try
{
litePullConsumer
.
start
();
failBecauseExceptionWasNotThrown
(
MQClientException
.
class
);
}
catch
(
MQClientException
e
)
{
assertThat
(
e
).
hasMessageContaining
(
"allocateMessageQueueStrategy is null"
);
}
finally
{
litePullConsumer
.
shutdown
();
}
litePullConsumer
=
new
DefaultLitePullConsumer
(
consumerGroup
+
System
.
currentTimeMillis
());
litePullConsumer
.
setConsumerTimeoutMillisWhenSuspend
(
1
);
try
{
litePullConsumer
.
start
();
failBecauseExceptionWasNotThrown
(
MQClientException
.
class
);
}
catch
(
MQClientException
e
)
{
assertThat
(
e
).
hasMessageContaining
(
"Long polling mode, the consumer consumerTimeoutMillisWhenSuspend must greater than brokerSuspendMaxTimeMillis"
);
}
finally
{
litePullConsumer
.
shutdown
();
}
}
private
void
initDefaultLitePullConsumer
(
DefaultLitePullConsumer
litePullConsumer
)
throws
Exception
{
...
...
@@ -310,33 +449,40 @@ public class DefaultLitePullConsumerTest {
DefaultLitePullConsumer
litePullConsumer
=
new
DefaultLitePullConsumer
(
consumerGroup
+
System
.
currentTimeMillis
());
litePullConsumer
.
setNamesrvAddr
(
"127.0.0.1:9876"
);
litePullConsumer
.
subscribe
(
topic
,
"*"
);
litePullConsumer
.
start
();
initDefaultLitePullConsumer
(
litePullConsumer
);
return
litePullConsumer
;
}
private
DefaultLitePullConsumer
create
Broadcas
tLitePullConsumer
()
throws
Exception
{
private
DefaultLitePullConsumer
create
Star
tLitePullConsumer
()
throws
Exception
{
DefaultLitePullConsumer
litePullConsumer
=
new
DefaultLitePullConsumer
(
consumerGroup
+
System
.
currentTimeMillis
());
litePullConsumer
.
setNamesrvAddr
(
"127.0.0.1:9876"
);
litePullConsumer
.
setMessageModel
(
MessageModel
.
BROADCASTING
);
litePullConsumer
.
subscribe
(
topic
,
"*"
);
litePullConsumer
.
start
();
initDefaultLitePullConsumer
(
litePullConsumer
);
return
litePullConsumer
;
}
private
DefaultLitePullConsumer
createStartLitePullConsumer
()
throws
Exception
{
private
DefaultLitePullConsumer
createNotStartLitePullConsumer
()
{
DefaultLitePullConsumer
litePullConsumer
=
new
DefaultLitePullConsumer
(
consumerGroup
+
System
.
currentTimeMillis
());
return
litePullConsumer
;
}
private
DefaultLitePullConsumer
createBroadcastLitePullConsumer
()
throws
Exception
{
DefaultLitePullConsumer
litePullConsumer
=
new
DefaultLitePullConsumer
(
consumerGroup
+
System
.
currentTimeMillis
());
litePullConsumer
.
setNamesrvAddr
(
"127.0.0.1:9876"
);
litePullConsumer
.
setMessageModel
(
MessageModel
.
BROADCASTING
);
litePullConsumer
.
subscribe
(
topic
,
"*"
);
litePullConsumer
.
start
();
initDefaultLitePullConsumer
(
litePullConsumer
);
return
litePullConsumer
;
}
private
DefaultLitePullConsumer
createNotStartLitePullConsumer
()
{
DefaultLitePullConsumer
litePullConsumer
=
new
DefaultLitePullConsumer
(
consumerGroup
+
System
.
currentTimeMillis
());
return
litePullConsumer
;
private
MessageQueue
createMessageQueue
()
{
MessageQueue
messageQueue
=
new
MessageQueue
();
messageQueue
.
setBrokerName
(
brokerName
);
messageQueue
.
setQueueId
(
0
);
messageQueue
.
setTopic
(
topic
);
return
messageQueue
;
}
private
PullResultExt
createPullResult
(
PullMessageRequestHeader
requestHeader
,
PullStatus
pullStatus
,
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录