Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

added startLogging call to twisted logger

  • Loading branch information...
commit 680dc6d56b694cf8b3d5fc04f9e90b012821a0e0 1 parent d392fd9
@mriehl mriehl authored
View
8 src/main/python/yadtreceiver/__init__.py
@@ -32,6 +32,7 @@
from twisted.application import service
from twisted.internet import reactor
from twisted.python import log
+from twisted.python.logfile import LogFile
from yadtbroadcastclient import WampBroadcaster
@@ -164,7 +165,7 @@ def _refresh_connection(self, delay=60 * 60, first_call=False):
def onConnectionLost(self, reason):
"""
- Allows for clean reconnect behaviour, because it 'none'ifies
+ Allows for clean reconnect behaviour, because it 'none'ifies
the client explicitly
"""
log.err('connection lost: %s' % reason)
@@ -175,8 +176,8 @@ def onConnectionLost(self, reason):
def _client_watchdog(self, delay=1):
"""
Checks periodically if broadcaster.client is set
- (see also onConnectionLost), tries to reconnect after a
- delay (delay after failed connect: 1, 2, 4, 8, 16, 32, 60, 60, ...
+ (see also onConnectionLost), tries to reconnect after a
+ delay (delay after failed connect: 1, 2, 4, 8, 16, 32, 60, 60, ...
seconds)
"""
if hasattr(self, 'broadcaster') and self.broadcaster.client:
@@ -222,6 +223,7 @@ def startService(self):
"""
Initializes logging and establishes connection to broadcaster.
"""
+ log.startLogging(LogFile.fromFullPath(self.configuration['log_filename']))
log.msg('yadtreceiver version %s' % __version__)
self._client_watchdog()
self._refresh_connection(first_call=True)
View
87 src/unittest/python/yadtreceiver_tests.py
@@ -20,6 +20,8 @@
import unittest
from mock import Mock, call, patch
+from twisted.mail.scripts.mailmail import Configuration
+from twisted.python.logfile import LogFile
from yadtreceiver import __version__, Receiver, ReceiverException
from yadtreceiver.events import Event
@@ -39,7 +41,7 @@ def test_should_set_configuration (self):
self.assertEquals(configuration, receiver.configuration)
@patch('yadtreceiver.WampBroadcaster')
- def test_should_initialize_broadcaster_when_starting_service (self, mock_wamb):
+ def test_should_initialize_broadcaster_when_connecting_broadcaster (self, mock_wamb):
configuration = Mock(Configuration)
configuration.broadcaster_host = 'broadcaster-host'
configuration.broadcaster_port = 1234
@@ -52,7 +54,7 @@ def test_should_initialize_broadcaster_when_starting_service (self, mock_wamb):
@patch('yadtreceiver.WampBroadcaster')
- def test_should_add_session_handler_to_broadcaster_when_starting_service (self, mock_wamb):
+ def test_should_add_session_handler_to_broadcaster_when_connecting_broadcaster (self, mock_wamb):
receiver = Receiver()
configuration = {'broadcaster_host' : 'broadcasterhost',
'broadcaster_port' : 1234}
@@ -66,7 +68,7 @@ def test_should_add_session_handler_to_broadcaster_when_starting_service (self,
@patch('yadtreceiver.WampBroadcaster')
- def test_should_connect_broadcaster_when_starting_service (self, mock_wamb):
+ def test_should_connect_broadcaster_when_connecting_broadcaster (self, mock_wamb):
receiver = Receiver()
configuration = {'broadcaster_host' : 'broadcaster-host',
'broadcaster_port' : 1234}
@@ -80,7 +82,7 @@ def test_should_connect_broadcaster_when_starting_service (self, mock_wamb):
@patch('yadtreceiver.WampBroadcaster')
- def test_should_initialize_broadcaster_when_starting_service (self, mock_wamb):
+ def test_should_initialize_broadcaster_when_connecting_broadcaster (self, mock_wamb):
configuration = {'broadcaster_host' : 'broadcaster-host',
'broadcaster_port' : 1234}
receiver = Receiver()
@@ -105,8 +107,12 @@ def test_should_exit_when_no_target_configured (self, mock_exit, mock_log):
self.assertEquals(call(1), mock_exit.call_args)
- def test_should_initialize_watchdog_and_start_connection_when_service_starts (self):
+ @patch('yadtreceiver.LogFile')
+ @patch('yadtreceiver.log')
+ def test_should_initialize_watchdog_and_start_connection_when_service_starts (self, mock_log, mock_log_file):
mock_receiver = Mock(Receiver)
+ mock_receiver.configuration = {'log_filename' : 'log/file.log'}
+
Receiver.startService(mock_receiver)
@@ -114,6 +120,22 @@ def test_should_initialize_watchdog_and_start_connection_when_service_starts (se
self.assertEquals(call(first_call=True), mock_receiver._refresh_connection.call_args)
+ @patch('yadtreceiver.LogFile')
+ @patch('yadtreceiver.log')
+ def test_should_initialize_logging_when_service_starts (self, mock_log, mock_log_file_class):
+ receiver = Receiver()
+ receiver.set_configuration({'log_filename' : 'log/file.log',
+ 'targets' : set(['devabc123']),
+ 'broadcaster_host': 'broadcaster_host',
+ 'broadcaster_port': 1234})
+ mock_log_file = Mock(LogFile)
+ mock_log_file_class.fromFullPath.return_value = mock_log_file
+
+ receiver.startService()
+
+ self.assertEquals(call(mock_log_file), mock_log.startLogging.call_args)
+
+
def test_should_subscribe_to_target_from_configuration_when_connected (self):
receiver = Receiver()
mock_broadcaster_client = Mock()
@@ -146,7 +168,7 @@ def test_should_raise_exception_when_target_directory_does_not_exist (self, mock
mock_exists.return_value = False
receiver = Receiver()
configuration = {'hostname' : 'hostname',
- 'targets_directory' : '/etc/yadtshell/targets/'}
+ 'targets_directory' : '/etc/yadtshell/targets/'}
receiver.set_configuration(configuration)
self.assertRaises(ReceiverException, receiver.get_target_directory, 'spargel')
@@ -157,7 +179,7 @@ def test_should_append_target_name_to_targets_directory (self, mock_exists):
mock_exists.return_value = True
receiver = Receiver()
configuration = {'hostname' : 'hostname',
- 'targets_directory' : '/etc/yadtshell/targets/'}
+ 'targets_directory' : '/etc/yadtshell/targets/'}
receiver.set_configuration(configuration)
actual_target_directory = receiver.get_target_directory('spargel')
@@ -170,7 +192,7 @@ def test_should_join_target_name_with_targets_directory (self, mock_exists):
mock_exists.return_value = True
receiver = Receiver()
configuration = {'hostname' : 'hostname',
- 'targets_directory' : '/etc/yadtshell/targets/'}
+ 'targets_directory' : '/etc/yadtshell/targets/'}
receiver.set_configuration(configuration)
actual_target_directory = receiver.get_target_directory('spargel')
@@ -361,9 +383,9 @@ def test_should_publish_event_about_failed_request_when_handle_request_fails (se
@patch('yadtreceiver.reactor')
def test_should_queue_call_to_refresh_connection (self, mock_reactor):
mock_receiver = Mock(Receiver)
-
+
Receiver._refresh_connection(mock_receiver, 123)
-
+
self.assertEquals(call(123, mock_receiver._refresh_connection), mock_reactor.callLater.call_args)
@@ -374,7 +396,7 @@ def test_should_close_connection_to_broadcaster_when_not_first_call (self, mock_
mock_receiver.broadcaster = mock_broadcaster
Receiver._refresh_connection(mock_receiver, 123)
-
+
self.assertEquals(call(), mock_broadcaster.client.sendClose.call_args)
@@ -385,7 +407,7 @@ def test_should_not_close_connection_to_broadcaster_when_first_call (self, mock_
mock_receiver.broadcaster = mock_broadcaster
Receiver._refresh_connection(mock_receiver, 123, first_call=True)
-
+
self.assertEquals(None, mock_broadcaster.client.sendClose.call_args)
@@ -395,12 +417,12 @@ def test_should_set_the_client_to_none (self, mock_log):
mock_broadcaster = Mock()
mock_broadcaster.client = 'Test client'
mock_receiver.broadcaster = mock_broadcaster
-
+
Receiver.onConnectionLost(mock_receiver, 'Spam eggs.')
-
+
self.assertEquals(None, mock_broadcaster.client)
-
-
+
+
@patch('yadtreceiver.log')
@patch('yadtreceiver.reactor')
def test_should_queue_call_to_client_watchdog_when_client_available (self, mock_reactor, mock_log):
@@ -408,9 +430,9 @@ def test_should_queue_call_to_client_watchdog_when_client_available (self, mock_
mock_broadcaster = Mock()
mock_broadcaster.client = 'Test client'
mock_receiver.broadcaster = mock_broadcaster
-
+
Receiver._client_watchdog(mock_receiver)
-
+
self.assertEquals(call(1, mock_receiver._client_watchdog), mock_reactor.callLater.call_args)
@@ -418,9 +440,9 @@ def test_should_queue_call_to_client_watchdog_when_client_available (self, mock_
@patch('yadtreceiver.reactor')
def test_should_queue_call_to_client_watchdog_no_client_available (self, mock_reactor, mock_log):
mock_receiver = Mock(Receiver)
-
+
Receiver._client_watchdog(mock_receiver)
-
+
self.assertEquals(call(1, mock_receiver._client_watchdog, 2), mock_reactor.callLater.call_args)
@@ -428,9 +450,9 @@ def test_should_queue_call_to_client_watchdog_no_client_available (self, mock_re
@patch('yadtreceiver.reactor')
def test_should_queue_call_to_client_watchdog_no_client_available_and_double_delay_when_delay_of_two_is_given (self, mock_reactor, mock_log):
mock_receiver = Mock(Receiver)
-
+
Receiver._client_watchdog(mock_receiver, delay=2)
-
+
self.assertEquals(call(2, mock_receiver._client_watchdog, 4), mock_reactor.callLater.call_args)
@@ -438,9 +460,9 @@ def test_should_queue_call_to_client_watchdog_no_client_available_and_double_del
@patch('yadtreceiver.reactor')
def test_should_queue_call_to_client_watchdog_no_client_available_and_double_delay_when_delay_of_four_is_given (self, mock_reactor, mock_log):
mock_receiver = Mock(Receiver)
-
+
Receiver._client_watchdog(mock_receiver, delay=4)
-
+
self.assertEquals(call(4, mock_receiver._client_watchdog, 8), mock_reactor.callLater.call_args)
@@ -448,9 +470,9 @@ def test_should_queue_call_to_client_watchdog_no_client_available_and_double_del
@patch('yadtreceiver.reactor')
def test_should_queue_call_to_client_watchdog_no_client_available_and_return_sixty_as_maximum_delay_when_delay_of_one_sixty_is_given (self, mock_reactor, mock_log):
mock_receiver = Mock(Receiver)
-
+
Receiver._client_watchdog(mock_receiver, delay=60)
-
+
self.assertEquals(call(60, mock_receiver._client_watchdog, 60), mock_reactor.callLater.call_args)
@@ -459,18 +481,17 @@ def test_should_queue_call_to_client_watchdog_no_client_available_and_return_six
def test_should_return_result_of_connect_broadcaster (self, mock_reactor, mock_log):
mock_receiver = Mock(Receiver)
mock_receiver._connect_broadcaster.return_value = 'spam eggs'
-
+
actual_result = Receiver._client_watchdog(mock_receiver)
-
+
self.assertEquals('spam eggs', actual_result)
-
-
+
+
@patch('yadtreceiver.log')
def test_should_log_shutting_down_of_service (self, mock_log):
mock_receiver = Mock(Receiver)
-
+
Receiver.stopService(mock_receiver)
-
+
# Since all the method does is logging we are asserting it here.
self.assertEquals(call('shutting down service'), mock_log.msg.call_args)
-
Please sign in to comment.
Something went wrong with that request. Please try again.