def test_exception_completion(self): jobspec = JobspecV1.from_command(["false"]) thread = _FluxExecutorThread(threading.Event(), threading.Event(), collections.deque(), 0.01, (), {}) fut = FluxExecutorFuture(threading.get_ident()) self.assertFalse(fut.done()) fut._set_event(EventLogEvent({"name": "start", "timestamp": 0})) self.assertFalse(fut.done()) thread._FluxExecutorThread__event_update( ShamJobEventWatchFuture( EventLogEvent({ "name": "exception", "timestamp": 0, "context": { "severity": 1, "type": "foobar" }, })), fut, ) self.assertFalse(fut.done()) thread._FluxExecutorThread__event_update( ShamJobEventWatchFuture( EventLogEvent({ "name": "exception", "timestamp": 0, "context": { "severity": 0, "type": "foobar" }, })), fut, ) self.assertTrue(fut.done()) self.assertIsInstance(fut.exception(), JobException)
def test_multiple_events(self): fut = FluxExecutorFuture(threading.get_ident()) counter = itertools.count() fut.add_event_callback("urgency", lambda fut, event: next(counter)) total_iterations = 5 for _ in range(5): fut._set_event(EventLogEvent({"name": "urgency", "timestamp": 0})) self.assertEqual(next(counter), total_iterations) new_counter = itertools.count() fut.add_event_callback("urgency", lambda fut, event: next(new_counter)) self.assertEqual(next(new_counter), total_iterations) fut._set_event(EventLogEvent({"name": "urgency", "timestamp": 0})) # invoking the callback increments the counter, as does checking the counter self.assertEqual(next(counter), total_iterations + 2) self.assertEqual(next(new_counter), total_iterations + 2)
def test_callback_deadlock(self): flag = threading.Event() # try waiting for the future from the callback fut = FluxExecutorFuture(threading.get_ident()).add_jobid_callback( lambda fut: (fut.result(), flag.set()) ) fut._set_jobid(5) # check that flag wasn't set---because the callback raised self.assertFalse(flag.is_set()) # try the same with an event callback log_event = EventLogEvent({"name": "debug", "timestamp": 0}) fut.add_event_callback( log_event.name, lambda fut, event: (fut.result(), flag.set()) ) fut._set_event(log_event) self.assertFalse(flag.is_set()) # now complete the future and try again fut.set_result(21) fut.add_jobid_callback(lambda fut: (fut.result(), flag.set())) self.assertTrue(flag.is_set()) flag.clear() fut.add_event_callback( log_event.name, lambda fut, event: (fut.result(), flag.set()) ) self.assertTrue(flag.is_set())
def test_finish_completion(self): thread = _FluxExecutorThread( threading.Event(), collections.deque(), 0.01, (), {} ) for exit_status in (0, 1, 15, 120, 255): flag = threading.Event() fut = FluxExecutorFuture(threading.get_ident()).add_done_callback( lambda fut: flag.set() ) thread._FluxExecutorThread__event_update( ShamJobEventWatchFuture( EventLogEvent( { "name": "finish", "timestamp": 0, "context": {"status": exit_status}, } ) ), fut, ) self.assertTrue(fut.done()) self.assertTrue(flag.is_set()) if os.WIFEXITED(exit_status): self.assertEqual(fut.result(), os.WEXITSTATUS(exit_status)) elif os.WIFSIGNALED(exit_status): self.assertEqual(fut.result(), -os.WTERMSIG(exit_status))
def test_bad_event(self): fut = FluxExecutorFuture(threading.get_ident()) event = "not an event" log_event = EventLogEvent({"name": event, "timestamp": 0}) with self.assertRaises(ValueError): fut._set_event(log_event) with self.assertRaises(ValueError): fut.add_event_callback(event, None)
def test_adding_callbacks_from_callbacks(self): fut = FluxExecutorFuture(threading.get_ident()) flag = threading.Event() nested_callback = lambda fut, event: flag.set() fut.add_event_callback( "start", lambda fut, event: fut.add_event_callback( "start", nested_callback)) fut._set_event(EventLogEvent({"name": "start", "timestamp": 0})) self.assertTrue(flag.is_set())
def test_event_callback(self): fut = FluxExecutorFuture(threading.get_ident()) flag = threading.Event() for state in ("clean", "start", "alloc"): flag.clear() fut.add_event_callback( state, lambda f, log: (flag.set(), self.assertEqual(log.name, state))) log_event = EventLogEvent({"name": state, "timestamp": 0}) fut._set_event(log_event) self.assertTrue(flag.is_set()) # now check that adding callbacks fires them immediately flag.clear() fut.add_event_callback( state, lambda f, log: (flag.set(), self.assertEqual(log.name, state))) self.assertTrue(flag.is_set())