|
45 | 45 | import java.lang.reflect.Field;
|
46 | 46 | import java.util.ArrayList;
|
47 | 47 | import java.util.Collections;
|
| 48 | +import java.util.Iterator; |
48 | 49 | import java.util.List;
|
| 50 | +import java.util.Map; |
49 | 51 | import java.util.Queue;
|
50 | 52 | import java.util.Set;
|
51 | 53 | import java.util.concurrent.ConcurrentLinkedQueue;
|
@@ -171,6 +173,85 @@ public void testAddConsumerWhenClosed() throws Exception {
|
171 | 173 | assertTrue(persistentDispatcher.getSelector().getConsumerKeyHashRanges().isEmpty());
|
172 | 174 | }
|
173 | 175 |
|
| 176 | + @Test |
| 177 | + public void testSortRecentlyJoinedConsumersIfNeeded() throws Exception { |
| 178 | + PersistentStickyKeyDispatcherMultipleConsumers persistentDispatcher = |
| 179 | + new PersistentStickyKeyDispatcherMultipleConsumers( |
| 180 | + topicMock, cursorMock, subscriptionMock, configMock, |
| 181 | + new KeySharedMeta().setKeySharedMode(KeySharedMode.AUTO_SPLIT)); |
| 182 | + |
| 183 | + Consumer consumer0 = mock(Consumer.class); |
| 184 | + when(consumer0.consumerName()).thenReturn("c0-1"); |
| 185 | + Consumer consumer1 = mock(Consumer.class); |
| 186 | + when(consumer1.consumerName()).thenReturn("c1"); |
| 187 | + Consumer consumer2 = mock(Consumer.class); |
| 188 | + when(consumer2.consumerName()).thenReturn("c2"); |
| 189 | + Consumer consumer3 = mock(Consumer.class); |
| 190 | + when(consumer3.consumerName()).thenReturn("c3"); |
| 191 | + Consumer consumer4 = mock(Consumer.class); |
| 192 | + when(consumer4.consumerName()).thenReturn("c4"); |
| 193 | + Consumer consumer5 = mock(Consumer.class); |
| 194 | + when(consumer5.consumerName()).thenReturn("c5"); |
| 195 | + Consumer consumer6 = mock(Consumer.class); |
| 196 | + when(consumer6.consumerName()).thenReturn("c6"); |
| 197 | + |
| 198 | + when(cursorMock.getNumberOfEntriesSinceFirstNotAckedMessage()).thenReturn(100L); |
| 199 | + when(cursorMock.getMarkDeletedPosition()).thenReturn(PositionImpl.get(-1, -1)); |
| 200 | + |
| 201 | + when(cursorMock.getReadPosition()).thenReturn(PositionImpl.get(0, 0)); |
| 202 | + persistentDispatcher.addConsumer(consumer0).join(); |
| 203 | + |
| 204 | + when(cursorMock.getReadPosition()).thenReturn(PositionImpl.get(4, 1)); |
| 205 | + persistentDispatcher.addConsumer(consumer1).join(); |
| 206 | + |
| 207 | + when(cursorMock.getReadPosition()).thenReturn(PositionImpl.get(5, 2)); |
| 208 | + persistentDispatcher.addConsumer(consumer2).join(); |
| 209 | + |
| 210 | + when(cursorMock.getReadPosition()).thenReturn(PositionImpl.get(5, 1)); |
| 211 | + persistentDispatcher.addConsumer(consumer3).join(); |
| 212 | + |
| 213 | + when(cursorMock.getReadPosition()).thenReturn(PositionImpl.get(5, 3)); |
| 214 | + persistentDispatcher.addConsumer(consumer4).join(); |
| 215 | + |
| 216 | + when(cursorMock.getReadPosition()).thenReturn(PositionImpl.get(4, 2)); |
| 217 | + persistentDispatcher.addConsumer(consumer5).join(); |
| 218 | + |
| 219 | + when(cursorMock.getReadPosition()).thenReturn(PositionImpl.get(6, 1)); |
| 220 | + persistentDispatcher.addConsumer(consumer6).join(); |
| 221 | + |
| 222 | + assertEquals(persistentDispatcher.getRecentlyJoinedConsumers().size(), 6); |
| 223 | + |
| 224 | + Iterator<Map.Entry<Consumer, PositionImpl>> itr |
| 225 | + = persistentDispatcher.getRecentlyJoinedConsumers().entrySet().iterator(); |
| 226 | + |
| 227 | + Map.Entry<Consumer, PositionImpl> entry1 = itr.next(); |
| 228 | + assertEquals(entry1.getValue(), PositionImpl.get(4, 1)); |
| 229 | + assertEquals(entry1.getKey(), consumer1); |
| 230 | + |
| 231 | + Map.Entry<Consumer, PositionImpl> entry2 = itr.next(); |
| 232 | + assertEquals(entry2.getValue(), PositionImpl.get(4, 2)); |
| 233 | + assertEquals(entry2.getKey(), consumer5); |
| 234 | + |
| 235 | + Map.Entry<Consumer, PositionImpl> entry3 = itr.next(); |
| 236 | + assertEquals(entry3.getValue(), PositionImpl.get(5, 1)); |
| 237 | + assertEquals(entry3.getKey(), consumer3); |
| 238 | + |
| 239 | + Map.Entry<Consumer, PositionImpl> entry4 = itr.next(); |
| 240 | + assertEquals(entry4.getValue(), PositionImpl.get(5, 2)); |
| 241 | + assertEquals(entry4.getKey(), consumer2); |
| 242 | + |
| 243 | + Map.Entry<Consumer, PositionImpl> entry5 = itr.next(); |
| 244 | + assertEquals(entry5.getValue(), PositionImpl.get(5, 3)); |
| 245 | + assertEquals(entry5.getKey(), consumer4); |
| 246 | + |
| 247 | + Map.Entry<Consumer, PositionImpl> entry6 = itr.next(); |
| 248 | + assertEquals(entry6.getValue(), PositionImpl.get(6, 1)); |
| 249 | + assertEquals(entry6.getKey(), consumer6); |
| 250 | + |
| 251 | + // cleanup. |
| 252 | + persistentDispatcher.close(); |
| 253 | + } |
| 254 | + |
174 | 255 | @Test
|
175 | 256 | public void testSendMarkerMessage() {
|
176 | 257 | try {
|
|
0 commit comments