Example #1
0
class TestCancellationToken(unittest.TestCase):
    def setUp(self):
        self.ct = CancellationToken()
        self.sleep_time = 0.2
        self.count = 15

    def test_token(self):
        self.assertFalse(self.ct.is_cancellation_requested())
        self.ct.request_cancellation()
        self.assertTrue(self.ct.is_cancellation_requested())
        self.assertRaises(SystemExit, lambda: self.ct.raise_if_cancelled())

    def test_cancelling_threads(self):
        # Create a bunch of threads and start a timer
        threads = [
            threading.Thread(target=sleeper, args=(self.sleep_time, self.ct))
            for _ in range(self.count)
        ]
        start = timer()
        for t in threads:
            t.start()

        # Request cancellation and wait for threads to end
        self.ct.request_cancellation()
        for t in threads:
            t.join()

        # Assert that threads finished faster than 1.5x the sleep time
        stop = timer()
        self.assertLess(stop - start, 1.5 * self.sleep_time)
Example #2
0
    def test_process_is_not_killed_immediately_after_cancellation(self):
        ct = CancellationToken()
        with subprocess.Popen(["sleep", "1"]) as proc:
            res = mcerd.MCERD.cancellation_check(proc, 0.05, ct)

            obs = MockObserver()
            res.subscribe(obs)
            ct.request_cancellation()

            self.assertIsNone(proc.poll(), msg=FAILURE_MSG)
            self.assertEqual([], obs.nexts, msg=FAILURE_MSG)
Example #3
0
    def test_cancellation_check_returns_one_item_at_maximum(self):
        ct = CancellationToken()
        with subprocess.Popen(["sleep", "1"]) as proc:
            res = mcerd.MCERD.cancellation_check(proc, 0.01, ct)

            obs = MockObserver()
            res.subscribe(obs)

            time.sleep(0.25)
            ct.request_cancellation()
            time.sleep(0.25)

            self.assertEqual(1, len(obs.nexts), msg=FAILURE_MSG)
Example #4
0
    def test_requesting_cancellation_kills_the_process(self):
        ct = CancellationToken()
        with subprocess.Popen(["sleep", "1"]) as proc:
            res = mcerd.MCERD.cancellation_check(proc, 0.01, ct)

            obs = MockObserver()
            res.subscribe(obs)
            ct.request_cancellation()

            time.sleep(DEFAULT_SLEEP_TIME)
            self.assertNotEqual(0, int(proc.poll()), msg=FAILURE_MSG)
            self.assertEqual([{
                "is_running": False,
                "msg": "Simulation was stopped"
            }],
                             obs.nexts,
                             msg=FAILURE_MSG)
Example #5
0
    def test_cancelling_after_process_ends_does_not_change_observed_values(
            self):
        ct = CancellationToken()
        with subprocess.Popen(["echo", "hello"], stdout=subprocess.DEVNULL) as \
                proc:
            res = mcerd.MCERD.cancellation_check(proc, 0.01, ct)

        obs = MockObserver()
        res.subscribe(obs)
        ct.request_cancellation()

        time.sleep(DEFAULT_SLEEP_TIME)
        self.assertEqual(0, proc.poll(), msg=FAILURE_MSG)
        self.assertEqual([{
            "is_running": False,
            "msg": "Simulation was stopped"
        }],
                         obs.nexts,
                         msg=FAILURE_MSG)