@@ -3,9 +3,10 @@ use crate::utils::containers::KafkaContext;
33use crate :: utils:: logging:: init_test_logger;
44use crate :: utils:: producer:: create_base_producer;
55use 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 } ;
6+ use anyhow:: Context ;
7+ use rdkafka:: config:: FromClientConfig ;
8+ use rdkafka:: consumer:: BaseConsumer ;
9+ use rdkafka:: producer:: { BaseProducer , BaseRecord , Producer } ;
910use rdkafka:: util:: Timeout ;
1011use rdkafka:: { ClientConfig , Message } ;
1112use std:: time:: Duration ;
@@ -16,52 +17,46 @@ mod utils;
1617pub async fn test_basic_produce ( ) {
1718 init_test_logger ( ) ;
1819
19- let kafka_context_result = KafkaContext :: new ( ) . await ;
20- let Ok ( _kafka_context) = kafka_context_result else {
20+ // Get Kafka container context.
21+ let kafka_context_result = KafkaContext :: shared ( ) . await ;
22+ let Ok ( kafka_context) = kafka_context_result else {
2123 panic ! (
2224 "could not create kafka context: {}" ,
2325 kafka_context_result. unwrap_err( )
2426 ) ;
2527 } ;
28+ let test_topic_name = rand_test_topic ( "testing-topic" ) ;
2629
27- let bootstrap_servers_result = _kafka_context . bootstrap_servers ( ) . await ;
28- let Ok ( bootstrap_servers ) = bootstrap_servers_result else {
30+ let consumer_result = create_consumer ( & kafka_context . bootstrap_servers , & test_topic_name ) . await ;
31+ let Ok ( consumer ) = consumer_result else {
2932 panic ! (
30- "could not create bootstrap servers : {}" ,
31- bootstrap_servers_result . unwrap_err( )
33+ "could not create consumer : {}" ,
34+ consumer_result . unwrap_err( )
3235 ) ;
3336 } ;
34- let test_topic = rand_test_topic ( "testing-topic" ) ;
35-
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) ;
42- let Ok ( base_producer) = base_producer_result else {
37+ let create_producer_result = create_producer ( & kafka_context. bootstrap_servers ) . await ;
38+ let Ok ( base_producer) = create_producer_result else {
4339 panic ! (
44- "could not create based_producer : {}" ,
45- base_producer_result . unwrap_err( )
40+ "could not create base producer : {}" ,
41+ create_producer_result . unwrap_err( )
4642 ) ;
4743 } ;
4844
49- let record = BaseRecord :: to ( & test_topic ) // destination topic
45+ let record = BaseRecord :: to ( & test_topic_name ) // destination topic
5046 . key ( & [ 1 , 2 , 3 , 4 ] ) // message key
5147 . payload ( "content" ) ; // message payload
5248
5349 let send_result = base_producer. send ( record) ;
5450 if send_result. is_err ( ) {
5551 panic ! ( "could not produce record: {:?}" , send_result. unwrap_err( ) ) ;
5652 }
57-
5853 let flush_result = base_producer. flush ( Timeout :: After ( Duration :: from_secs ( 10 ) ) ) ;
5954 if let Err ( flush_error) = flush_result {
6055 panic ! ( "timed out waiting for producer flush: {}" , flush_error) ;
6156 }
6257
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 ) ;
58+ let Some ( next_message_result) = consumer . poll ( Duration :: from_secs ( 2 ) ) else {
59+ panic ! ( "there is no next message on the topic: {}" , test_topic_name ) ;
6560 } ;
6661 let Ok ( borrowed_next_message) = next_message_result else {
6762 panic ! (
@@ -82,13 +77,13 @@ pub async fn test_basic_produce() {
8277}
8378
8479async fn create_consumer (
85- bootstrap_servers : String ,
86- test_topic : String ,
80+ bootstrap_servers : & str ,
81+ test_topic : & str ,
8782) -> anyhow:: Result < BaseConsumer > {
8883 let mut consumer_client_config = ClientConfig :: default ( ) ;
8984 consumer_client_config. set ( "group.id" , rand_test_group ( ) ) ;
9085 consumer_client_config. set ( "client.id" , "rdkafka_integration_test_client" ) ;
91- consumer_client_config. set ( "bootstrap.servers" , & bootstrap_servers) ;
86+ consumer_client_config. set ( "bootstrap.servers" , bootstrap_servers) ;
9287 consumer_client_config. set ( "enable.partition.eof" , "false" ) ;
9388 consumer_client_config. set ( "session.timeout.ms" , "6000" ) ;
9489 consumer_client_config. set ( "enable.auto.commit" , "false" ) ;
@@ -104,5 +99,18 @@ async fn create_consumer(
10499 )
105100 } ;
106101
107- unimplemented ! ( "unimplemented" ) ;
102+ Ok ( base_consumer)
103+ }
104+
105+ async fn create_producer ( bootstrap_servers : & str ) -> anyhow:: Result < BaseProducer > {
106+ let mut producer_client_config = ClientConfig :: default ( ) ;
107+ producer_client_config. set ( "bootstrap.servers" , bootstrap_servers) ;
108+ let base_producer_result = create_base_producer ( & producer_client_config) ;
109+ let Ok ( base_producer) = base_producer_result else {
110+ panic ! (
111+ "could not create based_producer: {}" ,
112+ base_producer_result. unwrap_err( )
113+ ) ;
114+ } ;
115+ Ok ( base_producer)
108116}
0 commit comments