Skip to content

Commit

Permalink
In tests register all pings by instantiating them
Browse files Browse the repository at this point in the history
  • Loading branch information
badboy committed Nov 19, 2024
1 parent 2b80301 commit e0d62dd
Show file tree
Hide file tree
Showing 17 changed files with 141 additions and 20 deletions.
1 change: 1 addition & 0 deletions .swiftlint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ disabled_rules:
- force_cast
# We'll get to these when we get to them!
- todo
- function_body_length

identifier_name:
# Turn off it complaining about `id` or `let t = title`, etc, but keep
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import androidx.work.testing.WorkManagerTestInitHelper
import mozilla.telemetry.glean.BuildInfo
import mozilla.telemetry.glean.Glean
import mozilla.telemetry.glean.config.Configuration
import mozilla.telemetry.glean.private.NoReasonCodes
import mozilla.telemetry.glean.private.PingType
import mozilla.telemetry.glean.private.TimeUnit
import mozilla.telemetry.glean.scheduler.MetricsPingScheduler
import mozilla.telemetry.glean.utils.getISOTimeString
Expand Down Expand Up @@ -68,6 +70,37 @@ class GleanTestRule(

mps.updateSentDate(getISOTimeString(fakeNow, truncateTo = TimeUnit.DAY))

PingType<NoReasonCodes>(
name = "store1",
includeClientId = true,
sendIfEmpty = false,
preciseTimestamps = true,
includeInfoSections = true,
enabled = true,
schedulesPings = emptyList(),
reasonCodes = emptyList(),
)
PingType<NoReasonCodes>(
name = "store2",
includeClientId = true,
sendIfEmpty = false,
preciseTimestamps = true,
includeInfoSections = true,
enabled = true,
schedulesPings = emptyList(),
reasonCodes = emptyList(),
)
PingType<NoReasonCodes>(
name = "store3",
includeClientId = true,
sendIfEmpty = false,
preciseTimestamps = true,
includeInfoSections = true,
enabled = true,
schedulesPings = emptyList(),
reasonCodes = emptyList(),
)

Glean.resetGlean(
context = context,
config = configToUse,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,21 @@ class CustomPingTest {

val pingName = "custom-events-1"

// Define a new custom ping inline.
// This will register the ping as well.
// Ususally this happens in user code by calling `Glean.registerPings(Pings)`
@Suppress("UNUSED_VARIABLE")
val customPing = PingType<NoReasonCodes>(
name = pingName,
includeClientId = true,
sendIfEmpty = true,
preciseTimestamps = true,
includeInfoSections = true,
enabled = true,
schedulesPings = emptyList(),
reasonCodes = emptyList(),
)

// Define a 'click' event
val click = EventMetricType<NoExtras>(
CommonMetricData(
Expand All @@ -178,21 +193,6 @@ class CustomPingTest {
// We need to simulate that the app is shutdown and all resources are freed.
Glean.testDestroyGleanHandle()

// Define a new custom ping inline.
// This will register the ping as well.
// Ususally this happens in user code by calling `Glean.registerPings(Pings)`
@Suppress("UNUSED_VARIABLE")
val customPing = PingType<NoReasonCodes>(
name = pingName,
includeClientId = true,
sendIfEmpty = true,
preciseTimestamps = true,
includeInfoSections = true,
enabled = true,
schedulesPings = emptyList(),
reasonCodes = emptyList(),
)

// The PingUploadWorker might be queued for at-init reasons, so to ensure
// init didn't submit this custom ping we submit it deliberately only once,
// and assert that we didn't receive it twice.
Expand Down
31 changes: 31 additions & 0 deletions glean-core/ios/GleanTests/TestUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,37 @@ func resetGleanDiscardingInitialPings(testCase: XCTestCase,
let mps = MetricsPingScheduler(true)
mps.updateSentDate(Date())

_ = Ping<NoReasonCodes>(
name: "store1",
includeClientId: true,
sendIfEmpty: false,
preciseTimestamps: true,
includeInfoSections: true,
enabled: true,
schedulesPings: [],
reasonCodes: []
)
_ = Ping<NoReasonCodes>(
name: "store2",
includeClientId: true,
sendIfEmpty: false,
preciseTimestamps: true,
includeInfoSections: true,
enabled: true,
schedulesPings: [],
reasonCodes: []
)
_ = Ping<NoReasonCodes>(
name: "store3",
includeClientId: true,
sendIfEmpty: false,
preciseTimestamps: true,
includeInfoSections: true,
enabled: true,
schedulesPings: [],
reasonCodes: []
)

Glean.shared.resetGlean(configuration: configuration, clearStores: clearStores)

testCase.waitForExpectations(timeout: 5.0) { error in
Expand Down
28 changes: 28 additions & 0 deletions glean-core/python/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from glean import config
from glean import testing
from glean import __version__ as glean_version
from glean.metrics import PingType

# This defines the location of the JSON schema used to validate the pings
# created during unit testing. This uses the vendored schema.
Expand All @@ -30,6 +31,33 @@

# This will be run before every test in the entire test suite
def pytest_runtest_setup(item):
PingType(
name="store1",
include_client_id=True,
send_if_empty=False,
precise_timestamps=True,
include_info_sections=True,
schedules_pings=[],
reason_codes=[],
)
PingType(
name="store2",
include_client_id=True,
send_if_empty=False,
precise_timestamps=True,
include_info_sections=True,
schedules_pings=[],
reason_codes=[],
)
PingType(
name="store3",
include_client_id=True,
send_if_empty=False,
precise_timestamps=True,
include_info_sections=True,
schedules_pings=[],
reason_codes=[],
)
testing.reset_glean(application_id="glean-python-test", application_version=glean_version)


Expand Down
1 change: 1 addition & 0 deletions glean-core/rlb/examples/crashing-threads.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ fn main() {
root.path().to_path_buf()
};

_ = &*PrototypePing;
let cfg = ConfigurationBuilder::new(true, data_path, "org.mozilla.glean_core.example")
.with_server_endpoint("invalid-test-host")
.with_use_core_mps(true)
Expand Down
1 change: 1 addition & 0 deletions glean-core/rlb/examples/delayed-ping-data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ fn main() {
let data_path = PathBuf::from(args.next().expect("need data path"));
let state = args.next().unwrap_or_default();

_ = &*PrototypePing;
let uploader = MovingUploader::new(data_path.clone());
let cfg = ConfigurationBuilder::new(true, data_path, "glean.pingflush")
.with_server_endpoint("invalid-test-host")
Expand Down
1 change: 1 addition & 0 deletions glean-core/rlb/examples/long-running.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ fn main() {

let data_path = PathBuf::from(args.next().expect("need data path"));

_ = &*PrototypePing;
let cfg = ConfigurationBuilder::new(true, data_path, "glean.longrunning")
.with_server_endpoint("invalid-test-host")
.with_use_core_mps(false)
Expand Down
1 change: 1 addition & 0 deletions glean-core/rlb/examples/ping-lifetime-flush.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ fn main() {
let data_path = PathBuf::from(args.next().expect("need data path"));
let state = args.next().unwrap_or_default();

_ = &*PrototypePing;
let uploader = MovingUploader::new(data_path.clone());
let cfg = ConfigurationBuilder::new(true, data_path, "glean.pingflush")
.with_server_endpoint("invalid-test-host")
Expand Down
1 change: 1 addition & 0 deletions glean-core/rlb/examples/prototype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ fn main() {
root.path().to_path_buf()
};

_ = &*PrototypePing;
let cfg = ConfigurationBuilder::new(true, data_path, "org.mozilla.glean_core.example")
.with_server_endpoint("invalid-test-host")
.with_use_core_mps(true)
Expand Down
3 changes: 3 additions & 0 deletions glean-core/rlb/src/common_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

use crate::private::PingType;
use crate::ClientInfoMetrics;
use crate::{Configuration, ConfigurationBuilder};
use std::sync::{Mutex, MutexGuard};
Expand Down Expand Up @@ -45,6 +46,8 @@ pub(crate) fn new_glean(
.build(),
};

_ = PingType::new("store1", true, true, true, true, true, vec![], vec![]);

crate::test_reset_glean(cfg, ClientInfoMetrics::unknown(), clear_stores);
dir
}
3 changes: 2 additions & 1 deletion glean-core/rlb/tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

use std::{panic, process};

use glean::{ClientInfoMetrics, Configuration};
use glean::{private::PingType, ClientInfoMetrics, Configuration};

/// Initialize the env logger for a test environment.
///
Expand Down Expand Up @@ -48,5 +48,6 @@ pub fn initialize(cfg: Configuration) {
locale: Some("xx-XX".to_string()),
};

_ = PingType::new("store1", true, true, true, true, true, vec![], vec![]);
glean::initialize(cfg, client_info);
}
1 change: 1 addition & 0 deletions glean-core/rlb/tests/init_fails.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ fn init_fails() {
let dir = tempfile::tempdir().unwrap();
let tmpname = dir.path().to_path_buf();

_ = &*pings::validation;
let cfg = ConfigurationBuilder::new(true, tmpname, "")
.with_server_endpoint("invalid-test-host")
.build();
Expand Down
1 change: 1 addition & 0 deletions glean-core/rlb/tests/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ fn simple_lifecycle() {
let dir = tempfile::tempdir().unwrap();
let tmpname = dir.path().to_path_buf();

_ = &*pings::validation;
let cfg = ConfigurationBuilder::new(true, tmpname, "firefox-desktop")
.with_server_endpoint("invalid-test-host")
.build();
Expand Down
11 changes: 10 additions & 1 deletion glean-core/src/lib_unit_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

use std::collections::HashSet;

use internal_pings::InternalPings;
use serde_json::json;

use super::*;
Expand All @@ -19,7 +20,15 @@ pub fn new_glean(tempdir: Option<tempfile::TempDir>) -> (Glean, tempfile::TempDi
None => tempfile::tempdir().unwrap(),
};
let tmpname = dir.path().display().to_string();
let glean = Glean::with_options(&tmpname, GLOBAL_APPLICATION_ID, true, true);
let mut glean = Glean::with_options(&tmpname, GLOBAL_APPLICATION_ID, true, true);
// Register the builtin pings as enabled.
_ = InternalPings::new(true);

// store{1, 2} is used throughout tests
let ping = PingType::new_internal("store1", true, false, true, true, true, vec![], vec![]);
glean.register_ping_type(&ping);
let ping = PingType::new_internal("store2", true, false, true, true, true, vec![], vec![]);
glean.register_ping_type(&ping);
(glean, dir)
}

Expand Down
10 changes: 8 additions & 2 deletions glean-core/tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// #[allow(dead_code)] is required on this module as a workaround for
// https://github.com/rust-lang/rust/issues/46379
#![allow(dead_code)]
use glean_core::{Glean, Result};
use glean_core::{Glean, PingType, Result};

use std::fs::{read_dir, File};
use std::io::{BufRead, BufReader};
Expand Down Expand Up @@ -68,7 +68,13 @@ pub fn new_glean(tempdir: Option<tempfile::TempDir>) -> (Glean, tempfile::TempDi
ping_lifetime_threshold: 0,
ping_lifetime_max_time: 0,
};
let glean = Glean::new(cfg).unwrap();
let mut glean = Glean::new(cfg).unwrap();

// store{1,2} is used throughout tests
let ping = PingType::new("store1", true, false, true, true, true, vec![], vec![]);
glean.register_ping_type(&ping);
let ping = PingType::new("store2", true, false, true, true, true, vec![], vec![]);
glean.register_ping_type(&ping);

(glean, dir)
}
Expand Down
4 changes: 3 additions & 1 deletion glean-core/tests/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,9 @@ fn with_event_timestamps() {
ping_lifetime_threshold: 0,
ping_lifetime_max_time: 0,
};
let glean = Glean::new(cfg).unwrap();
let mut glean = Glean::new(cfg).unwrap();
let ping = PingType::new("store1", true, false, true, true, true, vec![], vec![]);
glean.register_ping_type(&ping);

let store_name = "store1";
let event = EventMetric::new(
Expand Down

0 comments on commit e0d62dd

Please sign in to comment.