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)
def test_process_is_not_killed_before_timeout(self): ct = CancellationToken() with subprocess.Popen(["sleep", "1"]) as proc: res = mcerd.MCERD.timeout_check(proc, 0.1, ct) obs = MockObserver() res.subscribe(obs) self.assertIsNone(proc.poll(), msg=FAILURE_MSG) self.assertEqual([], obs.nexts, msg=FAILURE_MSG) self.assertFalse(ct.is_cancellation_requested(), msg=FAILURE_MSG)
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)
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)
def test_process_is_killed_after_timeout(self): ct = CancellationToken() with subprocess.Popen(["sleep", "1"]) as proc: res = mcerd.MCERD.timeout_check(proc, 0.01, ct) obs = MockObserver() res.subscribe(obs) time.sleep(DEFAULT_SLEEP_TIME) self.assertNotEqual(0, int(proc.poll()), msg=FAILURE_MSG) self.assertEqual([{ "is_running": False, "msg": "Simulation timed out" }], obs.nexts, msg=FAILURE_MSG) self.assertTrue(ct.is_cancellation_requested(), msg=FAILURE_MSG)
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)
def test_process_ending_before_timeout_does_not_change_outcome(self): ct = CancellationToken() with subprocess.Popen(["echo", "hello"], stdout=subprocess.DEVNULL) as proc: res = mcerd.MCERD.timeout_check(proc, 0.01, ct) obs = MockObserver() res.subscribe(obs) time.sleep(DEFAULT_SLEEP_TIME) self.assertEqual(0, proc.poll(), msg=FAILURE_MSG) self.assertEqual([{ "is_running": False, "msg": "Simulation timed out" }], obs.nexts, msg=FAILURE_MSG) self.assertTrue(ct.is_cancellation_requested(), msg=FAILURE_MSG)
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)
def test_timeout_check_returns_only_one_item_at_maximum(self): ct = CancellationToken() with subprocess.Popen(["sleep", "1"]) as proc: res = mcerd.MCERD.timeout_check(proc, 0.01, ct) obs = MockObserver() res.subscribe(obs) time.sleep(DEFAULT_SLEEP_TIME) self.assertEqual(1, len(obs.nexts), msg=FAILURE_MSG)
def test_not_requesting_cancellation_lets_the_process_finish(self): ct = CancellationToken() with subprocess.Popen(["sleep", "0.1"]) as proc: res = mcerd.MCERD.cancellation_check(proc, 0.01, ct) obs = MockObserver() res.subscribe(obs) time.sleep(0.2) self.assertEqual(0, proc.poll(), msg=FAILURE_MSG) self.assertEqual([], obs.nexts, msg=FAILURE_MSG)
def start_optimization(self): """Find necessary cut file and make energy spectrum with it, and start optimization with given parameters. """ elem_sim = self.selected_element_simulation cut, measurement = self.selected_cut_file # Delete previous results widget if it exists if self.tab.optimization_result_widget: self.tab.del_widget( self.tab.optimization_result_widget) self.tab.optimization_result_widget = None # Delete previous energy spectra if there are any df.delete_optim_espe(self, elem_sim) self.close() if self.current_mode == OptimizationType.RECOIL: params = self.recoil_widget.get_properties() else: params = self.fluence_widget.get_properties() # TODO move following code to the result widget if self.current_mode == OptimizationType.RECOIL: nsgaii = Nsgaii( element_simulation=elem_sim, measurement=measurement, cut_file=cut, ch=self.ch, **params, use_efficiency=self.use_efficiency, optimize_by_area=self.recoil_widget.optimize_by_area, verbose=self.verbose) else: nsgaii = Nsgaii( element_simulation=elem_sim, measurement=measurement, cut_file=cut, ch=self.ch, **params, use_efficiency=self.use_efficiency, optimize_by_area=self.fluence_widget.optimize_by_area, verbose=self.verbose) # Optimization running thread ct = CancellationToken() optimization_thread = threading.Thread( target=nsgaii.start_optimization, kwargs={"cancellation_token": ct}) # Create necessary results widget result_widget = self.tab.add_optimization_results_widget( elem_sim, cut.name, self.current_mode, ct=ct) elem_sim.optimization_widget = result_widget nsgaii.subscribe(result_widget) optimization_thread.daemon = True optimization_thread.start()
def setUp(self): self.ct = CancellationToken() self.sleep_time = 0.2 self.count = 15