@@ -84,6 +84,44 @@ def test_message_with_key(self):
8484 self .assertEqual (msg .value (), b"can you hear me?" )
8585 self .assertEqual (msg .key (), b"test_msg" )
8686
87+ def test_message_with_callback (self ):
88+ """Try writing a message into the Kafka broker, with a delivery callback.
89+ """
90+ topic = "test_message_with_callback"
91+ callback_called = False
92+ callback_error = False
93+
94+ def callback (err , msg ):
95+ nonlocal callback_called
96+ nonlocal callback_error
97+ callback_called = True
98+ callback_error = err is not None
99+
100+ ap = adc .producer
101+ with ap .Producer (ap .ProducerConfig (broker_urls = [self .kafka .address ],
102+ topic = topic , auth = self .kafka .auth )) as producer :
103+ producer .write ("message data" , delivery_callback = callback )
104+ producer .flush ()
105+ consumer = adc .consumer .Consumer (adc .consumer .ConsumerConfig (
106+ broker_urls = [self .kafka .address ],
107+ group_id = "test_consumer" ,
108+ auth = self .kafka .auth ,
109+ ))
110+ consumer .subscribe (topic )
111+ stream = consumer .stream ()
112+
113+ msg = next (stream )
114+ if msg .error () is not None :
115+ raise Exception (msg .error ())
116+ # give the producer's background thread time to notice the ack from the broker and
117+ # fire the callback
118+ producer ._producer .poll (0.1 )
119+
120+ self .assertEqual (msg .topic (), topic )
121+ self .assertEqual (msg .value (), b"message data" )
122+ self .assertEqual (callback_called , True )
123+ self .assertEqual (callback_error , False )
124+
87125 def test_reset_to_end (self ):
88126 # Write a few messages.
89127 topic = "test_reset_to_end"
@@ -486,7 +524,7 @@ def poll_for_kafka_broker_address(self, maxiter=20, sleep=timedelta(milliseconds
486524 """Block until the Docker daemon tells us the IP and Port of the Kafa broker.
487525
488526 Returns the ip and port as a string in the form "ip:port."
489- """
527+ """
490528 i = 0
491529 while (not self .query_kafka_broker_address ()) and i < maxiter :
492530 logger .info ("polling to wait for container to acquire port..." )
@@ -508,7 +546,7 @@ def query_kafka_broker_address(self):
508546 port = addrs [0 ]['HostPort' ]
509547 return f"{ ip } :{ port } "
510548
511- def poll_for_kafka_active (self , maxiter = 20 , sleep = timedelta (milliseconds = 500 )):
549+ def poll_for_kafka_active (self , maxiter = 40 , sleep = timedelta (milliseconds = 500 )):
512550 """Block until Kafka's network listener is accepting connections."""
513551 i = 0
514552 while (not self .query_kafka_active ()) and i < maxiter :
0 commit comments