|
| 1 | +/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 | + |
| 5 | +import assert from "assert"; |
| 6 | +import { Context } from "../../../src/core/context"; |
| 7 | + |
| 8 | +import Glean from "../../../src/core/glean"; |
| 9 | +import { Lifetime } from "../../../src/core/metrics/lifetime"; |
| 10 | +import QuantityMetricType from "../../../src/core/metrics/types/quantity"; |
| 11 | + |
| 12 | +describe("QuantityMetric", function() { |
| 13 | + const testAppId = `gleanjs.test.${this.title}`; |
| 14 | + |
| 15 | + beforeEach(async function() { |
| 16 | + await Glean.testResetGlean(testAppId); |
| 17 | + }); |
| 18 | + |
| 19 | + it("attempting to get the value of a metric that hasn't been recorded doesn't error", async function() { |
| 20 | + const metric = new QuantityMetricType({ |
| 21 | + category: "aCategory", |
| 22 | + name: "aQuantityMetric", |
| 23 | + sendInPings: ["aPing", "twoPing", "threePing"], |
| 24 | + lifetime: Lifetime.Ping, |
| 25 | + disabled: false |
| 26 | + }); |
| 27 | + |
| 28 | + assert.strictEqual(await metric.testGetValue("aPing"), undefined); |
| 29 | + }); |
| 30 | + |
| 31 | + it("attempting to set when glean upload is disabled is a no-op", async function() { |
| 32 | + Glean.setUploadEnabled(false); |
| 33 | + |
| 34 | + const metric = new QuantityMetricType({ |
| 35 | + category: "aCategory", |
| 36 | + name: "aQuantityMetric", |
| 37 | + sendInPings: ["aPing", "twoPing", "threePing"], |
| 38 | + lifetime: Lifetime.Ping, |
| 39 | + disabled: false |
| 40 | + }); |
| 41 | + |
| 42 | + metric.set(10); |
| 43 | + assert.strictEqual(await metric.testGetValue("aPing"), undefined); |
| 44 | + }); |
| 45 | + |
| 46 | + it("ping payload is correct", async function() { |
| 47 | + const metric = new QuantityMetricType({ |
| 48 | + category: "aCategory", |
| 49 | + name: "aQuantityMetric", |
| 50 | + sendInPings: ["aPing"], |
| 51 | + lifetime: Lifetime.Ping, |
| 52 | + disabled: false |
| 53 | + }); |
| 54 | + |
| 55 | + metric.set(10); |
| 56 | + assert.strictEqual(await metric.testGetValue("aPing"), 10); |
| 57 | + |
| 58 | + const snapshot = await Context.metricsDatabase.getPingMetrics("aPing", true); |
| 59 | + assert.deepStrictEqual(snapshot, { |
| 60 | + "quantity": { |
| 61 | + "aCategory.aQuantityMetric": 10 |
| 62 | + } |
| 63 | + }); |
| 64 | + }); |
| 65 | + |
| 66 | + it("set properly sets the value in all pings", async function() { |
| 67 | + const metric = new QuantityMetricType({ |
| 68 | + category: "aCategory", |
| 69 | + name: "aQuantityMetric", |
| 70 | + sendInPings: ["aPing", "twoPing", "threePing"], |
| 71 | + lifetime: Lifetime.Ping, |
| 72 | + disabled: false |
| 73 | + }); |
| 74 | + |
| 75 | + metric.set(0); |
| 76 | + assert.strictEqual(await metric.testGetValue("aPing"), 0); |
| 77 | + assert.strictEqual(await metric.testGetValue("twoPing"), 0); |
| 78 | + assert.strictEqual(await metric.testGetValue("threePing"), 0); |
| 79 | + }); |
| 80 | + |
| 81 | + it("must not set when passed negative", async function() { |
| 82 | + const metric = new QuantityMetricType({ |
| 83 | + category: "aCategory", |
| 84 | + name: "aQuantityMetric", |
| 85 | + sendInPings: ["aPing"], |
| 86 | + lifetime: Lifetime.Ping, |
| 87 | + disabled: false |
| 88 | + }); |
| 89 | + |
| 90 | + metric.set(-1); |
| 91 | + assert.strictEqual(await metric.testGetValue("aPing"), undefined); |
| 92 | + }); |
| 93 | + |
| 94 | + it("saturates at boundary", async function() { |
| 95 | + const metric = new QuantityMetricType({ |
| 96 | + category: "aCategory", |
| 97 | + name: "aQuantityMetric", |
| 98 | + sendInPings: ["aPing"], |
| 99 | + lifetime: Lifetime.Ping, |
| 100 | + disabled: false |
| 101 | + }); |
| 102 | + |
| 103 | + metric.set(Number.MAX_SAFE_INTEGER+1); |
| 104 | + assert.strictEqual(await metric.testGetValue("aPing"), Number.MAX_SAFE_INTEGER); |
| 105 | + }); |
| 106 | +}); |
0 commit comments