def __init__(self, script_name): """Initializes the ScriptRunner for the given script_name""" # DeltaGenerator deltas will be enqueued into self.report_queue. self.report_queue = ReportQueue() def enqueue_fn(msg): self.report_queue.enqueue(msg) self.maybe_handle_execution_control_request() return True self.main_dg = DeltaGenerator(enqueue_fn, container=BlockPath.MAIN) self.sidebar_dg = DeltaGenerator(enqueue_fn, container=BlockPath.SIDEBAR) self.script_request_queue = ScriptRequestQueue() script_path = os.path.join(os.path.dirname(__file__), 'test_data', script_name) super(TestScriptRunner, self).__init__(report=Report(script_path, []), main_dg=self.main_dg, sidebar_dg=self.sidebar_dg, widget_states=WidgetStates(), request_queue=self.script_request_queue) # Accumulates uncaught exceptions thrown by our run thread. self.script_thread_exceptions = [] # Accumulates all ScriptRunnerEvents emitted by us. self.events = [] def record_event(event, **kwargs): self.events.append(event) self.on_event.connect(record_event, weak=False)
def __init__(self, ioloop, script_path, command_line): """Initialize the ReportSession. Parameters ---------- ioloop : tornado.ioloop.IOLoop The Tornado IOLoop that we're running within. script_path : str Path of the Python file from which this report is generated. command_line : str Command line as input by the user. """ # Each ReportSession gets a unique ID self.id = ReportSession._next_id ReportSession._next_id += 1 self._ioloop = ioloop self._report = Report(script_path, command_line) self._state = ReportSessionState.REPORT_NOT_RUNNING self._uploaded_file_mgr = UploadedFileManager() self._main_dg = DeltaGenerator(enqueue=self.enqueue, container=BlockPath.MAIN) self._sidebar_dg = DeltaGenerator( enqueue=self.enqueue, container=BlockPath.SIDEBAR ) self._widget_states = WidgetStates() self._local_sources_watcher = LocalSourcesWatcher( self._report, self._on_source_file_changed ) self._sent_initialize_message = False self._storage = None self._maybe_reuse_previous_run = False self._run_on_save = config.get_option("server.runOnSave") # The ScriptRequestQueue is the means by which we communicate # with the active ScriptRunner. self._script_request_queue = ScriptRequestQueue() self._scriptrunner = None LOGGER.debug("ReportSession initialized (id=%s)", self.id)
def setUp(self): self._report_queue = ReportQueue() def enqueue(msg): self._report_queue.enqueue(msg) return True self._dg = DeltaGenerator(enqueue)
def test_enqueue_new_element_delta_same_id(self): cursor = LockedCursor(index=123) dg = DeltaGenerator(cursor=cursor) self.assertEqual(123, dg._cursor.index) test_data = "some test data" # Use FakeDeltaGenerator.fake_text cause if we use # DeltaGenerator.text, it calls enqueue_new_element_delta # automatically. Ideally I should unwrap it. fake_dg = FakeDeltaGenerator() def marshall_element(element): fake_dg.fake_text(element, test_data) new_dg = dg._enqueue_new_element_delta(marshall_element, None) self.assertEqual(dg._cursor, new_dg._cursor) msg = self.get_message_from_queue() self.assertEqual(123, msg.metadata.delta_id) self.assertEqual(msg.delta.new_element.text.body, test_data)
def test_enqueue_new_element_delta(self, container): dg = DeltaGenerator(container=container) self.assertEqual(0, dg._cursor.index) self.assertEqual(container, dg._container) test_data = "some test data" # Use FakeDeltaGenerator.fake_text cause if we use # DeltaGenerator.text, it calls enqueue_new_element_delta # automatically. Ideally I should unwrap it. fake_dg = FakeDeltaGenerator() def marshall_element(element): fake_dg.fake_text(element, test_data) new_dg = dg._enqueue_new_element_delta(marshall_element, None) self.assertNotEqual(dg, new_dg) self.assertEqual(1, dg._cursor.index) self.assertEqual(container, new_dg._container) element = self.get_delta_from_queue().new_element self.assertEqual(element.text.body, test_data)
def new_delta_generator(self, *args, **kwargs): def enqueue_fn(msg): self.report_queue.enqueue(msg) return True if len(args) > 0: enqueue = args[0] args = args[1:] elif "enqueue" in kwargs: enqueue = kwargs.pop("enqueue") else: enqueue = enqueue_fn return DeltaGenerator(enqueue, *args, **kwargs)
def __init__(self, ioloop, script_path, script_argv): """Initialize the ReportContext. Parameters ---------- ioloop : tornado.ioloop.IOLoop The Tornado IOLoop that we're running within script_path : str Path of the Python file from which this report is generated. script_argv : list of str Command-line arguments to run the script with. """ # Each ReportContext gets a unique ID self.id = ReportContext._next_id ReportContext._next_id += 1 self._ioloop = ioloop self._report = Report(script_path, script_argv) self._root_dg = DeltaGenerator(self.enqueue) self._scriptrunner = ScriptRunner(self._report) self._sent_initialize_message = False self._is_shutdown = False self._storage = None self._maybe_reuse_previous_run = False # ScriptRunner event handlers self._scriptrunner.on_state_changed.connect( self._enqueue_script_state_changed_message) self._scriptrunner.on_file_change_not_handled.connect( self._enqueue_file_change_message) self._scriptrunner.on_script_compile_error.connect( self._on_script_compile_error) # Kick off the scriptrunner's run loop, but don't run the script # itself. self._scriptrunner.start_run_loop(self) LOGGER.debug('ReportContext initialized (id=%s)', self.id)
def test_enqueue_new_element_delta_null(self): # Test "Null" Delta generators dg = DeltaGenerator(container=None) enqueue_fn = lambda x: None new_dg = dg._enqueue_new_element_delta(enqueue_fn, None) self.assertEqual(dg, new_dg)
def test_constructor_with_id(self): """Test DeltaGenerator() with an id.""" cursor = LockedCursor(index=1234) dg = DeltaGenerator(cursor=cursor) self.assertTrue(dg._cursor.is_locked) self.assertEqual(dg._cursor.index, 1234)
def test_constructor(self): """Test default DeltaGenerator().""" dg = DeltaGenerator() self.assertFalse(dg._cursor.is_locked) self.assertEqual(dg._cursor.index, 0)