@@ -23,67 +23,97 @@ TEST(KafkaConsumer, BasicPoll)
2323
2424    KafkaTestUtility::CreateKafkaTopic (topic, 5 , 3 );
2525
26-     //  The auto-commit consumer
27-     kafka::clients::consumer::KafkaConsumer consumer (KafkaTestUtility::GetKafkaClientCommonConfig ());
28-     std::cout << " [" kafka::utility::getCurrentTime () << " ] " name () << "  started" 
26+     std::map<std::string, std::string> brokersState;
27+ 
28+     kafka::clients::Interceptors interceptors;
29+     interceptors.onBrokerStateChange ([&brokersState](int  id, const  std::string& proto, const  std::string& name, int  port, const  std::string& state) {
30+                                         const  std::string brokerDescription = (std::to_string (id) + "  - " " ://" " :" std::to_string (port));
31+                                         std::cout << " Broker[" " ] ==> " 
32+                                         if  (!name.empty () && name != " GroupCoordinator" 
33+                                         {
34+                                             brokersState[name + " :" std::to_string (port)] = state;
35+                                         }
36+                                      });
2937
30-     //  Subscribe topics
31-     consumer.subscribe ({topic},
32-                        [](kafka::clients::consumer::RebalanceEventType et, const  kafka::TopicPartitions& tps) {
33-                             if  (et == kafka::clients::consumer::RebalanceEventType::PartitionsAssigned) {
34-                                 //  assignment finished
35-                                 std::cout << " [" kafka::utility::getCurrentTime () << " ] assigned partitions: " kafka::toString (tps) << std::endl;
36-                             }
37-                        });
38-     EXPECT_FALSE (consumer.subscription ().empty ());
38+     {
39+         //  Config the consumer with interceptors
40+         kafka::clients::consumer::KafkaConsumer consumer (KafkaTestUtility::GetKafkaClientCommonConfig ()
41+                                                              .put (kafka::clients::Config::INTERCEPTORS, interceptors));
3942
40-     //  No message yet
41-     auto  records = KafkaTestUtility::ConsumeMessagesUntilTimeout (consumer, std::chrono::seconds (1 ));
42-     EXPECT_EQ (0 , records.size ());
43+         std::cout << " [" kafka::utility::getCurrentTime () << " ] " name () << "  started" 
4344
44-     //  Try to get the beginning offsets
45-     const  kafka::TopicPartition tp{topic, partition};
46-     std::cout << " [" kafka::utility::getCurrentTime () << " ] Consumer get the beginningOffset[" beginningOffsets ({tp})[tp] << " ]" 
45+         //  Subscribe topics
46+         consumer.subscribe ({topic},
47+                            [](kafka::clients::consumer::RebalanceEventType et, const  kafka::TopicPartitions& tps) {
48+                                 if  (et == kafka::clients::consumer::RebalanceEventType::PartitionsAssigned) {
49+                                     //  assignment finished
50+                                     std::cout << " [" kafka::utility::getCurrentTime () << " ] assigned partitions: " kafka::toString (tps) << std::endl;
51+                                 }
52+                            });
53+         EXPECT_FALSE (consumer.subscription ().empty ());
4754
48-     //  Prepare some messages to send
49-     const  std::vector<std::tuple<kafka::Headers, std::string, std::string>> messages = {
50-         {kafka::Headers{}, " key1" " value1" 
51-         {kafka::Headers{}, " key2" " value2" 
52-         {kafka::Headers{}, " key3" " value3" 
53-     };
55+         //  No message yet
56+         auto  records = KafkaTestUtility::ConsumeMessagesUntilTimeout (consumer, std::chrono::seconds (1 ));
57+         EXPECT_EQ (0 , records.size ());
5458
55-     //  Send the messages
56-     KafkaTestUtility::ProduceMessages (topic, partition, messages);
59+         //  Should be able to get all brokers' state
60+         EXPECT_EQ (KafkaTestUtility::GetNumberOfKafkaBrokers (), brokersState.size ());
61+         //  All brokers' state should be "UP"
62+         for  (const  auto & brokerState: brokersState)
63+         {
64+             EXPECT_EQ (" UP" second );
65+         }
5766
58-     //  Poll these messages 
59-     records =  KafkaTestUtility::ConsumeMessagesUntilTimeout (consumer) ;
60-     EXPECT_EQ (messages. size (), records. size ()) ;
67+          //  Try to get the beginning offsets 
68+          const  kafka::TopicPartition tp{topic, partition} ;
69+         std::cout <<  " [ "  <<  kafka::utility::getCurrentTime () <<  " ] Consumer get the beginningOffset[ "  << consumer. beginningOffsets ({tp})[tp] <<  " ] "  << std::endl; ;
6170
62-     //  Copyable ConsumerRecord
63-     {
64-         auto  recordsCopy = records;
65-         recordsCopy.clear ();
66-     }
71+         //  Prepare some messages to send
72+         const  std::vector<std::tuple<kafka::Headers, std::string, std::string>> messages = {
73+             {kafka::Headers{}, " key1" " value1" 
74+             {kafka::Headers{}, " key2" " value2" 
75+             {kafka::Headers{}, " key3" " value3" 
76+         };
6777
68-     //  Check messages
69-     std::size_t  rcvMsgCount = 0 ;
70-     for  (auto & record: records)
71-     {
72-         ASSERT_TRUE (rcvMsgCount < messages.size ());
78+         //  Send the messages
79+         KafkaTestUtility::ProduceMessages (topic, partition, messages);
7380
74-         EXPECT_EQ (topic, record.topic ());
75-         EXPECT_EQ (partition, record.partition ());
76-         EXPECT_EQ (0 , record.headers ().size ());
77-         EXPECT_EQ (std::get<1 >(messages[rcvMsgCount]).size (), record.key ().size ());
78-         EXPECT_EQ (0 , std::memcmp (std::get<1 >(messages[rcvMsgCount]).c_str (), record.key ().data (), record.key ().size ()));
79-         EXPECT_EQ (std::get<2 >(messages[rcvMsgCount]).size (), record.value ().size ());
80-         EXPECT_EQ (0 , std::memcmp (std::get<2 >(messages[rcvMsgCount]).c_str (), record.value ().data (), record.value ().size ()));
81+         //  Poll these messages
82+         records = KafkaTestUtility::ConsumeMessagesUntilTimeout (consumer);
83+         EXPECT_EQ (messages.size (), records.size ());
8184
82-         ++rcvMsgCount;
85+         //  Copyable ConsumerRecord
86+         {
87+             auto  recordsCopy = records;
88+             recordsCopy.clear ();
89+         }
90+ 
91+         //  Check messages
92+         std::size_t  rcvMsgCount = 0 ;
93+         for  (auto & record: records)
94+         {
95+             ASSERT_TRUE (rcvMsgCount < messages.size ());
96+ 
97+             EXPECT_EQ (topic, record.topic ());
98+             EXPECT_EQ (partition, record.partition ());
99+             EXPECT_EQ (0 , record.headers ().size ());
100+             EXPECT_EQ (std::get<1 >(messages[rcvMsgCount]).size (), record.key ().size ());
101+             EXPECT_EQ (0 , std::memcmp (std::get<1 >(messages[rcvMsgCount]).c_str (), record.key ().data (), record.key ().size ()));
102+             EXPECT_EQ (std::get<2 >(messages[rcvMsgCount]).size (), record.value ().size ());
103+             EXPECT_EQ (0 , std::memcmp (std::get<2 >(messages[rcvMsgCount]).c_str (), record.value ().data (), record.value ().size ()));
104+ 
105+             ++rcvMsgCount;
106+         }
107+ 
108+         //  Close the consumer
109+         consumer.close ();
83110    }
84111
85-     //  Close the consumer
86-     consumer.close ();
112+     //  All brokers' state should be "DOWN"
113+     for  (const  auto & brokerState: brokersState)
114+     {
115+         EXPECT_EQ (" DOWN" second );
116+     }
87117}
88118
89119TEST (KafkaConsumer, PollWithHeaders)
0 commit comments