Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: test logging thread class. #65

Merged
merged 1 commit into from
Oct 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 97 additions & 0 deletions tests/test_logger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# -*- coding: utf-8 -*-

import time

import six
import unittest2 as unittest

from mock import Mock
from six.moves import StringIO

from kobo.worker.logger import LoggingThread, LoggingIO
from .utils import ArgumentIsInstanceOf


class TestLoggingThread(unittest.TestCase):

@unittest.skipIf(six.PY3, 'Encoding errors on Python 3')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you be able to file a bug about that?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but on GitHub we already have a bug report about py3.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then you can mention it in the text here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Created issue #66 for this case.

def test_upload_task_log_on_stop(self):
mock_hub = Mock()
thread = LoggingThread(mock_hub, 9999)
thread.daemon = True

thread.start()
self.assertTrue(thread.is_alive())
self.assertTrue(thread._running)

thread.write('This is a log message!')
mock_hub.upload_task_log.assert_not_called()

thread.stop()
self.assertFalse(thread.is_alive())
self.assertFalse(thread._running)
mock_hub.upload_task_log.assert_called_once_with(ArgumentIsInstanceOf(StringIO), 9999, 'stdout.log', append=True)

@unittest.skipIf(six.PY3, 'Encoding errors on Python 3')
def test_upload_task_log_after_some_time(self):
mock_hub = Mock()
thread = LoggingThread(mock_hub, 9999)
thread.daemon = True

thread.start()

self.assertTrue(thread.is_alive())
self.assertTrue(thread._running)

for i in range(5):
thread.write('%d - This is a very long message to be written in the log\n' % (i + 1))
mock_hub.upload_task_log.assert_not_called()

# let the thread running for a while
time.sleep(.1)
mock_hub.upload_task_log.assert_called_once_with(ArgumentIsInstanceOf(StringIO), 9999, 'stdout.log', append=True)

thread.stop()
self.assertFalse(thread.is_alive())
self.assertFalse(thread._running)


class TestLoggingIO(unittest.TestCase):

def test_write(self):
mock_io = Mock()
mock_logging_thread = Mock()

stream = LoggingIO(mock_io, mock_logging_thread)
stream.write('This is a log message!')

mock_io.write.assert_called_once_with('This is a log message!')
mock_logging_thread.write.assert_called_once_with('This is a log message!')

def test_write_multiple_calls(self):
mock_io = Mock()
mock_logging_thread = Mock()

stream = LoggingIO(mock_io, mock_logging_thread)

for i in range(10):
stream.write('This is the %d log message!' % (i + 1))

mock_io.write.assert_any_call('This is the 1 log message!')
mock_io.write.assert_any_call('This is the 10 log message!')

mock_logging_thread.write.assert_any_call('This is the 1 log message!')
mock_logging_thread.write.assert_any_call('This is the 10 log message!')

self.assertEqual(mock_io.write.call_count, 10)
self.assertEqual(mock_logging_thread.write.call_count, 10)

def test_getattr(self):
mock_io = Mock()
mock_logging_thread = Mock()

stream = LoggingIO(mock_io, mock_logging_thread)
random_variable = stream.random_variable

self.assertFalse(random_variable is None)
self.assertIsInstance(random_variable, Mock)
9 changes: 9 additions & 0 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,12 @@ def data_path(basename):
"""Returns path to a file under 'data' dir."""
this_dir = os.path.dirname(__file__)
return os.path.join(this_dir, 'data', basename)


class ArgumentIsInstanceOf(object):

def __init__(self, classinfo):
self.classinfo = classinfo

def __eq__(self, other):
return isinstance(other, self.classinfo)