18
18
package org .apache .rocketmq .broker .offset ;
19
19
20
20
import org .apache .rocketmq .broker .BrokerController ;
21
- import org .apache .rocketmq .broker .config .v1 .RocksDBLmqConsumerOffsetManager ;
21
+ import org .apache .rocketmq .broker .config .v1 .RocksDBConsumerOffsetManager ;
22
22
import org .apache .rocketmq .common .BrokerConfig ;
23
23
import org .apache .rocketmq .common .MixAll ;
24
24
import org .apache .rocketmq .store .config .MessageStoreConfig ;
28
28
29
29
import java .util .HashMap ;
30
30
import java .util .Map ;
31
- import java .util .concurrent .ConcurrentHashMap ;
32
31
33
32
import static org .junit .Assert .assertEquals ;
34
33
import static org .junit .Assert .assertNotNull ;
35
- import static org .junit .Assert .assertTrue ;
34
+ import static org .junit .Assert .assertNull ;
36
35
import static org .mockito .Mockito .when ;
37
36
38
37
public class RocksDBLmqConsumerOffsetManagerTest {
39
38
private static final String LMQ_GROUP = MixAll .LMQ_PREFIX + "FooBarGroup" ;
40
39
private static final String NON_LMQ_GROUP = "nonLmqGroup" ;
41
- private static final String TOPIC = "FooBarTopic" ;
40
+
41
+ private static final String LMQ_TOPIC = MixAll .LMQ_PREFIX + "FooBarTopic" ;
42
+ private static final String NON_LMQ_TOPIC = "FooBarTopic" ;
42
43
private static final int QUEUE_ID = 0 ;
43
44
private static final long OFFSET = 12345 ;
44
45
45
46
private BrokerController brokerController ;
46
47
47
- private RocksDBLmqConsumerOffsetManager offsetManager ;
48
+ private RocksDBConsumerOffsetManager offsetManager ;
48
49
49
50
@ Before
50
51
public void setUp () {
51
52
brokerController = Mockito .mock (BrokerController .class );
52
53
when (brokerController .getMessageStoreConfig ()).thenReturn (Mockito .mock (MessageStoreConfig .class ));
53
- when (brokerController .getBrokerConfig ()).thenReturn (Mockito . mock ( BrokerConfig . class ));
54
- offsetManager = new RocksDBLmqConsumerOffsetManager (brokerController );
54
+ when (brokerController .getBrokerConfig ()).thenReturn (new BrokerConfig ( ));
55
+ offsetManager = new RocksDBConsumerOffsetManager (brokerController );
55
56
}
56
57
57
- @ Test
58
- public void testQueryOffsetForLmq () {
59
- // Setup
60
- offsetManager .getLmqOffsetTable ().put (getKey (), OFFSET );
61
- // Execute
62
- long actualOffset = offsetManager .queryOffset (LMQ_GROUP , TOPIC , QUEUE_ID );
63
- // Verify
64
- assertEquals ("Offset should match the expected value." , OFFSET , actualOffset );
65
- }
66
58
67
59
@ Test
68
60
public void testQueryOffsetForNonLmq () {
69
- long actualOffset = offsetManager .queryOffset (NON_LMQ_GROUP , TOPIC , QUEUE_ID );
61
+ long actualOffset = offsetManager .queryOffset (NON_LMQ_GROUP , NON_LMQ_TOPIC , QUEUE_ID );
70
62
// Verify
71
63
assertEquals ("Offset should not be null." , -1 , actualOffset );
72
64
}
73
65
74
66
75
67
@ Test
76
68
public void testQueryOffsetForLmqGroupWithExistingOffset () {
77
- offsetManager .getLmqOffsetTable (). put ( getKey () , OFFSET );
69
+ offsetManager .commitOffset ( "127.0.0.1" , LMQ_GROUP , LMQ_TOPIC , QUEUE_ID , OFFSET );
78
70
79
71
// Act
80
- Map <Integer , Long > actualOffsets = offsetManager .queryOffset (LMQ_GROUP , TOPIC );
72
+ Map <Integer , Long > actualOffsets = offsetManager .queryOffset (LMQ_GROUP , LMQ_TOPIC );
81
73
82
74
// Assert
83
75
assertNotNull (actualOffsets );
@@ -89,23 +81,20 @@ public void testQueryOffsetForLmqGroupWithExistingOffset() {
89
81
public void testQueryOffsetForLmqGroupWithoutExistingOffset () {
90
82
// Act
91
83
Map <Integer , Long > actualOffsets = offsetManager .queryOffset (LMQ_GROUP , "nonExistingTopic" );
92
-
93
84
// Assert
94
- assertNotNull (actualOffsets );
95
- assertTrue ("The map should be empty for non-existing offsets" , actualOffsets .isEmpty ());
85
+ assertNull (actualOffsets );
96
86
}
97
87
98
88
@ Test
99
89
public void testQueryOffsetForNonLmqGroup () {
100
- when (brokerController .getBrokerConfig ().getConsumerOffsetUpdateVersionStep ()).thenReturn (1L );
101
90
// Arrange
102
91
Map <Integer , Long > mockOffsets = new HashMap <>();
103
92
mockOffsets .put (QUEUE_ID , OFFSET );
104
93
105
- offsetManager .commitOffset ("clientHost" , NON_LMQ_GROUP , TOPIC , QUEUE_ID , OFFSET );
94
+ offsetManager .commitOffset ("clientHost" , NON_LMQ_GROUP , NON_LMQ_TOPIC , QUEUE_ID , OFFSET );
106
95
107
96
// Act
108
- Map <Integer , Long > actualOffsets = offsetManager .queryOffset (NON_LMQ_GROUP , TOPIC );
97
+ Map <Integer , Long > actualOffsets = offsetManager .queryOffset (NON_LMQ_GROUP , NON_LMQ_TOPIC );
109
98
110
99
// Assert
111
100
assertNotNull (actualOffsets );
@@ -115,21 +104,13 @@ public void testQueryOffsetForNonLmqGroup() {
115
104
@ Test
116
105
public void testCommitOffsetForLmq () {
117
106
// Execute
118
- offsetManager .commitOffset ("clientHost" , LMQ_GROUP , TOPIC , QUEUE_ID , OFFSET );
107
+ offsetManager .commitOffset ("clientHost" , LMQ_GROUP , LMQ_TOPIC , QUEUE_ID , OFFSET );
119
108
// Verify
120
- Long expectedOffset = offsetManager .getLmqOffsetTable ().get (getKey () );
109
+ Long expectedOffset = offsetManager .getOffsetTable ().get (getLMQKey ()). get ( QUEUE_ID );
121
110
assertEquals ("Offset should be updated correctly." , OFFSET , expectedOffset .longValue ());
122
111
}
123
112
124
- @ Test
125
- public void testEncode () {
126
- offsetManager .setLmqOffsetTable (new ConcurrentHashMap <>(512 ));
127
- offsetManager .getLmqOffsetTable ().put (getKey (), OFFSET );
128
- String encodedData = offsetManager .encode ();
129
- assertTrue (encodedData .contains (String .valueOf (OFFSET )));
130
- }
131
-
132
- private String getKey () {
133
- return TOPIC + "@" + LMQ_GROUP ;
113
+ private String getLMQKey () {
114
+ return LMQ_TOPIC + "@" + LMQ_GROUP ;
134
115
}
135
116
}
0 commit comments