1
1
#!/usr/bin/env python
2
2
3
3
from confluent_kafka import Producer , KafkaError , KafkaException
4
- import threading , time , queue
4
+ import threading
5
+ import time
6
+ try :
7
+ from queue import Queue , Empty
8
+ except :
9
+ from Queue import Queue , Empty
5
10
6
11
7
12
class IntendedException (Exception ):
8
13
pass
9
14
10
- def thread_run (myid ,p ,q ):
11
- def do_crash (err , msg ):
15
+
16
+ def thread_run (myid , p , q ):
17
+ def do_crash (err , msg ):
12
18
raise IntendedException ()
13
19
14
20
for i in range (1 , 3 ):
@@ -23,45 +29,41 @@ def do_crash (err, msg):
23
29
except IntendedException :
24
30
print (myid , "Intentional callback crash: ok" )
25
31
continue
26
- except :
27
- raise
28
32
29
33
print (myid , 'Done' )
30
34
q .put (myid )
31
35
32
-
33
36
34
37
def test_thread_safety ():
35
38
""" Basic thread safety tests. """
36
39
37
- q = queue . Queue ()
38
- p = Producer ({'socket.timeout.ms' :10 ,
40
+ q = Queue ()
41
+ p = Producer ({'socket.timeout.ms' : 10 ,
39
42
'socket.blocking.max.ms' : 10 ,
40
43
'default.topic.config' : {'message.timeout.ms' : 10 }})
41
44
42
45
threads = list ()
43
46
for i in range (1 , 5 ):
44
- thr = threading .Thread (target = thread_run , name = str (i ), args = [i ,p , q ])
47
+ thr = threading .Thread (target = thread_run , name = str (i ), args = [i , p , q ])
45
48
thr .start ()
46
49
threads .append (thr )
47
50
48
51
for thr in threads :
49
52
thr .join ()
50
53
51
-
52
54
# Count the number of threads that exited cleanly
53
55
cnt = 0
54
56
try :
55
57
for x in iter (q .get_nowait , None ):
56
58
cnt += 1
57
- except queue . Empty :
59
+ except Empty :
58
60
pass
59
61
60
62
if cnt != len (threads ):
61
63
raise Exception ('Only %d/%d threads succeeded' % (cnt , len (threads )))
62
64
63
65
print ('Done' )
64
66
65
-
66
- if __name__ == '__main__' :
67
+
68
+ if __name__ == '__main__' :
67
69
test_thread_safety ()
0 commit comments