|
4 | 4 | import sys
|
5 | 5 | import unittest
|
6 | 6 |
|
| 7 | +from mock import patch |
| 8 | +from unittest import mock |
| 9 | + |
7 | 10 | import fluent.asynchandler
|
8 | 11 | import fluent.handler
|
9 | 12 | from tests import mockserver
|
@@ -309,3 +312,52 @@ def test_simple(self):
|
309 | 312 | eq('userB', el[2]['to'])
|
310 | 313 | self.assertTrue(el[1])
|
311 | 314 | self.assertTrue(isinstance(el[1], int))
|
| 315 | + |
| 316 | + |
| 317 | +class QueueOverflowException(Exception): |
| 318 | + pass |
| 319 | + |
| 320 | + |
| 321 | +def queue_overflow_handler(discarded_bytes): |
| 322 | + raise QueueOverflowException(discarded_bytes) |
| 323 | + |
| 324 | + |
| 325 | + |
| 326 | + |
| 327 | +class TestHandlerWithCircularQueueHandler(unittest.TestCase): |
| 328 | + Q_SIZE = 1 |
| 329 | + |
| 330 | + def setUp(self): |
| 331 | + super(TestHandlerWithCircularQueueHandler, self).setUp() |
| 332 | + self._server = mockserver.MockRecvServer('localhost') |
| 333 | + self._port = self._server.port |
| 334 | + |
| 335 | + def tearDown(self): |
| 336 | + self._server.close() |
| 337 | + |
| 338 | + def get_handler_class(self): |
| 339 | + # return fluent.handler.FluentHandler |
| 340 | + return fluent.asynchandler.FluentHandler |
| 341 | + |
| 342 | + @patch.object(fluent.asynchandler.asyncsender.Queue, 'full', mock.Mock(return_value=True)) |
| 343 | + def test_simple(self): |
| 344 | + handler = self.get_handler_class()('app.follow', port=self._port, |
| 345 | + queue_maxsize=self.Q_SIZE, |
| 346 | + queue_circular=True, |
| 347 | + queue_overflow_handler=queue_overflow_handler) |
| 348 | + with handler: |
| 349 | + self.assertEqual(handler.sender.queue_circular, True) |
| 350 | + self.assertEqual(handler.sender.queue_maxsize, self.Q_SIZE) |
| 351 | + |
| 352 | + logging.basicConfig(level=logging.INFO) |
| 353 | + log = logging.getLogger('fluent.test') |
| 354 | + handler.setFormatter(fluent.handler.FluentRecordFormatter()) |
| 355 | + log.addHandler(handler) |
| 356 | + |
| 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'}) |
| 363 | + |
0 commit comments