1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
|
# Writing Instrumentation Tests
```{admonition} Old Glean Proverb
If it's important enough to be instrumented, it's important enough to be tested.
```
All metrics and pings in the Glean SDK have [well-documented APIs for testing][glean-metrics-apis].
You'll want to familiarize yourself with `TestGetValue()`
(here's [an example JS (xpcshell) test of some metrics][metrics-xpcshell-test])
for metrics and
[`TestBeforeNextSubmit()`][test-before-next-submit]
(here's [an example C++ (gtest) test of a custom ping][ping-gtest])
for pings.
All test APIs are available in all three of FOG's supported languages:
Rust, C++, and JavaScript.
But how do you get into a position where you can even call these test APIs?
How do they fit in with Firefox Desktop's testing frameworks?
## Manual Testing and Debugging
The Glean SDK has [debugging capabilities][glean-debug]
for manually verifying that instrumentation makes it to Mozilla's Data Pipeline.
Firefox Desktop supports these via environment variables _and_
via the interface on `about:glean`.
This is all well and good for getting a good sense check that things are going well _now_,
but in order to check that everything stays good through the future,
you're going to want to write some automated tests.
## General Things To Bear In Mind
* You may see values from previous tests persist across tests because the profile directory was shared between test cases.
* You can reset Glean before your test by calling
`Services.fog.testResetFOG()` (in JS).
* You shouldn't have to do this in C++ or Rust since there you should use the
`FOGFixture` test fixture.
* If your metric is based on timing (`timespan`, `timing_distribution`),
do not expect to be able to assert the correct timing value.
Glean does a lot of timing for you deep in the SDK, so unless you mock the system's monotonic clock,
do not expect the values to be predictable.
* Instead, check that a value is `> 0` or that the number of samples is expected.
* You might be able to assert that the value is at least as much as a known, timed value,
but beware of rounding.
* Errors in instrumentation APIs do not panic, throw, or crash.
But Glean remembers that the errors happened.
* Test APIs, on the other hand, are permitted
(some may say "encouraged")
to panic, throw, or crash on bad behaviour.
* If you call a test API and it panics, throws, or crashes,
that means your instrumentation did something wrong.
Check your test logs for details about what went awry.
## The Usual Test Format
Instrumentation tests tend to follow the same three-part format:
1) Assert no value in the metric
2) Express behaviour
3) Assert correct value in the metric
Your choice of test suite will depend on how the instrumented behaviour can be expressed.
## `xpcshell` Tests
If the instrumented behaviour is on the main or content process and can be called from privileged JS,
`xpcshell` is an excellent choice.
`xpcshell` is so minimal an environment, however, that
(pending [bug 1756055](https://bugzilla.mozilla.org/show_bug.cgi?id=1756055))
you'll need to manually tell it you need two things:
1) A profile directory
2) An initialized FOG
```js
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
"use strict";
add_setup(function test_setup() {
// FOG needs a profile directory to put its data in.
do_get_profile();
// FOG needs to be initialized in order for data to flow.
Services.fog.initializeFOG();
});
```
From there, just follow The Usual Test Format:
```js
add_task(function test_instrumentation() {
// 1) Assert no value
Assert.equal(undefined, Glean.myMetricCategory.myMetricName.testGetValue());
// 2) Express behaviour
// ...<left as an exercise to the reader>...
// 3) Assert correct value
Assert.equal(kValue, Glean.myMetricCategory.myMetricName.testGetValue());
});
```
If your new instrumentation includes a new custom ping,
there are two small additions to The Usual Test Format:
* 1.1) Call `testBeforeNextSubmit` _before_ your ping is submitted.
The callback you register in `testBeforeNextSubmit`
is called synchronously with the call to the ping's `submit()`.
* 3.1) Check that the ping actually was submitted.
If all your Asserts are inside `testBeforeNextSubmit`'s closure,
another way this test could pass is by not running any of them.
```js
add_task(function test_custom_ping() {
// 1) Assert no value
Assert.equal(undefined, Glean.myMetricCategory.myMetricName.testGetValue());
// 1.1) Set up Step 3.
let submitted = false;
GleanPings.myPing.testBeforeNextSubmit(reason => {
submitted = true;
// 3) Assert correct value
Assert.equal(kExpectedReason, reason, "Reason of submitted ping must match.");
Assert.equal(kExpectedMetricValue, Glean.myMetricCategory.myMetricName.testGetValue());
});
// 2) Express behaviour that sends a ping with expected reason and contents
// ...<left as an exercise to the reader>...
// 3.1) Check that the ping actually was submitted.
Assert.ok(submitted, "Ping was submitted, callback was called.");
});
```
(( We acknowledge that this isn't the most ergonomic form.
Please follow
[bug 1756637](https://bugzilla.mozilla.org/show_bug.cgi?id=1756637)
for updates on a better design and implementation for ping tests. ))
## mochitest
`browser-chrome`-flavoured mochitests can be tested very similarly to `xpcshell`,
though you do not need to request a profile or initialize FOG.
`plain`-flavoured mochitests aren't yet supported (follow
[bug 1799977](https://bugzilla.mozilla.org/show_bug.cgi?id=1799977)
for updates and a workaround).
If you're testing in `mochitest`, your instrumentation (or your test)
might not be running in the parent process.
This means you get to learn the IPC test APIs.
### IPC
All test APIs must be called on the main process
(they'll assert otherwise).
But your instrumentation might be on any process, so how do you test it?
In this case there's a slight addition to the Usual Test Format:
1) Assert no value in the metric
2) Express behaviour
3) _Flush all pending FOG IPC operations with `Services.fog.testFlushAllChildren()`_
4) Assert correct value in the metric.
## GTests/Google Tests
Please make use of the `FOGFixture` fixture when writing your tests, like:
```cpp
TEST_F(FOGFixture, MyTestCase) {
// 1) Assert no value
ASSERT_EQ(mozilla::Nothing(),
my_metric_category::my_metric_name.TestGetValue());
// 2) Express behaviour
// ...<left as an exercise to the reader>...
// 3) Assert correct value
ASSERT_EQ(kValue,
my_metric_category::my_metric_name.TestGetValue().unwrap().ref());
}
```
The fixture will take care of ensuring storage is reset between tests.
## Rust `rusttests`
The general-purpose
[Testing & Debugging Rust Code in Firefox](/testing-rust-code/index)
is a good thing to review first.
Unfortunately, FOG requires gecko
(to tell it where the profile dir is, and other things),
which means we need to use the
[GTest + FFI approach](/testing-rust-code/index.md#gtests)
where GTest is the runner and Rust is just the language the test is written in.
This means your test will look like a GTest like this:
```cpp
extern "C" void Rust_MyRustTest();
TEST_F(FOGFixture, MyRustTest) { Rust_MyRustTest(); }
```
Plus a Rust test like this:
```rust
#[no_mangle]
pub extern "C" fn Rust_MyRustTest() {
// 1) Assert no value
assert_eq!(None,
fog::metrics::my_metric_category::my_metric_name.test_get_value(None));
// 2) Express behaviour
// ...<left as an exercise to the reader>...
// 3) Assert correct value
assert_eq!(Some(value),
fog::metrics::my_metric_category::my_metric_name.test_get_value(None));
}
```
[glean-metrics-apis]: https://mozilla.github.io/glean/book/reference/metrics/index.html
[metrics-xpcshell-test]: https://searchfox.org/mozilla-central/rev/66e59131c1c76fe486424dc37f0a8a399ca874d4/toolkit/mozapps/update/tests/unit_background_update/test_backgroundupdate_glean.js#28
[ping-gtest]: https://searchfox.org/mozilla-central/rev/66e59131c1c76fe486424dc37f0a8a399ca874d4/toolkit/components/glean/tests/gtest/TestFog.cpp#232
[test-before-next-submit]: https://mozilla.github.io/glean/book/reference/pings/index.html#testbeforenextsubmit
[glean-debug]: https://mozilla.github.io/glean/book/reference/debug/index.html
|