Example #1
0
        def do_test(kill_pgid):
            depth = 2  # this is the parent
            res_fn = os.path.join(self.tempdir,
                                  'nested_kill_pgid_%s' % kill_pgid)
            start = time.time()
            RunTimeout.KILL_PGID = kill_pgid
            ec, output = run_timeout(
                [SCRIPT_NESTED, str(depth), res_fn], timeout=timeout)
            # reset it to default
            RunTimeout.KILL_PGID = default
            stop = time.time()
            self.assertEqual(
                ec,
                RUNRUN_TIMEOUT_EXITCODE,
                msg='run_nested kill_pgid %s stopped due to timeout' %
                kill_pgid)
            self.assertTrue(
                stop - start < timeout + 1,
                msg='run_nested kill_pgid %s timeout within margin' %
                kill_pgid)  # give 1 sec margin
            # make it's not too fast
            time.sleep(5)
            # there's now 6 seconds to complete the remainder
            pids = range(depth + 1)
            # normally this is ordered output, but you never know
            for line in open(res_fn).readlines():
                dep, pid, _ = line.strip().split(" ")  # 3rd is PPID
                pids[int(dep)] = int(pid)

            # pids[0] should be killed
            self.assertFalse(
                check_pid(pids[depth]),
                "main depth=%s pid (pids %s) is killed by timeout" % (
                    depth,
                    pids,
                ))

            if kill_pgid:
                # others should be killed as well
                test_fn = self.assertFalse
                msg = ""
            else:
                # others should be running
                test_fn = self.assertTrue
                msg = " not"

            for dep, pid in enumerate(pids[:depth]):
                test_fn(
                    check_pid(pid),
                    "depth=%s pid (pids %s) is%s killed kill_pgid %s" %
                    (dep, pids, msg, kill_pgid))

            # clean them all
            for pid in pids:
                try:
                    os.kill(pid, 0)  # test first
                    os.kill(pid, 9)
                except OSError:
                    pass
Example #2
0
 def test_timeout(self):
     start = time.time()
     timeout = 3
     # longsleep is 10sec
     ec, output = run_timeout([SCRIPT_SIMPLE, 'longsleep'], timeout=timeout)
     stop = time.time()
     self.assertEqual(ec, RUNRUN_TIMEOUT_EXITCODE)
     self.assertTrue(RUNRUN_TIMEOUT_OUTPUT == output)
     self.assertTrue(stop - start < timeout + 1)  # give 1 sec margin
Example #3
0
 def test_timeout(self):
     start = time.time()
     timeout = 3
     # longsleep is 10sec
     ec, output = run_timeout([SCRIPT_SIMPLE, 'longsleep'], timeout=timeout)
     stop = time.time()
     self.assertEqual(ec, RUNRUN_TIMEOUT_EXITCODE)
     self.assertTrue(RUNRUN_TIMEOUT_OUTPUT == output)
     self.assertTrue(stop - start < timeout + 1)  # give 1 sec margin
Example #4
0
        def do_test(kill_pgid):
            depth = 2 # this is the parent
            res_fn = os.path.join(self.tempdir, 'nested_kill_pgid_%s' % kill_pgid)
            start = time.time()
            RunTimeout.KILL_PGID = kill_pgid
            ec, output = run_timeout([SCRIPT_NESTED, str(depth), res_fn], timeout=timeout)
            # reset it to default
            RunTimeout.KILL_PGID = default
            stop = time.time()
            self.assertEqual(ec, RUNRUN_TIMEOUT_EXITCODE, msg='run_nested kill_pgid %s stopped due to timeout'  % kill_pgid)
            self.assertTrue(stop - start < timeout + 1, msg='run_nested kill_pgid %s timeout within margin' % kill_pgid)  # give 1 sec margin
            # make it's not too fast
            time.sleep(5)
            # there's now 6 seconds to complete the remainder
            pids = range(depth+1)
            # normally this is ordered output, but you never know
            for line in open(res_fn).readlines():
                dep, pid, _ = line.strip().split(" ") # 3rd is PPID
                pids[int(dep)] = int(pid)

            # pids[0] should be killed
            self.assertFalse(check_pid(pids[depth]), "main depth=%s pid (pids %s) is killed by timeout" % (depth, pids,))

            if kill_pgid:
                # others should be killed as well
                test_fn = self.assertFalse
                msg = ""
            else:
                # others should be running
                test_fn = self.assertTrue
                msg = " not"

            for dep, pid in enumerate(pids[:depth]):
                test_fn(check_pid(pid), "depth=%s pid (pids %s) is%s killed kill_pgid %s" % (dep, pids, msg, kill_pgid))

            # clean them all
            for pid in pids:
                try:
                    os.kill(pid, 0) # test first
                    os.kill(pid, 9)
                except OSError:
                    pass
