def test_msg_is_not_called_when_interval_not_reached(self): callback = MagicMock() message = 'a' cuckoo = Cuckoo(10, callback) cuckoo.reset() cuckoo.msg(message) self.assertFalse(callback.called)
def test_msg_resets_timestamp_after_interval_reached(self): callback = MagicMock() message = 'a' cuckoo = Cuckoo(0.1, callback) cuckoo.reset() timestamp1 = cuckoo.timestamp print(cuckoo.timestamp) self.assertFalse(callback.called) sleep(0.11) cuckoo.msg(message) timestamp2 = cuckoo.timestamp print(cuckoo.timestamp) self.assertTrue(callback.called) assert timestamp1 is not timestamp2
def configure(self): self.host = self.get("host") or "127.0.0.1" self.port = self.get("port") or 5553 self.tags = self.get("tags") or "MSG" self.timeout = self.get("timeout") or 60 * 60 * 24 self.max_queue = self.get("max_queue") or 50 self.key_for_data = self.get("key_for_data") or "CHData" self.key_for_prefix = self.get("key_for_prefix") or "CHPrefix" self.subscription_mode = self.get("subscription_mode", default="any") self.show_statistics = self.get("show_statistics", default=False) self.statistics_interval = self.get("statistics_interval", default=30) self.cuckoo_warn = Cuckoo(60 * 5, log.warning) self.performance_warn = Cuckoo( self.statistics_interval, self.show_performance_statistics ) self.idle_dt = deque(maxlen=1000) self.idle_timer = time.time() self.message_count = 0 self.loop_cycle = 0 self.queue = Queue() self.client = None self.thread = None if self.subscription_mode == "all": self.log.warning( "You subscribed to the ligier in 'all'-mode! " "If you are too slow with data processing, " "you will block other clients. " "If you don't understand this message " "and are running this code on a DAQ machine, " "consult a DAQ expert now and stop this script." ) print( "Connecting to {0} on port {1}\n" "Subscribed tags: {2}\n" "Connection timeout: {3}s\n" "Maximum queue size for incoming data: {4}".format( self.host, self.port, self.tags, self.timeout, self.max_queue ) ) self._init_controlhost() self._start_thread()
def test_msg_is_only_called_when_interval_reached(self): callback = MagicMock() message = "a" cuckoo = Cuckoo(0.1, callback) cuckoo.reset() cuckoo.msg(message) self.assertFalse(callback.called) sleep(0.11) cuckoo.msg(message) self.assertTrue(callback.called)
def test_msg_is_only_called_when_interval_reached(self): callback = MagicMock() message = 'a' cuckoo = Cuckoo(0.1, callback) cuckoo.reset() cuckoo.msg(message) self.assertFalse(callback.called) sleep(0.11) cuckoo.msg(message) self.assertTrue(callback.called)
def test_msg_is_not_called_when_interval_not_reached(self): callback = MagicMock() message = "a" cuckoo = Cuckoo(10, callback) cuckoo.reset() cuckoo.msg(message) self.assertFalse(callback.called)
def test_msg_resets_timestamp_after_interval_reached(self): callback = MagicMock() message = "a" cuckoo = Cuckoo(0.1, callback) cuckoo.reset() timestamp1 = cuckoo.timestamp print(cuckoo.timestamp) self.assertFalse(callback.called) sleep(0.11) cuckoo.msg(message) timestamp2 = cuckoo.timestamp print(cuckoo.timestamp) self.assertTrue(callback.called) assert timestamp1 is not timestamp2
def test_set_interval_on_init(self): cuckoo = Cuckoo(1) self.assertEqual(1, cuckoo.interval)
def test_reset_timestamp(self): cuckoo = Cuckoo() cuckoo.reset() delta = datetime.now() - cuckoo.timestamp self.assertGreater(total_seconds(delta), 0)
def test_interval_reached(self): cuckoo = Cuckoo(0.1) cuckoo.reset() self.assertFalse(cuckoo._interval_reached()) sleep(0.11) self.assertTrue(cuckoo._interval_reached())
def test_msg_calls_callback(self): callback = MagicMock() message = 'a' cuckoo = Cuckoo(callback=callback) cuckoo.msg(message) callback.assert_called_with(message)
def test_msg_calls_callback_with_mixed_args_and_kwargs(self): callback = MagicMock() cuckoo = Cuckoo(callback=callback) cuckoo.msg(1, 2, c=3, d=4) callback.assert_called_with(1, 2, c=3, d=4)
def test_msg_calls_callback_with_multiple_args(self): callback = MagicMock() cuckoo = Cuckoo(callback=callback) cuckoo.msg(1, 2, 3) callback.assert_called_with(1, 2, 3)
def test_msg_calls_callback_with_multiple_kwargs(self): callback = MagicMock() cuckoo = Cuckoo(callback=callback) cuckoo.msg(a=1, b=2) callback.assert_called_with(a=1, b=2)
def test_msg_calls_callback_with_empty_args(self): callback = MagicMock() cuckoo = Cuckoo(callback=callback) cuckoo.msg() callback.assert_called_with()
def test_set_callback(self): callback = 1 cuckoo = Cuckoo(callback=callback) self.assertEqual(1, cuckoo.callback)
def test_msg_gets_called_on_the_very_first_time(self): callback = MagicMock() message = 'a' cuckoo = Cuckoo(1, callback) cuckoo.msg(message) self.assertTrue(callback.called)
def test_msg_gets_called_on_the_very_first_time(self): callback = MagicMock() message = "a" cuckoo = Cuckoo(1, callback) cuckoo.msg(message) self.assertTrue(callback.called)
def test_direct_call_calls_callback(self): callback = MagicMock() message = "a" cuckoo = Cuckoo(callback=callback) cuckoo(message) callback.assert_called_with(message)
def test_msg_sets_timestamp_on_first_call(self): cuckoo = Cuckoo() cuckoo.msg() assert cuckoo.timestamp