@@ -1224,3 +1224,89 @@ def test_elect_leaders():
12241224 with pytest .raises (KafkaException ):
12251225 a .elect_leaders (correct_election_type , [correct_partitions ])\
12261226 .result (timeout = 1 )
1227+
1228+
1229+ @pytest .mark .skipif (libversion ()[1 ] < 0x000b0500 ,
1230+ reason = "AdminAPI requires librdkafka >= v0.11.5" )
1231+ def test_admin_callback_exception_no_system_error ():
1232+ """Test AdminClient callbacks exception handling with different exception types"""
1233+
1234+ # Test error_cb with different exception types
1235+ def error_cb_kafka_exception (error ):
1236+ raise KafkaException (KafkaError ._FAIL , "KafkaException from error_cb" )
1237+
1238+ def error_cb_value_error (error ):
1239+ raise ValueError ("ValueError from error_cb" )
1240+
1241+ def error_cb_runtime_error (error ):
1242+ raise RuntimeError ("RuntimeError from error_cb" )
1243+
1244+ # Test error_cb with KafkaException
1245+ admin = AdminClient ({
1246+ 'bootstrap.servers' : 'nonexistent-broker:9092' ,
1247+ 'socket.timeout.ms' : 100 ,
1248+ 'error_cb' : error_cb_kafka_exception
1249+ })
1250+
1251+ with pytest .raises (KafkaException ) as exc_info :
1252+ admin .poll (timeout = 0.2 )
1253+ assert "KafkaException from error_cb" in str (exc_info .value )
1254+
1255+ # Test error_cb with ValueError
1256+ admin = AdminClient ({
1257+ 'bootstrap.servers' : 'nonexistent-broker:9092' ,
1258+ 'socket.timeout.ms' : 100 ,
1259+ 'error_cb' : error_cb_value_error
1260+ })
1261+
1262+ with pytest .raises (ValueError ) as exc_info :
1263+ admin .poll (timeout = 0.2 )
1264+ assert "ValueError from error_cb" in str (exc_info .value )
1265+
1266+ # Test error_cb with RuntimeError
1267+ admin = AdminClient ({
1268+ 'bootstrap.servers' : 'nonexistent-broker:9092' ,
1269+ 'socket.timeout.ms' : 100 ,
1270+ 'error_cb' : error_cb_runtime_error
1271+ })
1272+
1273+ with pytest .raises (RuntimeError ) as exc_info :
1274+ admin .poll (timeout = 0.2 )
1275+ assert "RuntimeError from error_cb" in str (exc_info .value )
1276+
1277+
1278+ @pytest .mark .skipif (libversion ()[1 ] < 0x000b0500 ,
1279+ reason = "AdminAPI requires librdkafka >= v0.11.5" )
1280+ def test_admin_multiple_callbacks_different_error_types ():
1281+ """Test AdminClient with multiple callbacks configured with different error types to see which one gets triggered"""
1282+
1283+ callbacks_called = []
1284+
1285+ def error_cb_that_raises_runtime (error ):
1286+ callbacks_called .append ('error_cb_runtime' )
1287+ raise RuntimeError ("RuntimeError from error_cb" )
1288+
1289+ def stats_cb_that_raises_value (stats_json ):
1290+ callbacks_called .append ('stats_cb_value' )
1291+ raise ValueError ("ValueError from stats_cb" )
1292+
1293+ def throttle_cb_that_raises_kafka (throttle_event ):
1294+ callbacks_called .append ('throttle_cb_kafka' )
1295+ raise KafkaException (KafkaError ._FAIL , "KafkaException from throttle_cb" )
1296+
1297+ admin = AdminClient ({
1298+ 'bootstrap.servers' : 'nonexistent-broker:9092' ,
1299+ 'socket.timeout.ms' : 100 ,
1300+ 'statistics.interval.ms' : 100 , # Enable stats callback
1301+ 'error_cb' : error_cb_that_raises_runtime ,
1302+ 'stats_cb' : stats_cb_that_raises_value ,
1303+ 'throttle_cb' : throttle_cb_that_raises_kafka
1304+ })
1305+
1306+ # Test that error_cb callback raises an exception (it's triggered by connection failures)
1307+ with pytest .raises (RuntimeError ) as exc_info :
1308+ admin .poll (timeout = 0.2 )
1309+
1310+ # Verify that error_cb was called
1311+ assert len (callbacks_called ) > 0
1312+ assert 'error_cb_runtime' in callbacks_called
0 commit comments