Example #5
0
    def test_timeout(self):
        timeout = 3

        # longsleep is 10sec
        start = time.time()
        ec, output = run_timeout([sys.executable, SCRIPT_SIMPLE, 'longsleep'], timeout=timeout)
        stop = time.time()
        self.assertEqual(ec, RUNRUN_TIMEOUT_EXITCODE, msg='longsleep stopped due to timeout')
        self.assertEqual(RUNRUN_TIMEOUT_OUTPUT, output, msg='longsleep expected output')
        self.assertTrue(stop - start < timeout + 1, msg='longsleep timeout within margin')  # give 1 sec margin

        # run_nested is 15 seconds sleep
        # 1st arg depth: 2 recursive starts
        # 2nd arg file: output file: format 'depth pid' (only other processes are sleep)

        def check_pid(pid):
            """ Check For the existence of a unix pid. """
            try:
                os.kill(pid, 0)
            except OSError:
                return False
            else:
                return True

        default = RunTimeout.KILL_PGID

        def do_test(kill_pgid):
            depth = 2 # this is the parent
            res_fn = os.path.join(self.tempdir, 'nested_kill_pgid_%s' % kill_pgid)
            start = time.time()
            RunTimeout.KILL_PGID = kill_pgid
            ec, output = run_timeout([SCRIPT_NESTED, str(depth), res_fn], timeout=timeout)
            # reset it to default
            RunTimeout.KILL_PGID = default
            stop = time.time()
            self.assertEqual(ec, RUNRUN_TIMEOUT_EXITCODE, msg='run_nested kill_pgid %s stopped due to timeout'  % kill_pgid)
            self.assertTrue(stop - start < timeout + 1, msg='run_nested kill_pgid %s timeout within margin' % kill_pgid)  # give 1 sec margin
            # make it's not too fast
            time.sleep(5)
            # there's now 6 seconds to complete the remainder
            pids = range(depth+1)
            # normally this is ordered output, but you never know
            for line in open(res_fn).readlines():
                dep, pid, _ = line.strip().split(" ") # 3rd is PPID
                pids[int(dep)] = int(pid)

            # pids[0] should be killed
            self.assertFalse(check_pid(pids[depth]), "main depth=%s pid (pids %s) is killed by timeout" % (depth, pids,))

            if kill_pgid:
                # others should be killed as well
                test_fn = self.assertFalse
                msg = ""
            else:
                # others should be running
                test_fn = self.assertTrue
                msg = " not"

            for dep, pid in enumerate(pids[:depth]):
                test_fn(check_pid(pid), "depth=%s pid (pids %s) is%s killed kill_pgid %s" % (dep, pids, msg, kill_pgid))

            # clean them all
            for pid in pids:
                try:
                    os.kill(pid, 0) # test first
                    os.kill(pid, 9)
                except OSError:
                    pass

        do_test(False)
Example #6
0
    def test_timeout(self):
        timeout = 3

        # longsleep is 10sec
        start = time.time()
        ec, output = run_timeout([sys.executable, SCRIPT_SIMPLE, 'longsleep'],
                                 timeout=timeout)
        stop = time.time()
        self.assertEqual(ec,
                         RUNRUN_TIMEOUT_EXITCODE,
                         msg='longsleep stopped due to timeout')
        self.assertEqual(RUNRUN_TIMEOUT_OUTPUT,
                         output,
                         msg='longsleep expected output')
        self.assertTrue(
            stop - start < timeout + 1,
            msg='longsleep timeout within margin')  # give 1 sec margin

        # run_nested is 15 seconds sleep
        # 1st arg depth: 2 recursive starts
        # 2nd arg file: output file: format 'depth pid' (only other processes are sleep)

        def check_pid(pid):
            """ Check For the existence of a unix pid. """
            try:
                os.kill(pid, 0)
            except OSError as err:
                sys.stderr.write("check_pid in test_timeout: %s\n" % err)
                return False
            else:
                return True

        default = RunTimeout.KILL_PGID

        def do_test(kill_pgid):
            depth = 2  # this is the parent
            res_fn = os.path.join(self.tempdir,
                                  'nested_kill_pgid_%s' % kill_pgid)
            start = time.time()
            RunTimeout.KILL_PGID = kill_pgid
            ec, output = run_timeout(
                [SCRIPT_NESTED, str(depth), res_fn], timeout=timeout)
            # reset it to default
            RunTimeout.KILL_PGID = default
            stop = time.time()
            self.assertEqual(
                ec,
                RUNRUN_TIMEOUT_EXITCODE,
                msg='run_nested kill_pgid %s stopped due to timeout' %
                kill_pgid)
            self.assertTrue(
                stop - start < timeout + 1,
                msg='run_nested kill_pgid %s timeout within margin' %
                kill_pgid)  # give 1 sec margin
            # make it's not too fast
            time.sleep(5)
            # there's now 6 seconds to complete the remainder
            pids = list(range(depth + 1))
            # normally this is ordered output, but you never know

            fp = open(res_fn)
            lines = fp.readlines()
            fp.close()
            for line in lines:
                dep, pid, _ = line.strip().split(" ")  # 3rd is PPID
                pids[int(dep)] = int(pid)

            # pids[0] should be killed
            self.assertFalse(
                check_pid(pids[depth]),
                "main depth=%s pid (pids %s) is killed by timeout" % (
                    depth,
                    pids,
                ))

            if kill_pgid:
                # others should be killed as well
                test_fn = self.assertFalse
                msg = ""
            else:
                # others should be running
                test_fn = self.assertTrue
                msg = " not"

            for dep, pid in enumerate(pids[:depth]):
                test_fn(
                    check_pid(pid),
                    "depth=%s pid (pids %s) is%s killed kill_pgid %s" %
                    (dep, pids, msg, kill_pgid))

            # clean them all
            for pid in pids:
                try:
                    os.kill(pid, 0)  # test first
                    os.kill(pid, 9)
                except OSError:
                    pass

        do_test(False)