|
21 | 21 | import com.google.common.collect.ImmutableMap; |
22 | 22 | import java.time.Duration; |
23 | 23 | import java.util.Collections; |
| 24 | +import java.util.Arrays; |
24 | 25 | import java.lang.reflect.Field; |
| 26 | +import java.util.concurrent.ExecutionException; |
| 27 | +import java.util.concurrent.TimeoutException; |
25 | 28 | import org.apache.kafka.clients.consumer.Consumer; |
26 | 29 | import org.apache.kafka.clients.consumer.ConsumerConfig; |
27 | 30 | import org.apache.kafka.clients.consumer.ConsumerRecord; |
| 31 | +import org.apache.kafka.clients.consumer.ConsumerRecords; |
| 32 | +import org.apache.kafka.common.TopicPartition; |
28 | 33 | import org.apache.kafka.common.security.auth.SecurityProtocol; |
29 | 34 | import org.apache.pulsar.client.api.Schema; |
| 35 | +import org.apache.pulsar.functions.api.Record; |
30 | 36 | import org.apache.pulsar.io.core.SourceContext; |
31 | 37 | import org.apache.pulsar.io.kafka.KafkaAbstractSource; |
32 | 38 | import org.apache.pulsar.io.kafka.KafkaSourceConfig; |
|
46 | 52 | import static org.testng.Assert.assertFalse; |
47 | 53 | import static org.testng.Assert.assertNotNull; |
48 | 54 | import static org.testng.Assert.assertNull; |
| 55 | +import static org.testng.Assert.assertTrue; |
49 | 56 | import static org.testng.Assert.expectThrows; |
50 | 57 | import static org.testng.Assert.fail; |
51 | 58 |
|
@@ -218,6 +225,88 @@ public final void throwExceptionByPoll() throws Exception { |
218 | 225 | source.read(); |
219 | 226 | } |
220 | 227 |
|
| 228 | + @Test |
| 229 | + public final void throwExceptionBySendFail() throws Exception { |
| 230 | + KafkaAbstractSource source = new DummySource(); |
| 231 | + |
| 232 | + KafkaSourceConfig kafkaSourceConfig = new KafkaSourceConfig(); |
| 233 | + kafkaSourceConfig.setTopic("test-topic"); |
| 234 | + kafkaSourceConfig.setAutoCommitEnabled(false); |
| 235 | + Field kafkaSourceConfigField = KafkaAbstractSource.class.getDeclaredField("kafkaSourceConfig"); |
| 236 | + kafkaSourceConfigField.setAccessible(true); |
| 237 | + kafkaSourceConfigField.set(source, kafkaSourceConfig); |
| 238 | + |
| 239 | + Field defaultMaxPollIntervalMsField = KafkaAbstractSource.class.getDeclaredField("maxPollIntervalMs"); |
| 240 | + defaultMaxPollIntervalMsField.setAccessible(true); |
| 241 | + defaultMaxPollIntervalMsField.set(source, 300000); |
| 242 | + |
| 243 | + Consumer consumer = mock(Consumer.class); |
| 244 | + ConsumerRecord<String, byte[]> consumerRecord = new ConsumerRecord<>("topic", 0, 0, |
| 245 | + "t-key", "t-value".getBytes(StandardCharsets.UTF_8)); |
| 246 | + ConsumerRecords<String, byte[]> consumerRecords = new ConsumerRecords<>(Collections.singletonMap( |
| 247 | + new TopicPartition("topic", 0), |
| 248 | + Arrays.asList(consumerRecord))); |
| 249 | + Mockito.doReturn(consumerRecords).when(consumer).poll(Mockito.any(Duration.class)); |
| 250 | + |
| 251 | + Field consumerField = KafkaAbstractSource.class.getDeclaredField("consumer"); |
| 252 | + consumerField.setAccessible(true); |
| 253 | + consumerField.set(source, consumer); |
| 254 | + source.start(); |
| 255 | + |
| 256 | + // Mock send message fail |
| 257 | + Record record = source.read(); |
| 258 | + record.fail(); |
| 259 | + |
| 260 | + // read again will throw RuntimeException. |
| 261 | + try { |
| 262 | + source.read(); |
| 263 | + fail("Should throw exception"); |
| 264 | + } catch (ExecutionException e) { |
| 265 | + assertTrue(e.getCause() instanceof RuntimeException); |
| 266 | + assertTrue(e.getCause().getMessage().contains("Failed to process record with kafka topic")); |
| 267 | + } |
| 268 | + } |
| 269 | + |
| 270 | + @Test |
| 271 | + public final void throwExceptionBySendTimeOut() throws Exception { |
| 272 | + KafkaAbstractSource source = new DummySource(); |
| 273 | + |
| 274 | + KafkaSourceConfig kafkaSourceConfig = new KafkaSourceConfig(); |
| 275 | + kafkaSourceConfig.setTopic("test-topic"); |
| 276 | + kafkaSourceConfig.setAutoCommitEnabled(false); |
| 277 | + Field kafkaSourceConfigField = KafkaAbstractSource.class.getDeclaredField("kafkaSourceConfig"); |
| 278 | + kafkaSourceConfigField.setAccessible(true); |
| 279 | + kafkaSourceConfigField.set(source, kafkaSourceConfig); |
| 280 | + |
| 281 | + Field defaultMaxPollIntervalMsField = KafkaAbstractSource.class.getDeclaredField("maxPollIntervalMs"); |
| 282 | + defaultMaxPollIntervalMsField.setAccessible(true); |
| 283 | + defaultMaxPollIntervalMsField.set(source, 1); |
| 284 | + |
| 285 | + Consumer consumer = mock(Consumer.class); |
| 286 | + ConsumerRecord<String, byte[]> consumerRecord = new ConsumerRecord<>("topic", 0, 0, |
| 287 | + "t-key", "t-value".getBytes(StandardCharsets.UTF_8)); |
| 288 | + ConsumerRecords<String, byte[]> consumerRecords = new ConsumerRecords<>(Collections.singletonMap( |
| 289 | + new TopicPartition("topic", 0), |
| 290 | + Arrays.asList(consumerRecord))); |
| 291 | + Mockito.doReturn(consumerRecords).when(consumer).poll(Mockito.any(Duration.class)); |
| 292 | + |
| 293 | + Field consumerField = KafkaAbstractSource.class.getDeclaredField("consumer"); |
| 294 | + consumerField.setAccessible(true); |
| 295 | + consumerField.set(source, consumer); |
| 296 | + source.start(); |
| 297 | + |
| 298 | + // Mock send message fail, just read do noting. |
| 299 | + source.read(); |
| 300 | + |
| 301 | + // read again will throw TimeOutException. |
| 302 | + try { |
| 303 | + source.read(); |
| 304 | + fail("Should throw exception"); |
| 305 | + } catch (Exception e) { |
| 306 | + assertTrue(e instanceof TimeoutException); |
| 307 | + } |
| 308 | + } |
| 309 | + |
221 | 310 | private File getFile(String name) { |
222 | 311 | ClassLoader classLoader = getClass().getClassLoader(); |
223 | 312 | return new File(classLoader.getResource(name).getFile()); |
|
0 commit comments