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)
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()