def test_serialize_final_report(self): report = Report("/not/a/script.py", "") _enqueue(report, INIT_MSG) _enqueue(report, TEXT_DELTA_MSG) _enqueue(report, EMPTY_DELTA_MSG) files = report.serialize_final_report_to_files() # Validate our messages. messages = [_parse_msg(msg_string) for _, msg_string in files[:-1]] self.assertEqual(3, len(messages)) self.assertEqual("initialize", messages[0].WhichOneof("type")) self.assertEqual("text1", messages[1].delta.new_element.text.body) self.assertEqual("empty", messages[2].delta.new_element.WhichOneof("type")) # Validate the manifest, which should be the final file. _, manifest_string = files[-1] manifest = StaticManifest() manifest.ParseFromString(manifest_string) self.assertEqual("script", manifest.name) self.assertEqual(3, manifest.num_messages) self.assertEqual(StaticManifest.DONE, manifest.server_status) self.assertEqual("", manifest.external_server_ip) self.assertEqual("", manifest.internal_server_ip) self.assertEqual(0, manifest.server_port)
def test_serialize_running_report(self): report = Report("/not/a/script.py", "") _enqueue(report, INIT_MSG) _enqueue(report, EMPTY_DELTA_MSG) _enqueue(report, TEXT_DELTA_MSG) _enqueue(report, EMPTY_DELTA_MSG) get_external_ip_patch = patch( "streamlit.Report.net_util.get_external_ip", return_value="external_ip") get_internal_ip_patch = patch( "streamlit.Report.net_util.get_internal_ip", return_value="internal_ip") with get_external_ip_patch, get_internal_ip_patch: files = report.serialize_running_report_to_files() # Running reports just serialize the manifest self.assertEqual(1, len(files)) # Validate the manifest. _, manifest_string = files[-1] manifest = StaticManifest() manifest.ParseFromString(manifest_string) self.assertEqual("script", manifest.name) self.assertEqual(0, manifest.num_messages) self.assertEqual(StaticManifest.RUNNING, manifest.server_status) self.assertEqual( config.get_option("browser.serverAddress"), manifest.configured_server_address, ) self.assertEqual(config.get_option("browser.serverPort"), manifest.server_port) self.assertEqual("external_ip", manifest.external_server_ip) self.assertEqual("internal_ip", manifest.internal_server_ip)
def _build_manifest( self, status, num_messages=None, external_server_ip=None, internal_server_ip=None, ): """Build a manifest dict for this report. Parameters ---------- status : StaticManifest.ServerStatus The report status. If the script is still executing, then the status should be RUNNING. Otherwise, DONE. num_messages : int or None Set only when status is DONE. The number of ForwardMsgs that this report is made of. external_server_ip : str or None Only when status is RUNNING. The IP of the Server's websocket. internal_server_ip : str or None Only when status is RUNNING. The IP of the Server's websocket. Returns ------- StaticManifest A StaticManifest protobuf message """ manifest = StaticManifest() manifest.name = self.name manifest.server_status = status if status == StaticManifest.RUNNING: manifest.external_server_ip = external_server_ip manifest.internal_server_ip = internal_server_ip manifest.configured_server_address = config.get_option( "browser.serverAddress" ) # Don't use _get_browser_address_bar_port() here, since we want the # websocket port, not the web server port. (These are the same in # prod, but different in dev) manifest.server_port = config.get_option("browser.serverPort") manifest.server_base_path = config.get_option("server.baseUrlPath") else: manifest.num_messages = num_messages return manifest