1- use rdkafka :: ClientConfig ;
1+ use crate :: utils :: consumer ;
22use crate :: utils:: containers:: KafkaContext ;
33use crate :: utils:: logging:: init_test_logger;
44use crate :: utils:: producer:: create_base_producer;
5- use crate :: utils:: rand:: rand_test_topic;
6- use rdkafka:: producer:: BaseRecord ;
5+ use crate :: utils:: rand:: { rand_test_group, rand_test_topic} ;
6+ use rdkafka:: consumer:: { BaseConsumer , Consumer } ;
7+ use rdkafka:: error:: KafkaError ;
8+ use rdkafka:: producer:: { BaseRecord , Producer } ;
9+ use rdkafka:: util:: Timeout ;
10+ use rdkafka:: { ClientConfig , Message } ;
11+ use std:: time:: Duration ;
712
813mod utils;
914
@@ -21,27 +26,83 @@ pub async fn test_basic_produce() {
2126
2227 let bootstrap_servers_result = _kafka_context. bootstrap_servers ( ) . await ;
2328 let Ok ( bootstrap_servers) = bootstrap_servers_result else {
24- panic ! ( "could not create bootstrap servers: {}" , bootstrap_servers_result. unwrap_err( ) ) ;
29+ panic ! (
30+ "could not create bootstrap servers: {}" ,
31+ bootstrap_servers_result. unwrap_err( )
32+ ) ;
2533 } ;
26- let mut client_config = ClientConfig :: default ( ) ;
27- client_config. set ( "bootstrap.servers" , bootstrap_servers) ;
34+ let test_topic = rand_test_topic ( "testing-topic" ) ;
2835
29- let base_producer_result = create_base_producer ( & client_config) ;
36+ create_consumer ( bootstrap_servers, test_topic) ;
37+
38+ let mut producer_client_config = ClientConfig :: default ( ) ;
39+ producer_client_config. set ( "bootstrap.servers" , & bootstrap_servers) ;
40+
41+ let base_producer_result = create_base_producer ( & producer_client_config) ;
3042 let Ok ( base_producer) = base_producer_result else {
3143 panic ! (
3244 "could not create based_producer: {}" ,
3345 base_producer_result. unwrap_err( )
3446 ) ;
3547 } ;
3648
37- let test_topic = rand_test_topic ( "testing-topic" ) ;
3849 let record = BaseRecord :: to ( & test_topic) // destination topic
3950 . key ( & [ 1 , 2 , 3 , 4 ] ) // message key
40- . payload ( "content" ) // message payload
41- . partition ( 5 ) ;
51+ . payload ( "content" ) ; // message payload
4252
4353 let send_result = base_producer. send ( record) ;
4454 if send_result. is_err ( ) {
4555 panic ! ( "could not produce record: {:?}" , send_result. unwrap_err( ) ) ;
4656 }
57+
58+ let flush_result = base_producer. flush ( Timeout :: After ( Duration :: from_secs ( 10 ) ) ) ;
59+ if let Err ( flush_error) = flush_result {
60+ panic ! ( "timed out waiting for producer flush: {}" , flush_error) ;
61+ }
62+
63+ let Some ( next_message_result) = base_consumer. poll ( Duration :: from_secs ( 2 ) ) else {
64+ panic ! ( "there is no next message on the topic: {}" , test_topic) ;
65+ } ;
66+ let Ok ( borrowed_next_message) = next_message_result else {
67+ panic ! (
68+ "could not get next message from based_consumer: {}" ,
69+ next_message_result. unwrap_err( )
70+ ) ;
71+ } ;
72+ let owned_next_message = borrowed_next_message. detach ( ) ;
73+ let Some ( message_payload) = owned_next_message. payload ( ) else {
74+ panic ! ( "message payload is empty" ) ;
75+ } ;
76+ let message_string_result = String :: from_utf8 ( message_payload. to_vec ( ) ) ;
77+ let Ok ( message_string) = message_string_result else {
78+ panic ! ( "message payload is not valid UTF-8" ) ;
79+ } ;
80+
81+ assert ! ( message_string. contains( "content" ) ) ;
82+ }
83+
84+ async fn create_consumer (
85+ bootstrap_servers : String ,
86+ test_topic : String ,
87+ ) -> anyhow:: Result < BaseConsumer > {
88+ let mut consumer_client_config = ClientConfig :: default ( ) ;
89+ consumer_client_config. set ( "group.id" , rand_test_group ( ) ) ;
90+ consumer_client_config. set ( "client.id" , "rdkafka_integration_test_client" ) ;
91+ consumer_client_config. set ( "bootstrap.servers" , & bootstrap_servers) ;
92+ consumer_client_config. set ( "enable.partition.eof" , "false" ) ;
93+ consumer_client_config. set ( "session.timeout.ms" , "6000" ) ;
94+ consumer_client_config. set ( "enable.auto.commit" , "false" ) ;
95+ consumer_client_config. set ( "debug" , "all" ) ;
96+ consumer_client_config. set ( "auto.offset.reset" , "earliest" ) ;
97+
98+ let base_consumer_result =
99+ consumer:: create_subscribed_base ( consumer_client_config, & [ & test_topic] ) . await ;
100+ let Ok ( base_consumer) = base_consumer_result else {
101+ panic ! (
102+ "could not create base consumer: {}" ,
103+ base_consumer_result. unwrap_err( )
104+ )
105+ } ;
106+
107+ unimplemented ! ( "unimplemented" ) ;
47108}
0 commit comments