Ejemplo n.º 1
0
def test_queued_recorded_metrics_correctly_during_init():
    Glean.reset()

    # Enable queueing
    Dispatcher.set_task_queueing(True)

    counter_metric = CounterMetricType(
        disabled=False,
        category="telemetry",
        lifetime=Lifetime.APPLICATION,
        name="counter_metric",
        send_in_pings=["store1"],
    )

    for i in range(2):
        counter_metric.add()

    Glean.initialize(
        application_id=GLEAN_APP_ID,
        application_version=glean_version,
        upload_enabled=True,
    )

    assert counter_metric.test_has_value()
    assert 2 == counter_metric.test_get_value()
Ejemplo n.º 2
0
def reset_glean(*,
                application_id: str,
                application_version: str,
                configuration: Optional[Configuration] = None,
                clear_stores: bool = True):
    """
    Resets the Glean singleton.

    Args:
        application_id (str): The application id to use when sending pings.
        application_version (str): The version of the application sending
            Glean data.
        configuration (glean.config.Configuration): (optional) An object with
            global settings.
    """
    from glean import Glean

    data_dir = None  # type: Optional[Path]
    if not clear_stores:
        Glean._destroy_data_dir = False
        data_dir = Glean._data_dir

    Glean.reset()
    Glean.initialize(
        application_id=application_id,
        application_version=application_version,
        upload_enabled=True,
        configuration=configuration,
        data_dir=data_dir,
    )
Ejemplo n.º 3
0
def test_other_label_without_predefined_labels_before_glean_init():
    labeled_counter_metric = metrics.LabeledCounterMetricType(
        disabled=False,
        category="telemetry",
        lifetime=Lifetime.APPLICATION,
        name="labeled_counter_metric",
        send_in_pings=["metrics"],
    )

    Glean.reset()
    Dispatcher.set_task_queueing(True)

    for i in range(21):
        labeled_counter_metric["label_{}".format(i)].add(1)
    labeled_counter_metric["label_0"].add(1)

    Glean.initialize(
        application_id="glean-python-test",
        application_version=glean_version,
        upload_enabled=True,
    )

    assert 2 == labeled_counter_metric["label_0"].test_get_value()
    for i in range(1, 16):
        assert 1 == labeled_counter_metric["label_{}".format(
            i)].test_get_value()
    assert 5 == labeled_counter_metric["__other__"].test_get_value()
Ejemplo n.º 4
0
def test_setting_upload_enabled_before_initialization_should_not_crash():
    Glean.reset()
    Glean.set_upload_enabled(True)
    Glean.initialize(
        application_id=GLEAN_APP_ID,
        application_version=glean_version,
        upload_enabled=True,
    )
Ejemplo n.º 5
0
def test_set_application_id_and_version():
    Glean.reset()

    Glean.initialize(application_id="my-id", application_version="my-version")

    assert ("my-id" == _builtins.metrics.glean.internal.metrics.app_build.
            test_get_value())
    assert ("my-version" == _builtins.metrics.glean.internal.metrics.
            app_display_version.test_get_value())
Ejemplo n.º 6
0
def test_the_app_channel_must_be_correctly_set():
    Glean.reset()
    Glean.initialize(
        application_id=GLEAN_APP_ID,
        application_version=glean_version,
        configuration=Configuration(channel="my-test-channel"),
    )
    assert ("my-test-channel" == _builtins.metrics.glean.internal.metrics.
            app_channel.test_get_value())
Ejemplo n.º 7
0
def test_experiments_recording_before_glean_inits():
    # This test relies on Glean not being initialized and task
    # queuing to be on.
    Glean.reset()

    Glean.set_experiment_active("experiment_set_preinit", "branch_a")
    Glean.set_experiment_active("experiment_preinit_disabled", "branch_a")

    Glean.set_experiment_inactive("experiment_preinit_disabled")

    # This will init Glean and flush the dispatcher's queue.
    Glean.initialize(application_id=GLEAN_APP_ID,
                     application_version=glean_version)

    assert Glean.test_is_experiment_active("experiment_set_preinit")
    assert not Glean.test_is_experiment_active("experiment_preinit_disabled")
Ejemplo n.º 8
0
def test_initialize_must_not_crash_if_data_dir_is_messed_up(tmpdir):
    filename = tmpdir / "dummy_file"

    # Create a file in a temporary directory
    with open(filename, "w") as fd:
        fd.write("Contents\n")

    Glean.reset()
    assert False is Glean.is_initialized()

    # Pass in the filename as the data_dir
    Glean.initialize(
        application_id=GLEAN_APP_ID,
        application_version=glean_version,
        data_dir=filename,
    )

    # This should cause initialization to fail
    assert False is Glean.is_initialized()

    shutil.rmtree(tmpdir)
Ejemplo n.º 9
0
def test_tempdir_is_cleared():
    tempdir = Glean._data_dir

    Glean.reset()

    assert not tempdir.exists()