Skip to content

Commit 06dac3e

Browse files
committed
Add a test for the circular queue sender using the python logging interface.
1 parent 717eb3c commit 06dac3e

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

tests/test_asynchandler.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,3 +277,60 @@ def test_non_string_dict_message(self):
277277
data = self.get_data()
278278
# For some reason, non-string keys are ignored
279279
self.assertFalse(42 in data[0][2])
280+
281+
282+
class TestHandlerWithCircularQueue(unittest.TestCase):
283+
Q_TIMEOUT = 0.04
284+
Q_SIZE = 3
285+
286+
def setUp(self):
287+
super(TestHandlerWithCircularQueue, self).setUp()
288+
self._server = mockserver.MockRecvServer('localhost')
289+
self._port = self._server.port
290+
self.handler = None
291+
292+
def get_handler_class(self):
293+
# return fluent.handler.FluentHandler
294+
return fluent.asynchandler.FluentHandler
295+
296+
def get_data(self):
297+
return self._server.get_recieved()
298+
299+
def test_simple(self):
300+
handler = self.get_handler_class()('app.follow', port=self._port,
301+
queue_timeout=self.Q_TIMEOUT,
302+
queue_maxsize=self.Q_SIZE,
303+
queue_circular=True)
304+
self.handler = handler
305+
306+
self.assertEqual(self.handler.sender.queue_circular, True)
307+
self.assertEqual(self.handler.sender.queue_maxsize, self.Q_SIZE)
308+
309+
logging.basicConfig(level=logging.INFO)
310+
log = logging.getLogger('fluent.test')
311+
handler.setFormatter(fluent.handler.FluentRecordFormatter())
312+
log.addHandler(handler)
313+
log.info({'cnt': 1, 'from': 'userA', 'to': 'userB'})
314+
log.info({'cnt': 2, 'from': 'userA', 'to': 'userB'})
315+
log.info({'cnt': 3, 'from': 'userA', 'to': 'userB'})
316+
log.info({'cnt': 4, 'from': 'userA', 'to': 'userB'})
317+
log.info({'cnt': 5, 'from': 'userA', 'to': 'userB'})
318+
319+
# wait, giving time to the communicator thread to send the messages
320+
time.sleep(0.5)
321+
# close the handler, to join the thread and let the test suite to terminate
322+
handler.close()
323+
324+
data = self.get_data()
325+
eq = self.assertEqual
326+
# with the logging interface, we can't be sure to have filled up the queue, so we can
327+
# test only for a cautelative condition here
328+
self.assertTrue(len(data) >= self.Q_SIZE)
329+
330+
el = data[0]
331+
eq(3, len(el))
332+
eq('app.follow', el[0])
333+
eq('userA', el[2]['from'])
334+
eq('userB', el[2]['to'])
335+
self.assertTrue(el[1])
336+
self.assertTrue(isinstance(el[1], int))

0 commit comments

Comments
 (0)