4
4
import sys
5
5
import unittest
6
6
7
- from mock import patch
8
- from unittest import mock
7
+ try :
8
+ from unittest import mock
9
+ except ImportError :
10
+ import mock
11
+ try :
12
+ from unittest .mock import patch
13
+ except ImportError :
14
+ from mock import patch
15
+
16
+
9
17
10
18
import fluent .asynchandler
11
19
import fluent .handler
@@ -322,8 +330,6 @@ def queue_overflow_handler(discarded_bytes):
322
330
raise QueueOverflowException (discarded_bytes )
323
331
324
332
325
-
326
-
327
333
class TestHandlerWithCircularQueueHandler (unittest .TestCase ):
328
334
Q_SIZE = 1
329
335
@@ -339,25 +345,30 @@ def get_handler_class(self):
339
345
# return fluent.handler.FluentHandler
340
346
return fluent .asynchandler .FluentHandler
341
347
342
- @patch .object (fluent .asynchandler .asyncsender .Queue , 'full' , mock .Mock (return_value = True ))
343
348
def test_simple (self ):
344
349
handler = self .get_handler_class ()('app.follow' , port = self ._port ,
345
350
queue_maxsize = self .Q_SIZE ,
346
351
queue_circular = True ,
347
352
queue_overflow_handler = queue_overflow_handler )
348
353
with handler :
349
- self .assertEqual (handler .sender .queue_circular , True )
350
- self .assertEqual (handler .sender .queue_maxsize , self .Q_SIZE )
354
+ def custom_full_queue ():
355
+ handler .sender ._queue .put (b'Mock' , block = True )
356
+ return True
351
357
352
- logging .basicConfig (level = logging .INFO )
353
- log = logging .getLogger ('fluent.test' )
354
- handler .setFormatter (fluent .handler .FluentRecordFormatter ())
355
- log .addHandler (handler )
358
+ with patch .object (fluent .asynchandler .asyncsender .Queue , 'full' , mock .Mock (side_effect = custom_full_queue )):
359
+ self .assertEqual (handler .sender .queue_circular , True )
360
+ self .assertEqual (handler .sender .queue_maxsize , self .Q_SIZE )
356
361
357
- log .info ({'cnt' : 1 , 'from' : 'userA' , 'to' : 'userB' })
358
- with self .assertRaises (QueueOverflowException ):
359
- log .info ({'cnt' : 2 , 'from' : 'userA' , 'to' : 'userB' })
360
- log .info ({'cnt' : 3 , 'from' : 'userA' , 'to' : 'userB' })
361
- with self .assertRaises (QueueOverflowException ):
362
- log .info ({'cnt' : 4 , 'from' : 'userA' , 'to' : 'userB' })
362
+ logging .basicConfig (level = logging .INFO )
363
+ log = logging .getLogger ('fluent.test' )
364
+ handler .setFormatter (fluent .handler .FluentRecordFormatter ())
365
+ log .addHandler (handler )
366
+
367
+ with self .assertRaises (QueueOverflowException ):
368
+ log .info ({'cnt' : 1 , 'from' : 'userA' , 'to' : 'userB' })
369
+
370
+ with self .assertRaises (QueueOverflowException ):
371
+ log .info ({'cnt' : 2 , 'from' : 'userA' , 'to' : 'userB' })
363
372
373
+ with self .assertRaises (QueueOverflowException ):
374
+ log .info ({'cnt' : 3 , 'from' : 'userA' , 'to' : 'userB' })
0 commit comments