@@ -24,49 +24,41 @@ def test_version():
24
24
assert confluent_kafka .version ()[0 ] == confluent_kafka .__version__
25
25
26
26
27
- # global variable for error_cb call back function
28
- seen_error_cb = False
29
-
30
-
31
27
def test_error_cb ():
32
28
""" Tests error_cb. """
29
+ seen_error_cb = False
33
30
34
31
def error_cb (error_msg ):
35
- global seen_error_cb
32
+ nonlocal seen_error_cb
36
33
seen_error_cb = True
37
34
acceptable_error_codes = (confluent_kafka .KafkaError ._TRANSPORT , confluent_kafka .KafkaError ._ALL_BROKERS_DOWN )
38
35
assert error_msg .code () in acceptable_error_codes
39
36
40
37
conf = {'bootstrap.servers' : 'localhost:65531' , # Purposely cause connection refused error
41
38
'group.id' : 'test' ,
42
- 'socket.timeout.ms' : '100' ,
43
39
'session.timeout.ms' : 1000 , # Avoid close() blocking too long
44
40
'error_cb' : error_cb
45
41
}
46
42
47
43
kc = confluent_kafka .Consumer (** conf )
48
44
kc .subscribe (["test" ])
49
45
while not seen_error_cb :
50
- kc .poll (timeout = 1 )
46
+ kc .poll (timeout = 0. 1 )
51
47
52
48
kc .close ()
53
49
54
50
55
- # global variable for stats_cb call back function
56
- seen_stats_cb = False
57
-
58
-
59
51
def test_stats_cb ():
60
52
""" Tests stats_cb. """
53
+ seen_stats_cb = False
61
54
62
55
def stats_cb (stats_json_str ):
63
- global seen_stats_cb
56
+ nonlocal seen_stats_cb
64
57
seen_stats_cb = True
65
58
stats_json = json .loads (stats_json_str )
66
59
assert len (stats_json ['name' ]) > 0
67
60
68
61
conf = {'group.id' : 'test' ,
69
- 'socket.timeout.ms' : '100' ,
70
62
'session.timeout.ms' : 1000 , # Avoid close() blocking too long
71
63
'statistics.interval.ms' : 200 ,
72
64
'stats_cb' : stats_cb
@@ -76,22 +68,20 @@ def stats_cb(stats_json_str):
76
68
77
69
kc .subscribe (["test" ])
78
70
while not seen_stats_cb :
79
- kc .poll (timeout = 1 )
71
+ kc .poll (timeout = 0. 1 )
80
72
kc .close ()
81
73
82
74
83
- seen_stats_cb_check_no_brokers = False
84
-
85
-
86
75
def test_conf_none ():
87
76
""" Issue #133
88
77
Test that None can be passed for NULL by setting bootstrap.servers
89
78
to None. If None would be converted to a string then a broker would
90
79
show up in statistics. Verify that it doesnt. """
80
+ seen_stats_cb_check_no_brokers = False
91
81
92
82
def stats_cb_check_no_brokers (stats_json_str ):
93
83
""" Make sure no brokers are reported in stats """
94
- global seen_stats_cb_check_no_brokers
84
+ nonlocal seen_stats_cb_check_no_brokers
95
85
stats = json .loads (stats_json_str )
96
86
assert len (stats ['brokers' ]) == 0 , "expected no brokers in stats: %s" % stats_json_str
97
87
seen_stats_cb_check_no_brokers = True
@@ -101,9 +91,8 @@ def stats_cb_check_no_brokers(stats_json_str):
101
91
'stats_cb' : stats_cb_check_no_brokers }
102
92
103
93
p = confluent_kafka .Producer (conf )
104
- p .poll (timeout = 1 )
94
+ p .poll (timeout = 0. 1 )
105
95
106
- global seen_stats_cb_check_no_brokers
107
96
assert seen_stats_cb_check_no_brokers
108
97
109
98
@@ -130,23 +119,19 @@ def test_throttle_event_types():
130
119
assert str (throttle_event ) == "broker/0 throttled for 10000 ms"
131
120
132
121
133
- # global variable for oauth_cb call back function
134
- seen_oauth_cb = False
135
-
136
-
137
122
def test_oauth_cb ():
138
123
""" Tests oauth_cb. """
124
+ seen_oauth_cb = False
139
125
140
126
def oauth_cb (oauth_config ):
141
- global seen_oauth_cb
127
+ nonlocal seen_oauth_cb
142
128
seen_oauth_cb = True
143
129
assert oauth_config == 'oauth_cb'
144
130
return 'token' , time .time () + 300.0
145
131
146
132
conf = {'group.id' : 'test' ,
147
133
'security.protocol' : 'sasl_plaintext' ,
148
134
'sasl.mechanisms' : 'OAUTHBEARER' ,
149
- 'socket.timeout.ms' : '100' ,
150
135
'session.timeout.ms' : 1000 , # Avoid close() blocking too long
151
136
'sasl.oauthbearer.config' : 'oauth_cb' ,
152
137
'oauth_cb' : oauth_cb
@@ -155,7 +140,59 @@ def oauth_cb(oauth_config):
155
140
kc = confluent_kafka .Consumer (** conf )
156
141
157
142
while not seen_oauth_cb :
158
- kc .poll (timeout = 1 )
143
+ kc .poll (timeout = 0.1 )
144
+ kc .close ()
145
+
146
+
147
+ def test_oauth_cb_principal_sasl_extensions ():
148
+ """ Tests oauth_cb. """
149
+ seen_oauth_cb = False
150
+
151
+ def oauth_cb (oauth_config ):
152
+ nonlocal seen_oauth_cb
153
+ seen_oauth_cb = True
154
+ assert oauth_config == 'oauth_cb'
155
+ return 'token' , time .time () + 300.0 , oauth_config , {"extone" : "extoneval" , "exttwo" : "exttwoval" }
156
+
157
+ conf = {'group.id' : 'test' ,
158
+ 'security.protocol' : 'sasl_plaintext' ,
159
+ 'sasl.mechanisms' : 'OAUTHBEARER' ,
160
+ 'session.timeout.ms' : 100 , # Avoid close() blocking too long
161
+ 'sasl.oauthbearer.config' : 'oauth_cb' ,
162
+ 'oauth_cb' : oauth_cb
163
+ }
164
+
165
+ kc = confluent_kafka .Consumer (** conf )
166
+
167
+ while not seen_oauth_cb :
168
+ kc .poll (timeout = 0.1 )
169
+ kc .close ()
170
+
171
+
172
+ def test_oauth_cb_failure ():
173
+ """ Tests oauth_cb. """
174
+ oauth_cb_count = 0
175
+
176
+ def oauth_cb (oauth_config ):
177
+ nonlocal oauth_cb_count
178
+ oauth_cb_count += 1
179
+ assert oauth_config == 'oauth_cb'
180
+ if oauth_cb_count == 2 :
181
+ return 'token' , time .time () + 100.0 , oauth_config , {"extthree" : "extthreeval" }
182
+ raise Exception
183
+
184
+ conf = {'group.id' : 'test' ,
185
+ 'security.protocol' : 'sasl_plaintext' ,
186
+ 'sasl.mechanisms' : 'OAUTHBEARER' ,
187
+ 'session.timeout.ms' : 1000 , # Avoid close() blocking too long
188
+ 'sasl.oauthbearer.config' : 'oauth_cb' ,
189
+ 'oauth_cb' : oauth_cb
190
+ }
191
+
192
+ kc = confluent_kafka .Consumer (** conf )
193
+
194
+ while oauth_cb_count < 2 :
195
+ kc .poll (timeout = 0.1 )
159
196
kc .close ()
160
197
161
198
@@ -194,11 +231,9 @@ def test_unordered_dict(init_func):
194
231
client .poll (0 )
195
232
196
233
197
- # global variable for on_delivery call back function
198
- seen_delivery_cb = False
199
-
200
-
201
234
def test_topic_config_update ():
235
+ seen_delivery_cb = False
236
+
202
237
# *NOTE* default.topic.config has been deprecated.
203
238
# This example remains to ensure backward-compatibility until its removal.
204
239
confs = [{"message.timeout.ms" : 600000 , "default.topic.config" : {"message.timeout.ms" : 1000 }},
@@ -207,7 +242,7 @@ def test_topic_config_update():
207
242
208
243
def on_delivery (err , msg ):
209
244
# Since there is no broker, produced messages should time out.
210
- global seen_delivery_cb
245
+ nonlocal seen_delivery_cb
211
246
seen_delivery_cb = True
212
247
assert err .code () == confluent_kafka .KafkaError ._MSG_TIMED_OUT
213
248
0 commit comments