Skip to content

Commit

Permalink
Use a mocked logging handler while running test logging messages
Browse files Browse the repository at this point in the history
  • Loading branch information
heyman committed Nov 15, 2019
1 parent 7146515 commit cffd751
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 5 deletions.
2 changes: 0 additions & 2 deletions locust/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
from .runners import STATE_CLEANUP, LOCUST_STATE_RUNNING, LOCUST_STATE_STOPPING, LOCUST_STATE_WAITING
from .util import deprecation

logger = logging.getLogger(__name__)


def task(weight=1):
"""
Expand Down
17 changes: 17 additions & 0 deletions locust/test/mock_logging.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import logging


class MockedLoggingHandler(logging.Handler):
debug = []
warning = []
info = []
error = []

def emit(self, record):
getattr(self.__class__, record.levelname.lower()).append(record.getMessage())

@classmethod
def reset(cls):
for attr in dir(cls):
if isinstance(getattr(cls, attr), list):
setattr(cls, attr, [])
5 changes: 3 additions & 2 deletions locust/test/test_runners.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,14 @@ def trigger(self):

class TestMasterRunner(LocustTestCase):
def setUp(self):
super(TestMasterRunner, self).setUp()
global_stats.reset_all()
self._slave_report_event_handlers = [h for h in events.slave_report._handlers]
self.options = mocked_options()


def tearDown(self):
events.slave_report._handlers = self._slave_report_event_handlers
super(TestMasterRunner, self).tearDown()

def test_slave_connect(self):
class MyTestLocust(Locust):
Expand Down Expand Up @@ -488,7 +489,7 @@ def test_message_serialize(self):
self.assertEqual(msg.data, rebuilt.data)
self.assertEqual(msg.node_id, rebuilt.node_id)

class TestStopTimeout(unittest.TestCase):
class TestStopTimeout(LocustTestCase):
def test_stop_timeout(self):
short_time = 0.05
class MyTaskSet(TaskSet):
Expand Down
5 changes: 4 additions & 1 deletion locust/test/test_zmqrpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,19 @@
from time import sleep
import zmq
from locust.rpc import zmqrpc, Message
from locust.test.testcases import LocustTestCase


class ZMQRPC_tests(unittest.TestCase):
class ZMQRPC_tests(LocustTestCase):
def setUp(self):
super(ZMQRPC_tests, self).setUp()
self.server = zmqrpc.Server('*', 0)
self.client = zmqrpc.Client('localhost', self.server.port, 'identity')

def tearDown(self):
self.server.socket.close()
self.client.socket.close()
super(ZMQRPC_tests, self).tearDown()

def test_constructor(self):
self.assertEqual(self.server.socket.getsockopt(zmq.TCP_KEEPALIVE), 1)
Expand Down
22 changes: 22 additions & 0 deletions locust/test/testcases.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import base64
import logging
import random
import sys
import unittest
Expand All @@ -13,7 +14,9 @@
send_file, stream_with_context)

from locust import events
from locust.log import console_logger
from locust.stats import global_stats
from locust.test.mock_logging import MockedLoggingHandler


app = Flask(__name__)
Expand Down Expand Up @@ -138,10 +141,29 @@ def setUp(self):
# ResourceWarning doesn't exist in Python 2, but since the warning only appears
# on Python 3 we don't need to mock it. Instead we can happily ignore the exception
pass

# set up mocked logging handler
self._logger_class = MockedLoggingHandler()
self._logger_class.setLevel(logging.INFO)
console_logger.propagate = True
self._root_log_handlers = [h for h in logging.root.handlers]
self._console_log_handlers = [h for h in console_logger.handlers]
[logging.root.removeHandler(h) for h in logging.root.handlers]
[console_logger.removeHandler(h) for h in console_logger.handlers]
logging.root.addHandler(self._logger_class)
logging.root.setLevel(logging.INFO)
self.mocked_log = MockedLoggingHandler

def tearDown(self):
for event, handlers in six.iteritems(self._event_handlers):
event._handlers = handlers

# restore logging class
logging.root.removeHandler(self._logger_class)
[logging.root.addHandler(h) for h in self._root_log_handlers]
[console_logger.addHandler(h) for h in self._console_log_handlers]
self.mocked_log.reset()
console_logger.propagate = False


class WebserverTestCase(LocustTestCase):
Expand Down

0 comments on commit cffd751

Please sign in to comment.