|
30 | 30 | import com.salesforce.kafka.test.listeners.SaslPlainListener;
|
31 | 31 | import com.salesforce.kafka.test.listeners.SaslSslListener;
|
32 | 32 | import com.salesforce.kafka.test.listeners.SslListener;
|
| 33 | +import org.apache.curator.test.InstanceSpec; |
33 | 34 | import org.apache.kafka.clients.admin.TopicDescription;
|
34 | 35 | import org.apache.kafka.clients.consumer.ConsumerRecord;
|
35 | 36 | import org.apache.kafka.common.Node;
|
|
40 | 41 | import org.junit.jupiter.params.provider.Arguments;
|
41 | 42 | import org.junit.jupiter.params.provider.MethodSource;
|
42 | 43 |
|
| 44 | +import java.util.ArrayList; |
43 | 45 | import java.util.Arrays;
|
44 | 46 | import java.util.Collection;
|
45 | 47 | import java.util.Collections;
|
@@ -459,6 +461,88 @@ private static Stream<Arguments> provideListeners() {
|
459 | 461 | );
|
460 | 462 | }
|
461 | 463 |
|
| 464 | + /** |
| 465 | + * Test a cluster instance with listeners on specified ports. |
| 466 | + */ |
| 467 | + @Test |
| 468 | + void testListenerWithSpecificPort() throws Exception { |
| 469 | + // Explicitly define our port |
| 470 | + final int exportedPort1 = InstanceSpec.getRandomPort(); |
| 471 | + final int exportedPort2 = InstanceSpec.getRandomPort(); |
| 472 | + |
| 473 | + // Create default plain listener |
| 474 | + final BrokerListener plainListener = new PlainListener() |
| 475 | + .onPorts(exportedPort1, exportedPort2); |
| 476 | + final List<BrokerListener> listeners = Collections.singletonList(plainListener); |
| 477 | + |
| 478 | + final String topicName = "TestTopic-" + System.currentTimeMillis(); |
| 479 | + final int expectedMsgCount = 2; |
| 480 | + final int numberOfBrokers = 2; |
| 481 | + |
| 482 | + // Speed up shutdown in our tests |
| 483 | + final Properties overrideProperties = getDefaultBrokerOverrideProperties(); |
| 484 | + |
| 485 | + // Create our test server instance |
| 486 | + try (final KafkaTestCluster kafkaTestCluster = new KafkaTestCluster(numberOfBrokers, overrideProperties, listeners)) { |
| 487 | + // Start broker |
| 488 | + kafkaTestCluster.start(); |
| 489 | + |
| 490 | + // Validate connect string is as expected. |
| 491 | + final String connectString = kafkaTestCluster.getKafkaConnectString(); |
| 492 | + final String expectedConnectString = "PLAINTEXT://localhost:" + exportedPort1 + ",PLAINTEXT://localhost:" + exportedPort2; |
| 493 | + Assertions.assertEquals(expectedConnectString, connectString, "Should be using our specified ports"); |
| 494 | + |
| 495 | + // Create KafkaTestUtils |
| 496 | + final KafkaTestUtils kafkaTestUtils = new KafkaTestUtils(kafkaTestCluster); |
| 497 | + |
| 498 | + // Create topic |
| 499 | + kafkaTestUtils.createTopic(topicName, 1, (short) numberOfBrokers); |
| 500 | + |
| 501 | + // Publish 2 messages into topic |
| 502 | + kafkaTestUtils.produceRecords(expectedMsgCount, topicName, 0); |
| 503 | + |
| 504 | + // Sanity test - Consume the messages back out before shutting down broker. |
| 505 | + final List<ConsumerRecord<byte[], byte[]>> records = kafkaTestUtils.consumeAllRecordsFromTopic(topicName); |
| 506 | + Assertions.assertNotNull(records); |
| 507 | + Assertions.assertEquals(expectedMsgCount, records.size(), "Should have found 2 records."); |
| 508 | + } |
| 509 | + } |
| 510 | + |
| 511 | + /** |
| 512 | + * Test a cluster instance with listeners on specified ports, where a port is duplicated. |
| 513 | + */ |
| 514 | + @Test |
| 515 | + void testListenerWithSpecificPortRepeated() throws Exception { |
| 516 | + // Explicitly define our port |
| 517 | + final int port1 = InstanceSpec.getRandomPort(); |
| 518 | + final int port2 = InstanceSpec.getRandomPort(); |
| 519 | + final int port3 = InstanceSpec.getRandomPort(); |
| 520 | + |
| 521 | + // Create plain listeners using the same port. |
| 522 | + final BrokerListener plainListener1 = new PlainListener() |
| 523 | + .onPorts(port1, port2); |
| 524 | + |
| 525 | + final BrokerListener plainListener2 = new PlainListener() |
| 526 | + .onPorts(port3, port1); |
| 527 | + |
| 528 | + final List<BrokerListener> listeners = new ArrayList<>(); |
| 529 | + listeners.add(plainListener1); |
| 530 | + listeners.add(plainListener2); |
| 531 | + |
| 532 | + // Define how many brokers. |
| 533 | + final int numberOfBrokers = 2; |
| 534 | + |
| 535 | + // Speed up shutdown in our tests |
| 536 | + final Properties overrideProperties = getDefaultBrokerOverrideProperties(); |
| 537 | + |
| 538 | + // Create our test server instance |
| 539 | + try (final KafkaTestCluster kafkaTestCluster = new KafkaTestCluster(numberOfBrokers, overrideProperties, listeners)) { |
| 540 | + |
| 541 | + // Start broker, this should throw an exception |
| 542 | + Assertions.assertThrows(RuntimeException.class, kafkaTestCluster::start); |
| 543 | + } |
| 544 | + } |
| 545 | + |
462 | 546 | private Properties getDefaultBrokerOverrideProperties() {
|
463 | 547 | // Speed up shutdown in our tests
|
464 | 548 | final Properties overrideProperties = new Properties();
|
|
0 commit comments