def test_sending_deletion_ping_if_disabled_outside_of_run( tmpdir, ping_schema_url): info_path = Path(str(tmpdir)) / "info.txt" data_dir = Path(str(tmpdir)) / "glean" Glean._reset() Glean.initialize( application_id=GLEAN_APP_ID, application_version=glean_version, upload_enabled=True, data_dir=data_dir, configuration=Configuration( ping_uploader=_RecordingUploader(info_path)), ) Glean._reset() Glean.initialize( application_id=GLEAN_APP_ID, application_version=glean_version, upload_enabled=False, data_dir=data_dir, configuration=Configuration( ping_uploader=_RecordingUploader(info_path)), ) while not info_path.exists(): time.sleep(0.1) with info_path.open("r") as fd: url_path = fd.readline() serialized_ping = fd.readline() assert "deletion-request" == url_path.split("/")[3] json_content = json.loads(serialized_ping) assert 0 == validate_ping.validate_ping( io.StringIO(serialized_ping), sys.stdout, schema_url=ping_schema_url, ) assert not json_content["client_info"]["client_id"].startswith("c0ffee")
def test_presubmit_makes_a_valid_ping(tmpdir, ping_schema_url, monkeypatch): # Bug 1648140: Submitting a ping prior to initialize meant that the core # metrics wouldn't yet be set. info_path = Path(str(tmpdir)) / "info.txt" Glean._reset() ping_name = "preinit_ping" ping = PingType(name=ping_name, include_client_id=True, send_if_empty=True, reason_codes=[]) # This test relies on testing mode to be disabled, since we need to prove the # real-world async behaviour of this. Dispatcher._testing_mode = False Dispatcher._queue_initial_tasks = True # Submit a ping prior to calling initialize ping.submit() Glean._initialize_with_tempdir_for_testing( application_id=GLEAN_APP_ID, application_version=glean_version, upload_enabled=True, configuration=Glean._configuration, ) monkeypatch.setattr(Glean._configuration, "ping_uploader", _RecordingUploader(info_path)) # Wait until the work is complete Dispatcher._task_worker._queue.join() while not info_path.exists(): time.sleep(0.1) with info_path.open("r") as fd: url_path = fd.readline() serialized_ping = fd.readline() print(url_path) assert ping_name == url_path.split("/")[3] assert 0 == validate_ping.validate_ping( io.StringIO(serialized_ping), sys.stdout, schema_url=ping_schema_url, )
def test_client_activity_api(tmpdir, monkeypatch): Glean._reset() info_path = Path(str(tmpdir)) / "info.txt" # This test relies on testing mode to be disabled, since we need to prove the # real-world async behaviour of this. configuration = Glean._configuration configuration.ping_uploader = _RecordingUploader(info_path) Glean._initialize_with_tempdir_for_testing( application_id=GLEAN_APP_ID, application_version=glean_version, upload_enabled=True, configuration=Glean._configuration, ) # Wait until the work is complete Dispatcher._task_worker._queue.join() # Making it active Glean.handle_client_active() url_path, payload = wait_for_ping(info_path) assert "baseline" == url_path.split("/")[3] assert payload["ping_info"]["reason"] == "active" assert "timespan" not in payload["metrics"] # Making it inactive Glean.handle_client_inactive() url_path, payload = wait_for_ping(info_path) assert "baseline" == url_path.split("/")[3] assert payload["ping_info"]["reason"] == "inactive" assert "glean.baseline.duration" in payload["metrics"]["timespan"] # Once more active Glean.handle_client_active() url_path, payload = wait_for_ping(info_path) assert "baseline" == url_path.split("/")[3] assert payload["ping_info"]["reason"] == "active" assert "timespan" not in payload["metrics"]
def test_flipping_upload_enabled_respects_order_of_events(tmpdir, monkeypatch): Glean._reset() info_path = Path(str(tmpdir)) / "info.txt" # We create a ping and a metric before we initialize Glean ping = PingType( name="sample_ping_1", include_client_id=True, send_if_empty=True, reason_codes=[], ) # This test relies on testing mode to be disabled, since we need to prove the # real-world async behaviour of this. Dispatcher._testing_mode = False Dispatcher._queue_initial_tasks = True configuration = Glean._configuration configuration.ping_uploader = _RecordingUploader(info_path) Glean._initialize_with_tempdir_for_testing( application_id=GLEAN_APP_ID, application_version=glean_version, upload_enabled=True, configuration=Glean._configuration, ) # Glean might still be initializing. Disable upload. Glean.set_upload_enabled(False) # Submit a custom ping. ping.submit() # Wait until the work is complete Dispatcher._task_worker._queue.join() while not info_path.exists(): time.sleep(0.1) with info_path.open("r") as fd: url_path = fd.readline() # Validate we got the deletion-request ping assert "deletion-request" == url_path.split("/")[3]
def test_ping_collection_must_happen_after_currently_scheduled_metrics_recordings( tmpdir, ping_schema_url, ): # Given the following block of code: # # metrics.metric.a.set("SomeTestValue") # Glean.submit_pings(["custom-ping-1"]) # # This test ensures that "custom-ping-1" contains "metric.a" with a value of "SomeTestValue" # when the ping is collected. info_path = Path(str(tmpdir)) / "info.txt" real_uploader = Glean._configuration.ping_uploader test_uploader = _RecordingUploader(info_path) Glean._configuration.ping_uploader = test_uploader Glean._configuration.log_pings = True ping_name = "custom_ping_1" ping = PingType( name=ping_name, include_client_id=True, send_if_empty=False, reason_codes=[] ) string_metric = StringMetricType( disabled=False, category="category", lifetime=Lifetime.PING, name="string_metric", send_in_pings=[ping_name], ) # This test relies on testing mode to be disabled, since we need to prove the # real-world async behaviour of this. Dispatcher._testing_mode = False # This is the important part of the test. Even though both the metrics API and # sendPings are async and off the main thread, "SomeTestValue" should be recorded, # the order of the calls must be preserved. test_value = "SomeTestValue" string_metric.set(test_value) ping.submit() # Wait until the work is complete Dispatcher._task_worker._queue.join() while not info_path.exists(): time.sleep(0.1) with info_path.open("r") as fd: url_path = fd.readline() serialized_ping = fd.readline() assert ping_name == url_path.split("/")[3] json_content = json.loads(serialized_ping) assert 0 == validate_ping.validate_ping( io.StringIO(serialized_ping), sys.stdout, schema_url=ping_schema_url, ) assert {"category.string_metric": test_value} == json_content["metrics"]["string"] Glean._configuration.ping_uploader = real_uploader