def test_enqueue_without_tracer(self, _1, _2, patched_config): """Make sure we try to handle execution control requests.""" def get_option(name): if name == "server.runOnSave": # Just to avoid starting the watcher for no reason. return False if name == "client.displayEnabled": return True if name == "runner.installTracer": return False raise RuntimeError("Unexpected argument to get_option: %s" % name) patched_config.get_option.side_effect = get_option rs = ReportSession(None, "", "", UploadedFileManager()) mock_script_runner = MagicMock() mock_script_runner._install_tracer = ScriptRunner._install_tracer rs._scriptrunner = mock_script_runner mock_msg = MagicMock() rs.enqueue(mock_msg) func = mock_script_runner.maybe_handle_execution_control_request # Expect func to be called only once, inside enqueue(). func.assert_called_once()
def test_set_page_config_immutable(self, _1): """st.set_page_config must be called at most once""" file_mgr = MagicMock(spec=UploadedFileManager) rs = ReportSession(None, "", "", file_mgr) msg = ForwardMsg() msg.page_config_changed.title = "foo" rs.enqueue(msg) with self.assertRaises(StreamlitAPIException): rs.enqueue(msg)
def test_set_page_config_first(self, _1): """st.set_page_config must be called before other st commands""" file_mgr = MagicMock(spec=UploadedFileManager) rs = ReportSession(None, "", "", file_mgr) markdown_msg = ForwardMsg() markdown_msg.delta.new_element.markdown.body = "foo" msg = ForwardMsg() msg.page_config_changed.title = "foo" rs.enqueue(markdown_msg) with self.assertRaises(StreamlitAPIException): rs.enqueue(msg)
def test_enqueue_with_tracer(self, _1, _2, patched_config, _4): """Make sure there is no lock contention when tracer is on. When the tracer is set up, we want maybe_handle_execution_control_request to be executed only once. There was a bug in the past where it was called twice: once from the tracer and once from the enqueue function. This caused a lock contention. """ def get_option(name): if name == "server.runOnSave": # Just to avoid starting the watcher for no reason. return False if name == "client.displayEnabled": return True if name == "runner.installTracer": return True raise RuntimeError("Unexpected argument to get_option: %s" % name) patched_config.get_option.side_effect = get_option rs = ReportSession(None, "", "", UploadedFileManager()) mock_script_runner = MagicMock() rs._scriptrunner = mock_script_runner mock_msg = MagicMock() rs.enqueue(mock_msg) func = mock_script_runner.maybe_handle_execution_control_request # In reality, outside of a testing environment func should be called # once. But in this test we're actually not installing a tracer here, # since Report is mocked. So the correct behavior here is for func to # never be called. If you ever see it being called once here it's # likely because there's a bug in the enqueue function (which should # skip func when installTracer is on). func.assert_not_called()