Ejemplo n.º 1
0
 def _execute(self, signal):
     sleep(0.1)
     if hasattr(signal, 'error') and signal.error > 0:
         # If it's an 'error' signal riase an exception to retry
         signal.error -= 1
         raise Exception
     return signal
Ejemplo n.º 2
0
 def test_debounce(self):
     block = Debouncer()
     block._backup = MagicMock()
     self.configure_block(block, {"interval": {"milliseconds": 200}})
     block.start()
     block.process_signals([Signal(), Signal()])
     block.process_signals([Signal()])
     self.assert_num_signals_notified(1, block)
     sleep(.3)
     block.process_signals([Signal()])
     self.assert_num_signals_notified(2, block)
     block.stop()
Ejemplo n.º 3
0
 def test_debounce(self):
     block = Debouncer()
     block._backup = MagicMock()
     self.configure_block(block, {
         "interval": {
             "milliseconds": 200
         }
     })
     block.start()
     block.process_signals([Signal(), Signal()])
     block.process_signals([Signal()])
     self.assert_num_signals_notified(1, block)
     sleep(.3)
     block.process_signals([Signal()])
     self.assert_num_signals_notified(2, block)
     block.stop()
Ejemplo n.º 4
0
    def test_tracker(self):
        """ Test the accuracy of the frequency tracker """
        tracker = FrequencyTracker()

        sleep(1.5)
        # skip the 1st second

        tracker.record(1)
        tracker.record(2)
        # Should be 1 + 2 in the 2nd second = ~3
        self.assertAlmostEqual(tracker.get_frequency(), 3, 1)

        sleep(1)
        tracker.record(3)
        tracker.record(4)
        # Should be 3+4 in the 3rd second = ~7
        self.assertAlmostEqual(tracker.get_frequency(), 7, 1)
Ejemplo n.º 5
0
    def test_collects(self):
        """Test that the block collects the way it is supposed to.

        Run for 5 seconds, collecting every 2 seconds. Spit out a signal every
        second but make sure that only 4 got collected and notified
        """
        block = DummyBlock()
        self.configure_block(block, {
            "collect": {'seconds': 2}
        })
        block.start()

        for _ in range(5):
            block.notify_signals([Signal()])
            sleep(1.1)

        block.stop()
        self.assert_num_signals_notified(4)
Ejemplo n.º 6
0
 def test_debounce_group(self):
     block = Debouncer()
     block._backup = MagicMock()
     self.configure_block(block, {
         "interval": {
             "milliseconds": 200
         },
         "group_by": "{{$foo}}"
     })
     block.start()
     block.process_signals([
         Signal({'foo': 'bar'}),
     ])
     self.assert_num_signals_notified(1, block)
     block.process_signals([Signal({'foo': 'bar'}), Signal({'foo': 'qux'})])
     self.assert_num_signals_notified(2, block)
     sleep(.3)
     block.process_signals([Signal({'foo': 'bar'})])
     self.assert_num_signals_notified(3, block)
     block.stop()
Ejemplo n.º 7
0
    def _before_retry(self, retry_count, **kwargs):
        """Determine if a retry should be attempted or not.

        Optionally override in block.

        If a retry is going to be executed, use this to customize any handling
        before the retry is attempted.

        Return:
            bool: True if a retry should be attempted.

        """
        self._logger.debug("Determining if a retry should be attempted")
        if retry_count >= self.num_retries:
            return False
        else:
            self._logger.debug(
                "Waiting {} seconds before retrying execute method".format(
                    retry_count))
            sleep(retry_count)
            return True
Ejemplo n.º 8
0
 def test_debounce_group(self):
     block = Debouncer()
     block._backup = MagicMock()
     self.configure_block(block, {
         "interval": {
             "milliseconds": 200
         },
         "group_by": "{{$foo}}"
     })
     block.start()
     block.process_signals([
         Signal({'foo': 'bar'}),
     ])
     self.assert_num_signals_notified(1, block)
     block.process_signals([
         Signal({'foo': 'bar'}),
         Signal({'foo': 'qux'})
     ])
     self.assert_num_signals_notified(2, block)
     sleep(.3)
     block.process_signals([Signal({'foo': 'bar'})])
     self.assert_num_signals_notified(3, block)
     block.stop()
Ejemplo n.º 9
0
    def test_backup_job(self):
        """ Tests that periodic backups occur """
        block = DummyBlock()
        self.configure_block(block, {
            'backup_interval': {
                'seconds': 1
            }
        })
        block.persistence.save = MagicMock()
        block.start()
        # Give the block time to perform two saves
        sleep(2.5)
        # Stop the block to initiate one more save
        block.stop()

        # We should have had 3 saves, one every second and one on the stop
        self.assertEqual(block.persistence.save.call_count, 3)

        # Make sure the right data was saved
        self.assertTrue(block.persistence.has_key('saved_1'))
        self.assertTrue(block.persistence.has_key('saved_2'))
        self.assertEqual(len(block.persistence._values), 2)
        self.assertEqual(block.persistence.load('saved_1'), 'value')
        self.assertEqual(block.persistence.load('saved_2'), 'another value')
Ejemplo n.º 10
0
 def _execute(self, signal):
     sleep(0.1)
     return signal