def test_cleanup_thread_resilience(self, *args, **kwargs):
        # test cleanup thread resilience to Redis Exceptions

        redis_conn = MagicMock()
        tc = ThreadedCounter(start_time=1442671176, cycle_time=1,
                             keep_max=1)  # Initialize the counter
        tc.setup(
            redis_conn=redis_conn
        )  # The thread ready but not started since _threaded_start is mock patched

        # Check that a non redis exception kills the thread (assurance that the correct method is tested)
        tc.expire = MagicMock(side_effect=Exception("1"))
        try:
            tc._do_thread_work()
            # this should NOT be reached
            self.assertTrue(False)
        except Exception as e:
            # this should be reached
            self.assertEqual(str(e), "1")

        # Check that a redis exception doesn't kill the thread
        tc.expire = MagicMock(side_effect=redis.RedisError())
        try:
            tc._do_thread_work()
            # this should be reached
            self.assertTrue(True)
        except:
            # this should NOT be reached
            self.assertTrue(False)
Exemplo n.º 2
0
    def test_is_expired(self):
        # test rolling the key we are using
        tc = ThreadedCounter(start_time=1442671176, window=5)
        tc._time = MagicMock(return_value=1442671177)
        self.assertFalse(tc.is_expired())

        tc._time = MagicMock(return_value=1442671182)
        self.assertTrue(tc.is_expired())
Exemplo n.º 3
0
    def test_set_key(self):
        tc = ThreadedCounter(start_time=1442671176)
        self.assertEqual('default_counter', tc.get_key())

        tc.roll = True
        tc.key = 'myKey'
        tc._set_key()
        self.assertEqual('myKey:2015-09-19_13:59:36', tc.get_key())
Exemplo n.º 4
0
 def test_threading(self):
     # this test ensures the thread can start and stop
     tc = ThreadedCounter(start_time=1442671176, cycle_time=1)
     tc.expire = MagicMock()
     tc.setup(redis_conn=self.redis_conn)
     time.sleep(1)
     tc.stop()
Exemplo n.º 5
0
    def test_set_key(self):
        tc = ThreadedCounter(start_time=1442671176)
        self.assertEqual('default_counter', tc.get_key())

        tc.roll = True
        tc.key = 'myKey'
        tc._set_key()
        self.assertEqual('myKey:2015-09-19_13:59:36', tc.get_key())
Exemplo n.º 6
0
 def test_threading(self):
     # this test ensures the thread can start and stop
     tc = ThreadedCounter(start_time=1442671176, cycle_time=1)
     tc.expire = MagicMock()
     tc.setup(redis_conn=self.redis_conn)
     time.sleep(1)
     tc.stop()
Exemplo n.º 7
0
    def test_is_expired(self):
        # test rolling the key we are using
        tc = ThreadedCounter(start_time=1442671176, window=5)
        tc._time = MagicMock(return_value=1442671177)
        self.assertFalse(tc.is_expired())

        tc._time = MagicMock(return_value=1442671182)
        self.assertTrue(tc.is_expired())
Exemplo n.º 8
0
    def test_purge_old(self):
        # test removing old keys
        tc = ThreadedCounter(keep_max=1)
        tc.keep_max = 1
        tc.redis_conn = self.redis_conn
        self.redis_conn.set('default_counter:2015-09', 'stuff')
        self.redis_conn.set('default_counter:2015-10', 'stuff2')

        tc.purge_old()
        self.assertEqual(['default_counter:2015-10'],
                         self.redis_conn.keys(tc.get_key() + ':*'))

        self.redis_conn.delete('default_counter:2015-10')
Exemplo n.º 9
0
    def test_purge_old(self):
        # test removing old keys
        tc = ThreadedCounter(keep_max=1)
        tc.keep_max = 1
        tc.redis_conn = self.redis_conn
        self.redis_conn.set('default_counter:2015-09', 'stuff')
        self.redis_conn.set('default_counter:2015-10', 'stuff2')

        tc.purge_old()
        self.assertEqual(['default_counter:2015-10'],
            self.redis_conn.keys(tc.get_key() + ':*'))

        self.redis_conn.delete('default_counter:2015-10')