|
18 | 18 | */
|
19 | 19 | package org.apache.pulsar.broker.service;
|
20 | 20 |
|
| 21 | +import static org.mockito.ArgumentMatchers.anyString; |
| 22 | +import static org.mockito.Mockito.doAnswer; |
21 | 23 | import static org.mockito.Mockito.mock;
|
22 | 24 | import static org.mockito.Mockito.when;
|
23 | 25 |
|
24 | 26 |
|
| 27 | +import java.nio.charset.StandardCharsets; |
| 28 | +import java.util.concurrent.atomic.AtomicInteger; |
25 | 29 | import org.apache.pulsar.broker.service.BrokerServiceException.ConsumerAssignException;
|
26 | 30 | import org.apache.pulsar.client.api.Range;
|
| 31 | +import org.apache.pulsar.client.impl.StickyKeyConsumerPredicate; |
| 32 | +import org.apache.pulsar.client.impl.StickyKeyConsumerPredicate.Predicate4ConsistentHashingStickyKeyConsumerSelector; |
27 | 33 | import org.testng.Assert;
|
28 | 34 | import org.testng.annotations.Test;
|
29 | 35 |
|
|
37 | 43 | @Test(groups = "broker")
|
38 | 44 | public class ConsistentHashingStickyKeyConsumerSelectorTest {
|
39 | 45 |
|
| 46 | + @Test |
| 47 | + public void testEventListener() throws Exception{ |
| 48 | + final ConsistentHashingStickyKeyConsumerSelector selector = |
| 49 | + new ConsistentHashingStickyKeyConsumerSelector(100); |
| 50 | + // consumer count: 0 --> 1 |
| 51 | + Consumer consumer1 = mock(Consumer.class); |
| 52 | + when(consumer1.consumerName()).thenReturn("c1"); |
| 53 | + AtomicInteger eventCount1 = new AtomicInteger(); |
| 54 | + doAnswer(invocation -> { |
| 55 | + String props = invocation.getArgument(0); |
| 56 | + StickyKeyConsumerPredicate predicate = StickyKeyConsumerPredicate.decode(props); |
| 57 | + Assert.assertTrue(predicate instanceof Predicate4ConsistentHashingStickyKeyConsumerSelector); |
| 58 | + eventCount1.incrementAndGet(); |
| 59 | + return null; |
| 60 | + }).when(consumer1).notifyActiveConsumerChange(anyString()); |
| 61 | + selector.addConsumer(consumer1); |
| 62 | + Assert.assertEquals(1, eventCount1.get()); |
| 63 | + // consumer count: 1 --> 2 |
| 64 | + Consumer consumer2 = mock(Consumer.class); |
| 65 | + when(consumer2.consumerName()).thenReturn("c2"); |
| 66 | + AtomicInteger eventCount2 = new AtomicInteger(); |
| 67 | + doAnswer(invocation -> { |
| 68 | + String props = invocation.getArgument(0); |
| 69 | + StickyKeyConsumerPredicate predicate = StickyKeyConsumerPredicate.decode(props); |
| 70 | + Assert.assertTrue(predicate instanceof Predicate4ConsistentHashingStickyKeyConsumerSelector); |
| 71 | + eventCount2.incrementAndGet(); |
| 72 | + return null; |
| 73 | + }).when(consumer2).notifyActiveConsumerChange(anyString()); |
| 74 | + selector.addConsumer(consumer2); |
| 75 | + Assert.assertEquals(1, eventCount2.get()); |
| 76 | + Assert.assertEquals(2, eventCount1.get()); |
| 77 | + // consumer count: 2 --> 3 |
| 78 | + Consumer consumer3 = mock(Consumer.class); |
| 79 | + when(consumer3.consumerName()).thenReturn("c3"); |
| 80 | + AtomicInteger eventCount3 = new AtomicInteger(); |
| 81 | + doAnswer(invocation -> { |
| 82 | + String props = invocation.getArgument(0); |
| 83 | + StickyKeyConsumerPredicate predicate = StickyKeyConsumerPredicate.decode(props); |
| 84 | + Assert.assertTrue(predicate instanceof Predicate4ConsistentHashingStickyKeyConsumerSelector); |
| 85 | + eventCount3.incrementAndGet(); |
| 86 | + return null; |
| 87 | + }).when(consumer3).notifyActiveConsumerChange(anyString()); |
| 88 | + selector.addConsumer(consumer3); |
| 89 | + Assert.assertEquals(eventCount1.get(), 3); |
| 90 | + Assert.assertEquals(eventCount2.get(), 2); |
| 91 | + Assert.assertEquals(eventCount3.get(), 1); |
| 92 | + // consumer count: 3 --> 2 |
| 93 | + selector.removeConsumer(consumer1); |
| 94 | + Assert.assertEquals(eventCount1.get(), 3); |
| 95 | + Assert.assertEquals(eventCount2.get(), 3); |
| 96 | + Assert.assertEquals(eventCount3.get(), 2); |
| 97 | + // consumer count: 2 --> 1 |
| 98 | + selector.removeConsumer(consumer2); |
| 99 | + Assert.assertEquals(eventCount1.get(), 3); |
| 100 | + Assert.assertEquals(eventCount2.get(), 3); |
| 101 | + Assert.assertEquals(eventCount3.get(), 3); |
| 102 | + // consumer count: 1 --> 0 |
| 103 | + selector.removeConsumer(consumer3); |
| 104 | + Assert.assertEquals(eventCount1.get(), 3); |
| 105 | + Assert.assertEquals(eventCount2.get(), 3); |
| 106 | + Assert.assertEquals(eventCount3.get(), 3); |
| 107 | + } |
| 108 | + |
| 109 | + @Test(dependsOnMethods = {"testConsumerSelect"}) |
| 110 | + public void testGenerateSpecialPredicate() throws Exception{ |
| 111 | + final ConsistentHashingStickyKeyConsumerSelector selector = new ConsistentHashingStickyKeyConsumerSelector(100); |
| 112 | + String key1 = "anyKey"; |
| 113 | + // one consumer |
| 114 | + Consumer consumer1 = mock(Consumer.class); |
| 115 | + when(consumer1.consumerName()).thenReturn("c1"); |
| 116 | + selector.addConsumer(consumer1); |
| 117 | + Assert.assertTrue(selector.generateSpecialPredicate(consumer1).test(key1)); |
| 118 | + // more consumer |
| 119 | + Consumer consumer2 = mock(Consumer.class); |
| 120 | + when(consumer2.consumerName()).thenReturn("c2"); |
| 121 | + selector.addConsumer(consumer2); |
| 122 | + Consumer consumer3 = mock(Consumer.class); |
| 123 | + when(consumer3.consumerName()).thenReturn("c3"); |
| 124 | + selector.addConsumer(consumer3); |
| 125 | + Consumer consumer4 = mock(Consumer.class); |
| 126 | + when(consumer4.consumerName()).thenReturn("c4"); |
| 127 | + selector.addConsumer(consumer4); |
| 128 | + Consumer consumer5 = mock(Consumer.class); |
| 129 | + when(consumer5.consumerName()).thenReturn("c5"); |
| 130 | + selector.addConsumer(consumer5); |
| 131 | + // do test |
| 132 | + final Map<Consumer, StickyKeyConsumerPredicate> predicateMapping = new HashMap<>(); |
| 133 | + predicateMapping.put(consumer1, |
| 134 | + StickyKeyConsumerPredicate.decode(selector.generateSpecialPredicate(consumer1).encode())); |
| 135 | + predicateMapping.put(consumer2, |
| 136 | + StickyKeyConsumerPredicate.decode(selector.generateSpecialPredicate(consumer2).encode())); |
| 137 | + predicateMapping.put(consumer3, |
| 138 | + StickyKeyConsumerPredicate.decode(selector.generateSpecialPredicate(consumer3).encode())); |
| 139 | + predicateMapping.put(consumer4, |
| 140 | + StickyKeyConsumerPredicate.decode(selector.generateSpecialPredicate(consumer4).encode())); |
| 141 | + predicateMapping.put(consumer5, |
| 142 | + StickyKeyConsumerPredicate.decode(selector.generateSpecialPredicate(consumer5).encode())); |
| 143 | + for (int i = 0; i < 100; i++){ |
| 144 | + String randomKey = UUID.randomUUID().toString(); |
| 145 | + Consumer selectedConsumer = selector.select(randomKey.getBytes(StandardCharsets.UTF_8)); |
| 146 | + for (Map.Entry<Consumer, StickyKeyConsumerPredicate> entry : predicateMapping.entrySet()){ |
| 147 | + if (selectedConsumer == entry.getKey()){ |
| 148 | + Assert.assertTrue(entry.getValue().test(randomKey)); |
| 149 | + } else { |
| 150 | + Assert.assertFalse(entry.getValue().test(randomKey)); |
| 151 | + } |
| 152 | + } |
| 153 | + } |
| 154 | + } |
| 155 | + |
40 | 156 | @Test
|
41 | 157 | public void testConsumerSelect() throws ConsumerAssignException {
|
42 | 158 |
|
|
0 commit comments