-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
ConsumerImplTest.java
204 lines (179 loc) · 8.25 KB
/
ConsumerImplTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/**
* 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.pulsar.client.impl;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import org.apache.pulsar.client.api.Consumer;
import org.apache.pulsar.client.api.Message;
import org.apache.pulsar.client.api.Messages;
import org.apache.pulsar.client.api.PulsarClientException;
import org.apache.pulsar.client.impl.conf.ClientConfigurationData;
import org.apache.pulsar.client.impl.conf.ConsumerConfigurationData;
import org.apache.pulsar.client.util.ExecutorProvider;
import org.awaitility.Awaitility;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
public class ConsumerImplTest {
private ExecutorProvider executorProvider;
private ExecutorService internalExecutor;
private ConsumerImpl<byte[]> consumer;
private ConsumerConfigurationData consumerConf;
@BeforeMethod(alwaysRun = true)
public void setUp() {
executorProvider = new ExecutorProvider(1, "ConsumerImplTest");
internalExecutor = Executors.newSingleThreadScheduledExecutor();
consumerConf = new ConsumerConfigurationData<>();
PulsarClientImpl client = ClientTestFixtures.createPulsarClientMock(executorProvider, internalExecutor);
ClientConfigurationData clientConf = client.getConfiguration();
clientConf.setOperationTimeoutMs(100);
clientConf.setStatsIntervalSeconds(0);
CompletableFuture<Consumer<ConsumerImpl>> subscribeFuture = new CompletableFuture<>();
String topic = "non-persistent://tenant/ns1/my-topic";
consumerConf.setSubscriptionName("test-sub");
consumer = ConsumerImpl.newConsumerImpl(client, topic, consumerConf,
executorProvider, -1, false, subscribeFuture, null, null, null,
true);
consumer.setState(HandlerState.State.Ready);
}
@AfterMethod(alwaysRun = true)
public void cleanup() {
if (executorProvider != null) {
executorProvider.shutdownNow();
executorProvider = null;
}
if (internalExecutor != null) {
internalExecutor.shutdownNow();
internalExecutor = null;
}
}
@Test(invocationTimeOut = 1000)
public void testNotifyPendingReceivedCallback_EmptyQueueNotThrowsException() {
consumer.notifyPendingReceivedCallback(null, null);
}
@Test(invocationTimeOut = 500)
public void testCorrectBackoffConfiguration() {
final Backoff backoff = consumer.getConnectionHandler().backoff;
ClientConfigurationData clientConfigurationData = new ClientConfigurationData();
Assert.assertEquals(backoff.getMax(), TimeUnit.NANOSECONDS.toMillis(clientConfigurationData.getMaxBackoffIntervalNanos()));
Assert.assertEquals(backoff.next(), TimeUnit.NANOSECONDS.toMillis(clientConfigurationData.getInitialBackoffIntervalNanos()));
}
@Test(invocationTimeOut = 1000)
public void testNotifyPendingReceivedCallback_CompleteWithException() {
CompletableFuture<Message<byte[]>> receiveFuture = new CompletableFuture<>();
consumer.pendingReceives.add(receiveFuture);
Exception exception = new PulsarClientException.InvalidMessageException("some random exception");
consumer.notifyPendingReceivedCallback(null, exception);
try {
receiveFuture.join();
} catch (CompletionException e) {
// Completion exception must be the same we provided at calling time
Assert.assertEquals(e.getCause(), exception);
}
Assert.assertTrue(receiveFuture.isCompletedExceptionally());
}
@Test(invocationTimeOut = 1000)
public void testNotifyPendingReceivedCallback_CompleteWithExceptionWhenMessageIsNull() {
CompletableFuture<Message<byte[]>> receiveFuture = new CompletableFuture<>();
consumer.pendingReceives.add(receiveFuture);
consumer.notifyPendingReceivedCallback(null, null);
try {
receiveFuture.join();
} catch (CompletionException e) {
Assert.assertEquals("received message can't be null", e.getCause().getMessage());
}
Assert.assertTrue(receiveFuture.isCompletedExceptionally());
}
@Test(invocationTimeOut = 1000)
public void testNotifyPendingReceivedCallback_InterceptorsWorksWithPrefetchDisabled() {
CompletableFuture<Message<byte[]>> receiveFuture = new CompletableFuture<>();
MessageImpl message = mock(MessageImpl.class);
ConsumerImpl<byte[]> spy = spy(consumer);
consumer.pendingReceives.add(receiveFuture);
consumerConf.setReceiverQueueSize(0);
doReturn(message).when(spy).beforeConsume(any());
spy.notifyPendingReceivedCallback(message, null);
Message<byte[]> receivedMessage = receiveFuture.join();
verify(spy, times(1)).beforeConsume(message);
Assert.assertTrue(receiveFuture.isDone());
Assert.assertFalse(receiveFuture.isCompletedExceptionally());
Assert.assertEquals(receivedMessage, message);
}
@Test(invocationTimeOut = 1000)
public void testNotifyPendingReceivedCallback_WorkNormally() {
CompletableFuture<Message<byte[]>> receiveFuture = new CompletableFuture<>();
MessageImpl message = mock(MessageImpl.class);
ConsumerImpl<byte[]> spy = spy(consumer);
consumer.pendingReceives.add(receiveFuture);
doReturn(message).when(spy).beforeConsume(any());
doNothing().when(spy).messageProcessed(message);
spy.notifyPendingReceivedCallback(message, null);
Message<byte[]> receivedMessage = receiveFuture.join();
verify(spy, times(1)).beforeConsume(message);
verify(spy, times(1)).messageProcessed(message);
Assert.assertTrue(receiveFuture.isDone());
Assert.assertFalse(receiveFuture.isCompletedExceptionally());
Assert.assertEquals(receivedMessage, message);
}
@Test
public void testReceiveAsyncCanBeCancelled() {
// given
CompletableFuture<Message<byte[]>> future = consumer.receiveAsync();
Awaitility.await().untilAsserted(() -> Assert.assertTrue(consumer.hasNextPendingReceive()));
// when
future.cancel(true);
// then
Assert.assertTrue(consumer.pendingReceives.isEmpty());
}
@Test
public void testBatchReceiveAsyncCanBeCancelled() {
// given
CompletableFuture<Messages<byte[]>> future = consumer.batchReceiveAsync();
Awaitility.await().untilAsserted(() -> Assert.assertTrue(consumer.hasPendingBatchReceive()));
// when
future.cancel(true);
// then
Assert.assertFalse(consumer.hasPendingBatchReceive());
}
@Test
public void testClose() {
Exception checkException = null;
try {
if (consumer != null) {
consumer.negativeAcknowledge(new MessageIdImpl(-1, -1, -1));
consumer.close();
}
} catch (Exception e) {
checkException = e;
}
Assert.assertNull(checkException);
}
}