33 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
44
55import assert from "assert" ;
6- import { Context } from "../../../../src/core/context" ;
76import { ErrorType } from "../../../../src/core/error/error_type" ;
87
98import Glean from "../../../../src/core/glean" ;
109import { Lifetime } from "../../../../src/core/metrics/lifetime" ;
11- import QuantityMetricType from "../../../../src/core/metrics/types/quantity " ;
10+ import RateMetricType from "../../../../src/core/metrics/types/rate " ;
1211
1312describe ( "RateMetric" , function ( ) {
1413 const testAppId = `gleanjs.test.${ this . title } ` ;
@@ -17,93 +16,32 @@ describe("RateMetric", function() {
1716 await Glean . testResetGlean ( testAppId ) ;
1817 } ) ;
1918
20- it ( "attempting to get the value of a metric that hasn't been recorded doesn't error" , async function ( ) {
21- const metric = new QuantityMetricType ( {
22- category : "aCategory" ,
23- name : "aQuantityMetric" ,
24- sendInPings : [ "aPing" , "twoPing" , "threePing" ] ,
25- lifetime : Lifetime . Ping ,
26- disabled : false
27- } ) ;
28-
29- assert . strictEqual ( await metric . testGetValue ( "aPing" ) , undefined ) ;
30- } ) ;
31-
32- it ( "attempting to set when glean upload is disabled is a no-op" , async function ( ) {
33- Glean . setUploadEnabled ( false ) ;
34-
35- const metric = new QuantityMetricType ( {
36- category : "aCategory" ,
37- name : "aQuantityMetric" ,
38- sendInPings : [ "aPing" , "twoPing" , "threePing" ] ,
39- lifetime : Lifetime . Ping ,
40- disabled : false
41- } ) ;
42-
43- metric . set ( 10 ) ;
44- assert . strictEqual ( await metric . testGetValue ( "aPing" ) , undefined ) ;
45- } ) ;
19+ it ( "smoke test for rate metric" , async function ( ) {
20+ Glean . setUploadEnabled ( true ) ;
4621
47- it ( "ping payload is correct" , async function ( ) {
48- const metric = new QuantityMetricType ( {
22+ const metric = new RateMetricType ( {
4923 category : "aCategory" ,
50- name : "aQuantityMetric " ,
24+ name : "aRate " ,
5125 sendInPings : [ "aPing" ] ,
5226 lifetime : Lifetime . Ping ,
5327 disabled : false
5428 } ) ;
5529
56- metric . set ( 10 ) ;
57- assert . strictEqual ( await metric . testGetValue ( "aPing" ) , 10 ) ;
30+ metric . add_to_numerator ( 0 ) ;
31+ metric . add_to_denominator ( 0 ) ;
5832
59- const snapshot = await Context . metricsDatabase . getPingMetrics ( "aPing" , true ) ;
60- assert . deepStrictEqual ( snapshot , {
61- "quantity" : {
62- "aCategory.aQuantityMetric" : 10
63- }
64- } ) ;
65- } ) ;
33+ assert . strictEqual ( await metric . testGetNumRecordedErrors ( ErrorType . InvalidValue ) , 0 ) ;
6634
67- it ( "set properly sets the value in all pings" , async function ( ) {
68- const metric = new QuantityMetricType ( {
69- category : "aCategory" ,
70- name : "aQuantityMetric" ,
71- sendInPings : [ "aPing" , "twoPing" , "threePing" ] ,
72- lifetime : Lifetime . Ping ,
73- disabled : false
74- } ) ;
35+ metric . add_to_numerator ( - 1 ) ;
36+ metric . add_to_denominator ( - 1 ) ;
7537
76- metric . set ( 0 ) ;
77- assert . strictEqual ( await metric . testGetValue ( "aPing" ) , 0 ) ;
78- assert . strictEqual ( await metric . testGetValue ( "twoPing" ) , 0 ) ;
79- assert . strictEqual ( await metric . testGetValue ( "threePing" ) , 0 ) ;
80- } ) ;
38+ assert . strictEqual ( await metric . testGetNumRecordedErrors ( ErrorType . InvalidValue ) , 2 ) ;
8139
82- it ( "must not set when passed negative" , async function ( ) {
83- const metric = new QuantityMetricType ( {
84- category : "aCategory" ,
85- name : "aQuantityMetric" ,
86- sendInPings : [ "aPing" ] ,
87- lifetime : Lifetime . Ping ,
88- disabled : false
89- } ) ;
40+ assert . deepStrictEqual ( await metric . testGetValue ( "aPing" ) , { numerator : 0 , denominator : 0 } ) ;
9041
91- metric . set ( - 1 ) ;
92- assert . strictEqual ( await metric . testGetValue ( "aPing" ) , undefined ) ;
42+ metric . add_to_numerator ( 22 ) ;
43+ metric . add_to_denominator ( 7 ) ;
9344
94- assert . strictEqual ( await metric . testGetNumRecordedErrors ( ErrorType . InvalidValue , "aPing" ) , 1 ) ;
95- } ) ;
96-
97- it ( "saturates at boundary" , async function ( ) {
98- const metric = new QuantityMetricType ( {
99- category : "aCategory" ,
100- name : "aQuantityMetric" ,
101- sendInPings : [ "aPing" ] ,
102- lifetime : Lifetime . Ping ,
103- disabled : false
104- } ) ;
105-
106- metric . set ( Number . MAX_SAFE_INTEGER + 1 ) ;
107- assert . strictEqual ( await metric . testGetValue ( "aPing" ) , Number . MAX_SAFE_INTEGER ) ;
108- } ) ;
109- } ) ;
45+ assert . deepStrictEqual ( await metric . testGetValue ( "aPing" ) , { numerator : 22 , denominator : 7 } ) ;
46+ } ) ;
47+ } ) ;
0 commit comments