public void TestExperimentsRecording() { GleanInstance.SetExperimentActive( "experiment_test", "branch_a" ); GleanInstance.SetExperimentActive( "experiment_api", "branch_b", new Dictionary <string, string>() { { "test_key", "value" } } ); Assert.True(GleanInstance.TestIsExperimentActive("experiment_api")); Assert.True(GleanInstance.TestIsExperimentActive("experiment_test")); GleanInstance.SetExperimentInactive("experiment_test"); Assert.True(GleanInstance.TestIsExperimentActive("experiment_api")); Assert.False(GleanInstance.TestIsExperimentActive("experiment_test")); var storedData = GleanInstance.TestGetExperimentData("experiment_api"); Assert.Equal("branch_b", storedData.Branch); Assert.Single(storedData.Extra); Assert.Equal("value", storedData.Extra["test_key"]); }
public void TestExperimentsRecordingBeforeGleanInits() { // This test relies on Glean not being initialized and task queuing to be on. GleanInstance.TestDestroyGleanHandle(); Dispatchers.QueueInitialTasks = true; GleanInstance.SetExperimentActive( "experiment_set_preinit", "branch_a" ); GleanInstance.SetExperimentActive( "experiment_preinit_disabled", "branch_a" ); GleanInstance.SetExperimentInactive("experiment_preinit_disabled"); // This will init glean and flush the dispatcher's queue. string tempDataDir = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); GleanInstance.Reset( applicationId: "org.mozilla.csharp.tests", applicationVersion: "1.0-test", uploadEnabled: true, configuration: new Configuration(), dataDir: tempDataDir ); Assert.True(GleanInstance.TestIsExperimentActive("experiment_set_preinit")); Assert.False(GleanInstance.TestIsExperimentActive("experiment_preinit_disabled")); }
public void StartingAndStoppingATimerBeforeInitializationDoesNotCrash() { GleanInstance.TestDestroyGleanHandle(); Dispatchers.QueueInitialTasks = true; TimingDistributionMetricType metric = new TimingDistributionMetricType( disabled: false, category: "telemetry", lifetime: Lifetime.Ping, name: "timing_distribution", sendInPings: new string[] { "store1" }, timeUnit: TimeUnit.Second ); GleanTimerId timerId = metric.Start(); metric.StopAndAccumulate(timerId); // Start Glean again. string tempDataDir = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); GleanInstance.Initialize( applicationId: "org.mozilla.csharp.tests", applicationVersion: "1.0-test", uploadEnabled: true, configuration: new Configuration(), dataDir: tempDataDir ); Assert.True(metric.TestGetValue().Sum >= 0); }
/// <summary> /// Collect and submit the ping for eventual upload. /// /// While the collection of metrics into pings happens synchronously, the /// ping queuing and ping uploading happens asyncronously. /// There are no guarantees that this will happen immediately. /// /// If the ping currently contains no content, it will not be queued. /// </summary> /// <param name="reason">The reason code enum for ping submit.</param> public void Submit(ReasonCodesEnum?reason = null) { int enumValue = Convert.ToInt32(reason.GetValueOrDefault()); string reasonString = (reason != null && reasonCodes != null && reasonCodes.Length > 0) ? reasonCodes[enumValue] : null; GleanInstance.SubmitPing(this, reasonString); }
public LabeledMetricTypeTest() { // In xUnit, the constructor will be called before each test. This // feels like a natural place to initialize / reset Glean. GleanInstance.Reset( applicationId: "org.mozilla.csharp.tests", applicationVersion: "1.0-test", uploadEnabled: true, configuration: new Configuration(), dataDir: TempDataDir ); }
public void SettingMaxEventsDoesNotCrash() { string tempDataDir = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); GleanInstance.Reset( applicationId: "org.mozilla.csharp.tests", applicationVersion: "1.0-test", uploadEnabled: true, configuration: new Configuration(maxEvents: 500), dataDir: tempDataDir ); }
// Define a convenience function to manually reset Glean. private void ResetGlean(string dataDir) { GleanInstance.TestDestroyGleanHandle(); GleanInstance.Reset( applicationId: "org.mozilla.csharp.tests", applicationVersion: "1.0-test", uploadEnabled: true, configuration: new Configuration(httpClient: mockUploader), dataDir: dataDir ); }
static void Main(string[] args) { string gleanDataDir = Directory.GetCurrentDirectory() + "\\glean_data"; Console.WriteLine("Adding Glean data to {0}", gleanDataDir); GleanInstance.Initialize( applicationId: "org.mozilla.glean.csharp.sample", applicationVersion: "1.0", uploadEnabled: true, configuration: new Configuration(maxEvents: 37), dataDir: gleanDataDir ); }
public JweMetricTypeTest() { // Get a random test directory just for this single test. string tempDataDir = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); // In xUnit, the constructor will be called before each test. This // feels like a natural place to initialize / reset Glean. GleanInstance.Reset( applicationId: "org.mozilla.csharp.tests", applicationVersion: "1.0-test", uploadEnabled: true, configuration: new Configuration(), dataDir: tempDataDir ); }
public void TestOtherLabelWithoutPredefinedLabelsBeforeGleanInits() { CounterMetricType counterMetric = new CounterMetricType( disabled: false, category: "telemetry", lifetime: Lifetime.Application, name: "labeled_counter_metric", sendInPings: new string[] { "metrics" } ); var labeledCounterMetric = new LabeledMetricType <CounterMetricType>( disabled: false, category: "telemetry", lifetime: Lifetime.Application, name: "labeled_counter_metric", sendInPings: new string[] { "metrics" }, submetric: counterMetric ); // Make sure Glean isn't initialized, and turn task queueing on GleanInstance.TestDestroyGleanHandle(); Dispatchers.QueueInitialTasks = true; for (int i = 0; i <= 20; i++) { labeledCounterMetric[$"label_{i}"].Add(1); } // Go back and record in one of the real labels again labeledCounterMetric["label_0"].Add(1); // Initialize glean GleanInstance.Initialize( applicationId: "org.mozilla.csharp.tests", applicationVersion: "1.0-test", uploadEnabled: true, configuration: new Configuration(), dataDir: TempDataDir ); Assert.Equal(2, labeledCounterMetric["label_0"].TestGetValue()); for (int i = 1; i <= 15; i++) { Assert.Equal(1, labeledCounterMetric[$"label_{i}"].TestGetValue()); } Assert.Equal(5, labeledCounterMetric["__other__"].TestGetValue()); }
public TimingDistributionMetricTypeTest() { // Get a random test directory just for this single test. string tempDataDir = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); // Make sure to clear the HighPrecisionTimestamp mocked value between tests. HighPrecisionTimestamp.MockedValue = null; // In xUnit, the constructor will be called before each test. This // feels like a natural place to initialize / reset Glean. GleanInstance.Reset( applicationId: "org.mozilla.csharp.tests", applicationVersion: "1.0-test", uploadEnabled: true, configuration: new Configuration(), dataDir: tempDataDir ); }
public void GettingUploadEnabledBeforeInitShouldNotCrash() { // Can't use resetGlean directly GleanInstance.TestDestroyGleanHandle(); GleanInstance.SetUploadEnabled(true); Assert.True(GleanInstance.GetUploadEnabled()); string tempDataDir = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); GleanInstance.Initialize( applicationId: "org.mozilla.csharp.tests", applicationVersion: "1.0-test", uploadEnabled: true, configuration: new Configuration(), dataDir: tempDataDir ); }
public PingType( string name, bool includeClientId, bool sendIfEmpty, string[] reasonCodes ) { handle = LibGleanFFI.glean_new_ping_type( name: name, include_client_id: Convert.ToByte(includeClientId), send_if_empty: Convert.ToByte(sendIfEmpty), reason: reasonCodes, reason_codes_len: reasonCodes == null ? 0 : reasonCodes.Length ); this.name = name; this.reasonCodes = reasonCodes; GleanInstance.RegisterPingType(this); }
static void Main(string[] args) { string gleanDataDir = Path.Combine(Directory.GetCurrentDirectory(), "glean_data"); Console.WriteLine("Adding Glean data to {0}", gleanDataDir); GleanInstance.Initialize( applicationId: "org.mozilla.glean.csharp.sample", applicationVersion: "1.0", uploadEnabled: true, configuration: new Configuration(), dataDir: gleanDataDir ); CsharpTest.mystring.Set("test-string"); Pings.sample.Submit(); Console.WriteLine("Press any key to exit the sample..."); Console.ReadKey(); }
public void SendCustomPingsWithAnUnknownNameNoOp() { const string unknownPingName = "unknown"; Assert.False(GleanInstance.TestHasPingType(unknownPingName)); BooleanMetricType sampleMetric = new BooleanMetricType( disabled: false, category: "test", lifetime: Lifetime.Ping, name: "boolean_metric", sendInPings: new string[] { unknownPingName } ); sampleMetric.Set(true); Assert.True(sampleMetric.TestHasValue()); GleanInstance.SubmitPingByName(unknownPingName); // We don't expect any ping to be sent. Assert.Equal(0, mockUploader.GetUploadRequestCount()); }
// Start is called before the first frame update void Start() { // Redirect third party library logs to Unity panel. SystemConsoleRedirector.Redirect(); Console.WriteLine("Begin Glean test."); GleanInstance.Initialize( applicationId: "org.mycompany.glean.tests", applicationVersion: "0.1", uploadEnabled: true, configuration: new Configuration(channel: "debug", maxEvents: 500), dataDir: "data" ); // Create a custom ping. var ping = new PingType <NoReasonCodes>( name: "custom", includeClientId: true, sendIfEmpty: false, reasonCodes: null ); // Init launch ping. var metric = new StringMetricType( category: "hello_world", disabled: false, lifetime: Lifetime.Application, name: "test", sendInPings: new string[] { "custom" } ); metric.Set("my_data"); ping.Submit(); Console.WriteLine("End of Glean test."); }
public void EventsShouldNotRecordWhenUploadIsDisabled() { Private.EventMetricType <testNameKeys> eventMetric = new Private.EventMetricType <testNameKeys>( category: "ui", disabled: false, lifetime: Private.Lifetime.Ping, name: "event_metric", sendInPings: new string[] { "store1" }, allowedExtraKeys: new string[] { "test_name" } ); GleanInstance.SetUploadEnabled(true); eventMetric.Record(extra: new Dictionary <testNameKeys, string> { { testNameKeys.testName, "event1" } }); var snapshot1 = eventMetric.TestGetValue(); Assert.Single(snapshot1); GleanInstance.SetUploadEnabled(false); eventMetric.Record(extra: new Dictionary <testNameKeys, string> { { testNameKeys.testName, "event2" } }); try { eventMetric.TestGetValue(); Assert.True(false, "Expected events to be empty"); } catch (NullReferenceException) { } GleanInstance.SetUploadEnabled(true); eventMetric.Record(extra: new Dictionary <testNameKeys, string> { { testNameKeys.testName, "event3" } }); var snapshot3 = eventMetric.TestGetValue(); Assert.Single(snapshot3); }
public void SendAPing() { // TODO: This test needs a server to verify data are submitted successfully. // We should take `GleanTest.kt` as a reference to implement. GleanInstance.HandleBackgroundEvent(); }
public void RegistryShouldContainBuildInPings() { Assert.True(GleanInstance.TestHasPingType("metrics")); Assert.True(GleanInstance.TestHasPingType("events")); Assert.True(GleanInstance.TestHasPingType("baseline")); }
public void FlushQueuedEventsOnStartupAndCorrectlyHandlePreInitEvents() { string tempDataDir = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); // Re-initialize, we need to point this to the data directory we know of. ResetGlean(tempDataDir); Private.EventMetricType <SomeExtraKeys> eventMetric = new Private.EventMetricType <SomeExtraKeys>( category: "telemetry", disabled: false, lifetime: Private.Lifetime.Ping, name: "test_event", sendInPings: new string[] { "events" }, allowedExtraKeys: new string[] { "someExtra" } ); eventMetric.Record(extra: new Dictionary <SomeExtraKeys, string> { { SomeExtraKeys.SomeExtra, "run1" } }); Assert.Single(eventMetric.TestGetValue()); Dispatchers.QueueInitialTasks = true; eventMetric.Record(extra: new Dictionary <SomeExtraKeys, string> { { SomeExtraKeys.SomeExtra, "pre-init" } }); ResetGlean(tempDataDir); eventMetric.Record(extra: new Dictionary <SomeExtraKeys, string> { { SomeExtraKeys.SomeExtra, "post-init" } }); MockUploader.UploadRequest request = mockUploader.GetPendingUpload(); Assert.Equal("events", request.docType); // Check the content of the events ping. JsonDocument data = JsonDocument.Parse(request.payload); JsonElement root = data.RootElement; // This event comes from disk from the prior "run" Assert.Equal("startup", root.GetProperty("ping_info").GetProperty("reason").GetString()); JsonElement eventsProperty; Assert.True(root.TryGetProperty("events", out eventsProperty)); Assert.Equal(1, eventsProperty.GetArrayLength()); Assert.Equal("run1", eventsProperty.EnumerateArray().ElementAt(0).GetProperty("extra").GetProperty("someExtra").GetString() ); GleanInstance.SubmitPingByName("events", "background"); request = mockUploader.GetPendingUpload(); Assert.Equal("events", request.docType); data = JsonDocument.Parse(request.payload); root = data.RootElement; // This event comes from the pre-initialization event Assert.Equal("background", root.GetProperty("ping_info").GetProperty("reason").GetString()); Assert.True(root.TryGetProperty("events", out eventsProperty)); Assert.Equal(2, eventsProperty.GetArrayLength()); Assert.Equal("pre-init", eventsProperty.EnumerateArray().ElementAt(0).GetProperty("extra").GetProperty("someExtra").GetString() ); Assert.Equal("post-init", eventsProperty.EnumerateArray().ElementAt(1).GetProperty("extra").GetProperty("someExtra").GetString() ); }