11package com .helltractor .demo .messaging ;
22
3- import com .helltractor .demo .ConfluentKafkaContainerStandalone ;
43import com .helltractor .demo .KafkaApplication ;
4+ import com .helltractor .demo .container .ConfluentKafkaContainerCluster ;
55import com .helltractor .demo .message .TestMessage ;
66import com .helltractor .demo .util .IpUtil ;
7+ import org .awaitility .Awaitility ;
78import org .junit .jupiter .api .AfterEach ;
89import org .junit .jupiter .api .BeforeEach ;
910import org .junit .jupiter .api .Test ;
1011import org .slf4j .Logger ;
1112import org .slf4j .LoggerFactory ;
1213import org .springframework .beans .factory .annotation .Autowired ;
1314import org .springframework .boot .test .context .SpringBootTest ;
14- import org .springframework .context .annotation . Lazy ;
15- import org .springframework .test .annotation . DirtiesContext ;
15+ import org .springframework .test . context .DynamicPropertyRegistry ;
16+ import org .springframework .test .context . DynamicPropertySource ;
1617
18+ import java .time .Duration ;
1719import java .util .List ;
1820import java .util .concurrent .atomic .AtomicInteger ;
1921
2022import static org .junit .jupiter .api .Assertions .assertEquals ;
2123
2224@ SpringBootTest (classes = KafkaApplication .class )
23- @ DirtiesContext (classMode = DirtiesContext .ClassMode .AFTER_EACH_TEST_METHOD )
2425public class MessagingTest {
2526
2627 final static Logger logger = LoggerFactory .getLogger (MessagingTest .class );
2728
28- @ Lazy
29+ static class TestConsumer {
30+
31+ final AtomicInteger messageCount = new AtomicInteger ();
32+
33+ final long startTime = System .currentTimeMillis ();
34+
35+ void processMessages (List <TestMessage > messages ) {
36+ messageCount .addAndGet (messages .size ());
37+ long currentTime = System .currentTimeMillis ();
38+ logger .info ("Received {} messages, Total: {}, Elapsed time: {} ms" ,
39+ messages .size (), messageCount .get (), currentTime - startTime );
40+ }
41+
42+ int getTotalMessages () {
43+ return messageCount .get ();
44+ }
45+ }
46+
2947 @ Autowired
3048 MessagingFactory messagingFactory ;
3149
32- ConfluentKafkaContainerStandalone standalone ;
50+ ConfluentKafkaContainerCluster cluster ;
3351
3452 MessageProducer <TestMessage > processorOne ;
3553 MessageProducer <TestMessage > processorTwo ;
3654 MessageProducer <TestMessage > processorThree ;
3755
3856 @ BeforeEach
3957 void init () {
40- standalone = new ConfluentKafkaContainerStandalone ("7.4.0" );
58+ cluster = new ConfluentKafkaContainerCluster ("7.4.0" , 1 , 1 );
59+ cluster .start ();
4160 processorOne = messagingFactory .createMessageProducer (Messaging .Topic .TOPIC_ONE , TestMessage .class );
4261 processorTwo = messagingFactory .createMessageProducer (Messaging .Topic .TOPIC_TWO , TestMessage .class );
4362 processorThree = messagingFactory .createMessageProducer (Messaging .Topic .TOPIC_THREE , TestMessage .class );
4463 }
4564
4665 @ AfterEach
4766 void destroy () {
48- standalone .stop ();
49- }
50-
51- static class TestConsumer {
52-
53- final AtomicInteger messageCount = new AtomicInteger ();
54-
55- final long startTime = System .currentTimeMillis ();
56-
57- void processMessages (List <TestMessage > messages ) {
58- messageCount .addAndGet (messages .size ());
59- long currentTime = System .currentTimeMillis ();
60- logger .info ("Received {} messages, Total: {}, Elapsed time: {} ms" ,
61- messages .size (), messageCount .get (), currentTime - startTime );
62- }
63-
64- int getTotalMessages () {
65- return messageCount .get ();
66- }
67+ cluster .stop ();
6768 }
6869
6970 @ Test
@@ -82,7 +83,10 @@ void test() throws InterruptedException {
8283 messagingFactory .createBatchMessageListener (topic , groupId , testConsumer ::processMessages );
8384 }
8485
85- Thread .sleep (10000 );
86+ Awaitility .await ()
87+ .atMost (Duration .ofSeconds (10 ))
88+ .until (() -> testConsumer .getTotalMessages () == 300 );
89+
8690 assertEquals (300 , testConsumer .getTotalMessages ());
8791 }
8892}
0 commit comments