-
Notifications
You must be signed in to change notification settings - Fork 37
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
rohanpm
merged 1 commit into
release-engineering:master
from
alexandrevicenzi:test-logger
Oct 26, 2018
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.