def test_stdin(self): command = ["cat"] text = b"1\n2\n3\n" monitor = ProcessMonitor(command, capture_output=True, input=text) stats = monitor.run() self.assertEqual(stats["stdout"], text) self.assertEqual(stats["return_code"], 0)
def test_grandchildren(self): program = Path(DIR) / "_programs" / "child_cpu_time_2.sh" limit = 1 monitor = ProcessMonitor([program]) monitor.subscribe("wall_time", WallTimeLimiter(limit)) stats = monitor.run() self.assertLess(stats["wall_time"], limit + TOLERANCE)
def test_memory_usage(self): program = Path(DIR) / "_programs" / "child_memory.sh" limit = 200 * 1024 * 1024 monitor = ProcessMonitor([program], capture_output=True, freq=20) monitor.subscribe("max_memory", MaxMemoryLimiter(limit)) stats = monitor.run() self.assertLess(stats["max_memory"], 1.5 * limit) self.assertEqual(stats["stdout"], b"1\n")
def test_deep_sigterm_handler(self): program = Path(DIR) / "_programs" / "sigterm_echo.sh" num = 10 monitor = ProcessMonitor([program, str(num), "10"], capture_output=True) monitor.subscribe("wall_time", WallTimeLimiter(1)) stats = monitor.run() num_terminated = stats["stderr"].decode().count("Terminated") self.assertEqual(num_terminated, num)
def run( self, cmdline, out_path=None, err_path=None, input_str=None, directory=None, **kwargs, ): out_file = open(out_path, "wb") err_file = open(err_path, "wb") monitor = ProcessMonitor( cmdline, cwd=directory, stdout=out_file, stderr=err_file, input=input_str, freq=15, ) monitor.subscribe("wall_time", WallTimeLimiter(self.wall_limit)) monitor.subscribe("cpu_time", CpuTimeLimiter(self.cpu_limit)) monitor.subscribe("max_memory", MaxMemoryLimiter(self.mem_limit)) logger.debug(f"Running {directory}") stats = monitor.run() logger.debug(f"Finished {directory}") out_file.close() err_file.close() payload = self.compile_stats(stats) send_event(self.socket, STORE_RUNSTATS, payload)
def test_fast_exit(self): command = ["echo", "1"] monitor = ProcessMonitor(command, capture_output=True) monitor.subscribe("wall_time", WallTimeLimiter(1)) monitor.subscribe("cpu_time", CpuTimeLimiter(1)) monitor.subscribe("max_memory", MaxMemoryLimiter(1000000)) stats = monitor.run() self.assertEqual(stats["stdout"], b"1\n") self.assertEqual(stats["return_code"], 0) self.assertGreater(stats["wall_time"], 0) self.assertNotEqual(stats["cpu_time"], None) self.assertNotEqual(stats["max_memory"], None)