def test_remove_while_running(self, ): thread = threading.Thread(target=self.thread_function, ) thread.start() killer = sergeant.killer.thread.Killer(exception=ExceptionTest, ) killer.start() killer.add( thread_id=thread.ident, timeout=0.5, ) self.assertTrue(expr=thread.is_alive(), ) time.sleep(0.3) self.assertNotEqual( first=killer.thread_to_end_time, second={}, ) killer.remove(thread_id=thread.ident, ) self.assertEqual( first=killer.thread_to_end_time, second={}, ) time.sleep(0.3) self.assertTrue(expr=thread.is_alive(), ) killer.stop() self.thread_enabled = False thread.join()
def test_simple(self, ): thread = threading.Thread(target=self.thread_function, ) thread.start() killer = sergeant.killer.thread.Killer(exception=ExceptionTest, ) killer.start() killer.add( thread_id=thread.ident, timeout=0.5, ) self.assertTrue(expr=thread.is_alive(), ) time.sleep(0.6) self.assertFalse(expr=thread.is_alive(), ) self.assertIsInstance( obj=self.raised_exception, cls=ExceptionTest, ) self.assertEqual( first=killer.thread_to_end_time, second={}, ) self.assertTrue(expr=killer.enabled, ) killer.stop() self.assertFalse(expr=killer.enabled, )
def test_reuse_after_kill(self, ): thread = threading.Thread(target=self.thread_function, ) thread.start() killer = sergeant.killer.thread.Killer( thread_id=thread.ident, timeout=0.3, exception=ExceptionTest, ) killer.start() self.assertTrue(expr=thread.is_alive(), ) time.sleep(0.5) self.assertFalse(expr=thread.is_alive(), ) self.assertIsInstance( obj=self.raised_exception, cls=ExceptionTest, ) self.assertFalse(expr=killer.killer_thread.running, ) thread = threading.Thread(target=self.thread_function, ) thread.start() killer.killer_thread.thread_id = thread.ident killer.reset() killer.resume() self.assertTrue(expr=thread.is_alive(), ) time.sleep(0.5) self.assertFalse(expr=thread.is_alive(), ) self.assertIsInstance( obj=self.raised_exception, cls=ExceptionTest, ) self.assertFalse(expr=killer.killer_thread.running, ) killer.kill()
def test_suspend_resume(self, ): thread = threading.Thread(target=self.thread_function, ) thread.start() killer = sergeant.killer.thread.Killer( thread_id=thread.ident, timeout=0.7, exception=ExceptionTest, ) killer.start() self.assertTrue(expr=thread.is_alive(), ) time.sleep(0.5) killer.suspend() self.assertEqual( first=killer.killer_thread.time_elapsed, second=0.5, ) self.assertFalse(expr=killer.killer_thread.running, ) self.assertTrue(expr=killer.killer_thread.enabled, ) time.sleep(0.3) self.assertTrue(expr=thread.is_alive(), ) self.assertEqual( first=killer.killer_thread.time_elapsed, second=0.5, ) self.assertFalse(expr=killer.killer_thread.running, ) self.assertTrue(expr=killer.killer_thread.enabled, ) killer.resume() time.sleep(0.4) self.assertFalse(expr=thread.is_alive(), ) self.assertEqual( first=killer.killer_thread.time_elapsed, second=0.7, ) self.assertFalse(expr=killer.killer_thread.running, ) self.assertTrue(expr=killer.killer_thread.enabled, ) self.assertIsInstance( obj=self.raised_exception, cls=ExceptionTest, ) killer.kill() self.assertFalse(expr=killer.killer_thread.enabled, )