|
37 | 37 | import java.util.concurrent.CountDownLatch;
|
38 | 38 | import java.util.concurrent.ExecutionException;
|
39 | 39 | import java.util.concurrent.TimeUnit;
|
| 40 | +import java.util.concurrent.TimeoutException; |
40 | 41 | import java.util.concurrent.atomic.AtomicInteger;
|
41 | 42 | import java.util.concurrent.atomic.AtomicReference;
|
42 | 43 |
|
|
45 | 46 | import org.apache.kafka.clients.consumer.ConsumerRebalanceListener;
|
46 | 47 | import org.apache.kafka.clients.consumer.ConsumerRecord;
|
47 | 48 | import org.apache.kafka.clients.consumer.OffsetAndMetadata;
|
| 49 | +import org.apache.kafka.clients.producer.Callback; |
48 | 50 | import org.apache.kafka.clients.producer.Producer;
|
49 | 51 | import org.apache.kafka.clients.producer.ProducerConfig;
|
50 | 52 | import org.apache.kafka.clients.producer.ProducerRecord;
|
| 53 | +import org.apache.kafka.clients.producer.RecordMetadata; |
51 | 54 | import org.apache.kafka.common.TopicPartition;
|
52 | 55 | import org.apache.kafka.common.header.Headers;
|
53 | 56 | import org.apache.kafka.common.header.internals.RecordHeader;
|
@@ -676,6 +679,67 @@ public void withCustomHeaders() throws Exception {
|
676 | 679 | }
|
677 | 680 | }
|
678 | 681 |
|
| 682 | + @SuppressWarnings({ "rawtypes", "unchecked" }) |
| 683 | + @Test |
| 684 | + void nullDuration() throws Exception { |
| 685 | + ProducerFactory pf = mock(ProducerFactory.class); |
| 686 | + Producer producer = mock(Producer.class); |
| 687 | + willAnswer(invocation -> { |
| 688 | + Callback callback = invocation.getArgument(1); |
| 689 | + SettableListenableFuture<Object> future = new SettableListenableFuture<>(); |
| 690 | + future.set("done"); |
| 691 | + callback.onCompletion(new RecordMetadata(new TopicPartition("foo", 0), 0, 0, 0, null, 0, 0), null); |
| 692 | + return future; |
| 693 | + }).given(producer).send(any(), any()); |
| 694 | + given(pf.createProducer()).willReturn(producer); |
| 695 | + GenericMessageListenerContainer container = mock(GenericMessageListenerContainer.class); |
| 696 | + ContainerProperties properties = new ContainerProperties("two"); |
| 697 | + given(container.getContainerProperties()).willReturn(properties); |
| 698 | + ReplyingKafkaTemplate template = new ReplyingKafkaTemplate(pf, container); |
| 699 | + template.start(); |
| 700 | + Message<?> msg = MessageBuilder.withPayload("foo".getBytes()) |
| 701 | + .setHeader(KafkaHeaders.TOPIC, "foo") |
| 702 | + .build(); |
| 703 | + // was NPE here |
| 704 | + template.sendAndReceive(new ProducerRecord("foo", 0, "bar", "baz"), null).getSendFuture().get(); |
| 705 | + } |
| 706 | + |
| 707 | + @SuppressWarnings({ "rawtypes", "unchecked" }) |
| 708 | + @Test |
| 709 | + void requestTimeoutWithMessage() { |
| 710 | + ProducerFactory pf = mock(ProducerFactory.class); |
| 711 | + Producer producer = mock(Producer.class); |
| 712 | + willAnswer(invocation -> { |
| 713 | + return new SettableListenableFuture<>(); |
| 714 | + }).given(producer).send(any(), any()); |
| 715 | + given(pf.createProducer()).willReturn(producer); |
| 716 | + GenericMessageListenerContainer container = mock(GenericMessageListenerContainer.class); |
| 717 | + ContainerProperties properties = new ContainerProperties("two"); |
| 718 | + given(container.getContainerProperties()).willReturn(properties); |
| 719 | + ReplyingKafkaTemplate template = new ReplyingKafkaTemplate(pf, container); |
| 720 | + template.start(); |
| 721 | + Message<?> msg = MessageBuilder.withPayload("foo".getBytes()) |
| 722 | + .setHeader(KafkaHeaders.TOPIC, "foo") |
| 723 | + .build(); |
| 724 | + long t1 = System.currentTimeMillis(); |
| 725 | + RequestReplyTypedMessageFuture<String, String, Foo> future = template.sendAndReceive(msg, Duration.ofMillis(10), |
| 726 | + new ParameterizedTypeReference<Foo>() { |
| 727 | + }); |
| 728 | + try { |
| 729 | + future.get(10, TimeUnit.SECONDS); |
| 730 | + } |
| 731 | + catch (TimeoutException ex) { |
| 732 | + fail("get timed out"); |
| 733 | + } |
| 734 | + catch (InterruptedException e) { |
| 735 | + Thread.currentThread().interrupt(); |
| 736 | + fail("Interrupted"); |
| 737 | + } |
| 738 | + catch (ExecutionException e) { |
| 739 | + assertThat(System.currentTimeMillis() - t1).isLessThan(3000L); |
| 740 | + } |
| 741 | + } |
| 742 | + |
679 | 743 | @Configuration
|
680 | 744 | @EnableKafka
|
681 | 745 | public static class Config {
|
|
0 commit comments