|
1 | 1 | package cn.leancloud.kafka.consumer.integration; |
2 | 2 |
|
3 | | -import cn.leancloud.kafka.consumer.LcKafkaConsumer; |
4 | 3 | import org.slf4j.Logger; |
5 | 4 | import org.slf4j.LoggerFactory; |
6 | 5 |
|
7 | 6 | import java.io.Closeable; |
8 | 7 | import java.io.IOException; |
9 | 8 | import java.time.Duration; |
10 | | -import java.util.ArrayList; |
11 | | -import java.util.Collections; |
12 | | -import java.util.List; |
13 | | -import java.util.concurrent.CompletableFuture; |
14 | | -import java.util.concurrent.ThreadLocalRandom; |
15 | | -import java.util.concurrent.atomic.AtomicBoolean; |
16 | | - |
17 | | -import static java.util.concurrent.TimeUnit.SECONDS; |
18 | | -import static org.awaitility.Awaitility.await; |
19 | 9 |
|
20 | 10 | public class TestRunner implements Closeable { |
21 | 11 | private static final Logger logger = LoggerFactory.getLogger(TestRunner.class); |
22 | 12 |
|
23 | 13 | private final TestingProducer producer; |
24 | 14 | private final ConsumerFactory factory; |
25 | | - private final String topic; |
26 | | - private final TestStatistics statistics; |
| 15 | + private final TestContext context; |
27 | 16 |
|
28 | | - TestRunner(ConsumerFactory factory) { |
29 | | - this.topic = "Testing"; |
| 17 | + TestRunner(String topic, ConsumerFactory factory) { |
30 | 18 | this.factory = factory; |
31 | 19 | this.producer = new TestingProducer(Duration.ofMillis(100), 2); |
32 | | - this.statistics = new TestStatistics(); |
33 | | - } |
34 | | - |
35 | | - void startTest() throws Exception { |
36 | | - runSingleConsumerTest(); |
37 | | - |
38 | | - runTwoConsumersInSameGroupTest(); |
39 | | - |
40 | | - runJoinLeaveGroupTest(); |
| 20 | + this.context = new TestContext(topic, producer, factory); |
41 | 21 | } |
42 | 22 |
|
43 | | - @Override |
44 | | - public void close() throws IOException { |
45 | | - producer.close(); |
46 | | - } |
47 | | - |
48 | | - private void runSingleConsumerTest() throws Exception { |
49 | | - logger.info("Run single consumer test."); |
50 | | - |
51 | | - final LcKafkaConsumer<Integer, String> client = factory.buildConsumer("cn/leancloud/kafka/consumer", statistics); |
52 | | - client.subscribe(Collections.singletonList(topic)); |
53 | | - final int totalSent = producer.startFixedDurationTest(topic, Duration.ofSeconds(10)).get(); |
| 23 | + void runTest(IntegrationTest test) throws Exception { |
| 24 | + logger.info("\n\n\n------------------------------------ Start {} with consumer: {} ------------------------------------\n", |
| 25 | + test.name(), factory.type()); |
54 | 26 |
|
| 27 | + final TestStatistics statistics = context.newStatistics(); |
55 | 28 | try { |
56 | | - waitTestDone(totalSent); |
57 | | - } finally { |
58 | | - client.close(); |
59 | | - } |
60 | | - |
61 | | - logger.info("Single consumer test finished. \n"); |
62 | | - } |
63 | | - |
64 | | - private void runTwoConsumersInSameGroupTest() throws Exception { |
65 | | - logger.info("Run two consumers in same group test."); |
66 | | - |
67 | | - final LcKafkaConsumer<Integer, String> client = factory.buildConsumer("consumer-1", statistics); |
68 | | - final LcKafkaConsumer<Integer, String> client2 = factory.buildConsumer("consumer-2", statistics); |
69 | | - client.subscribe(Collections.singletonList(topic)); |
70 | | - client2.subscribe(Collections.singletonList(topic)); |
71 | | - final int totalSent = producer.startFixedDurationTest(topic, Duration.ofSeconds(10)).get(); |
72 | | - |
73 | | - try { |
74 | | - waitTestDone(totalSent); |
75 | | - } finally { |
76 | | - client.close(); |
77 | | - client2.close(); |
78 | | - } |
| 29 | + test.runTest(context, statistics); |
79 | 30 |
|
80 | | - logger.info("Two consumers in same group test finished. \n"); |
81 | | - } |
82 | | - |
83 | | - private void runJoinLeaveGroupTest() throws Exception { |
84 | | - logger.info("Run join leave group test."); |
85 | | - |
86 | | - final AtomicBoolean stopProducer = new AtomicBoolean(); |
87 | | - final CompletableFuture<Integer> producerSentCountFuture = producer.startNonStopTest(topic, stopProducer); |
88 | | - final LcKafkaConsumer<Integer, String> lingerConsumer = factory.buildConsumer("linger-consumer", statistics); |
89 | | - lingerConsumer.subscribe(Collections.singletonList(topic)); |
90 | | - |
91 | | - List<LcKafkaConsumer<Integer, String>> postJoinConsumers = new ArrayList<>(); |
92 | | - for (int i = 0; i < 5; ++i) { |
93 | | - final LcKafkaConsumer<Integer, String> consumer = factory.buildConsumer("consumer-" + i, statistics); |
94 | | - consumer.subscribe(Collections.singletonList(topic)); |
95 | | - postJoinConsumers.add(consumer); |
96 | | - Thread.sleep(ThreadLocalRandom.current().nextInt(1000, 5000)); |
97 | | - } |
98 | | - |
99 | | - for (LcKafkaConsumer<Integer, String> consumer : postJoinConsumers) { |
100 | | - consumer.close(); |
101 | | - Thread.sleep(ThreadLocalRandom.current().nextInt(1000, 5000)); |
102 | | - } |
103 | | - |
104 | | - stopProducer.set(true); |
105 | | - int totalSent = producerSentCountFuture.get(); |
106 | | - |
107 | | - try { |
108 | | - waitTestDone(totalSent); |
| 31 | + logger.info("{} test finished. sent: {}, received: {}, duplicates: {}", |
| 32 | + test.name(), statistics.getTotalSentCount(), |
| 33 | + statistics.getReceiveRecordsCount(), statistics.getDuplicateRecordsCount()); |
| 34 | + } catch (Exception ex) { |
| 35 | + logger.error("{} test got unexpected exception. sent: {}, received: {}, duplicates: {}", test.name(), |
| 36 | + statistics.getTotalSentCount(), statistics.getReceiveRecordsCount(), statistics.getDuplicateRecordsCount(), ex); |
| 37 | + throw ex; |
109 | 38 | } finally { |
110 | | - lingerConsumer.close(); |
| 39 | + statistics.clear(); |
111 | 40 | } |
112 | 41 |
|
113 | | - logger.info("Join leave group test finished. \n"); |
| 42 | + logger.info("------------------------------------ {} with consumer: {} finished ------------------------------------\n", |
| 43 | + test.name(), factory.type()); |
114 | 44 | } |
115 | 45 |
|
116 | | - private void waitTestDone(int totalSent) { |
117 | | - try { |
118 | | - await().atMost(10, SECONDS) |
119 | | - .pollInterval(1, SECONDS) |
120 | | - .until(() -> statistics.getReceiveRecordsCount() >= totalSent); |
121 | | - |
122 | | - logger.info("Integration test finished. sent: {}, received: {}, duplicates: {}", |
123 | | - totalSent, statistics.getReceiveRecordsCount(), statistics.getDuplicateRecordsCount()); |
124 | | - } catch (Exception ex) { |
125 | | - logger.error("Integration test got unexpected exception. sent: {}, received: {}, duplicates: {}", |
126 | | - totalSent, statistics.getReceiveRecordsCount(), statistics.getDuplicateRecordsCount(), ex); |
127 | | - } finally { |
128 | | - statistics.clear(); |
129 | | - } |
| 46 | + @Override |
| 47 | + public void close() throws IOException { |
| 48 | + producer.close(); |
130 | 49 | } |
131 | 50 | } |
0 commit comments