зеркало из https://github.com/mozilla/gecko-dev.git
Backed out 4 changesets (bug 1756057) for failures on browser_fog_init.js. CLOSED TREE
Backed out changeset afeabbc80268 (bug 1756057) Backed out changeset 1db54f4136a2 (bug 1756057) Backed out changeset 9364dd7f20bb (bug 1756057) Backed out changeset f7f1f73d38cd (bug 1756057)
This commit is contained in:
Родитель
255f433523
Коммит
8662f38c59
|
@ -6,10 +6,7 @@
|
|||
#include "GTestRunner.h"
|
||||
#include "gtest/gtest.h"
|
||||
#include "mozilla/Attributes.h"
|
||||
#include "mozilla/Preferences.h"
|
||||
#include "mozilla/glean/fog_ffi_generated.h"
|
||||
#include "nsICrashReporter.h"
|
||||
#include "nsString.h"
|
||||
#include "testing/TestHarness.h"
|
||||
#include "prenv.h"
|
||||
#ifdef ANDROID
|
||||
|
@ -156,14 +153,6 @@ int RunGTestFunc(int* argc, char** argv) {
|
|||
}
|
||||
}
|
||||
|
||||
// FOG should init exactly once, as early into running as possible, to enable
|
||||
// instrumentation tests to work properly.
|
||||
// However, at init, Glean may decide to send a ping. So let's first tell FOG
|
||||
// that these pings shouldn't actually be uploaded.
|
||||
Preferences::SetInt("telemetry.fog.test.localhost_port", -1);
|
||||
const nsCString empty;
|
||||
glean::impl::fog_init(&empty, &empty);
|
||||
|
||||
return RUN_ALL_TESTS();
|
||||
}
|
||||
|
||||
|
|
|
@ -33,9 +33,8 @@ you're going to want to write some automated tests.
|
|||
|
||||
* 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.
|
||||
`Services.fog.testResetFOG()` (in JS), or
|
||||
`mozilla::glean::TestResetFOG()` (in C++).
|
||||
* 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,
|
||||
|
@ -45,9 +44,6 @@ you're going to want to write some automated tests.
|
|||
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.
|
||||
|
@ -161,24 +157,18 @@ In this case there's a slight addition to the Usual Test Format:
|
|||
|
||||
## GTests/Google Tests
|
||||
|
||||
Please make use of the `FOGFixture` fixture when writing your tests, like:
|
||||
Unfortunately this is a pain to test at the moment.
|
||||
gtests run with a single temporary profile, but FOG's own gtests use it,
|
||||
polluting it from test to test.
|
||||
|
||||
```cpp
|
||||
TEST_F(FOGFixture, MyTestCase) {
|
||||
// 1) Assert no value
|
||||
ASSERT_EQ(mozilla::Nothing(),
|
||||
my_metric_category::my_metric_name.TestGetValue());
|
||||
In this case there's a slight addition to the Usual Test Format:
|
||||
1) _Reset FOG with `mozilla::glean::impl::fog_test_reset(""_ns, ""_ns);`_
|
||||
2) Assert no value in the metric
|
||||
3) Express behaviour
|
||||
4) Assert correct value in the metric.
|
||||
|
||||
// 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.
|
||||
Work to improve this is tracked in
|
||||
[bug 1756057](https://bugzilla.mozilla.org/show_bug.cgi?id=1756057).
|
||||
|
||||
## Rust `rusttests`
|
||||
|
||||
|
@ -192,30 +182,11 @@ which means we need to use the
|
|||
[GTest + FFI approach](/testing-rust-code/index.html#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));
|
||||
}
|
||||
```
|
||||
This means you follow the GTests/Google Tests variant on the Usual Test Format:
|
||||
1) _Reset FOG with `mozilla::glean::impl::fog_test_reset(""_ns, ""_ns);`_
|
||||
2) Assert no value in the metric
|
||||
3) Express behaviour
|
||||
4) Assert correct value in the metric.
|
||||
|
||||
[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
|
||||
|
|
|
@ -7,8 +7,6 @@ support-files = empty_file.html
|
|||
|
||||
[browser_fog_gpu.js]
|
||||
|
||||
[browser_fog_init.js]
|
||||
|
||||
[browser_fog_rdd.js]
|
||||
support-files = small-shot.ogg
|
||||
|
||||
|
|
|
@ -1,18 +0,0 @@
|
|||
/* Any copyright is dedicated to the Public Domain.
|
||||
* http://creativecommons.org/publicdomain/zero/1.0/ */
|
||||
|
||||
"use strict";
|
||||
|
||||
add_task(async () => {
|
||||
if (new Date().getHours() >= 3 && new Date().getHours() <= 4) {
|
||||
// We skip this test if it's too close to 4AM, when we might send a
|
||||
// "metrics" ping between init and this test being run.
|
||||
ok(true, "Too close to 'metrics' ping send window. Skipping test.");
|
||||
return;
|
||||
}
|
||||
Assert.greater(
|
||||
Glean.fog.initialization.testGetValue(),
|
||||
0,
|
||||
"FOG init happened, and its time was measured."
|
||||
);
|
||||
});
|
|
@ -1,23 +0,0 @@
|
|||
/* Any copyright is dedicated to the Public Domain.
|
||||
* http://creativecommons.org/publicdomain/zero/1.0/
|
||||
*/
|
||||
|
||||
#ifndef FOGFixture_h_
|
||||
#define FOGFixture_h_
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
#include "mozilla/glean/fog_ffi_generated.h"
|
||||
#include "nsString.h"
|
||||
|
||||
using namespace mozilla::glean::impl;
|
||||
|
||||
class FOGFixture : public ::testing::Test {
|
||||
protected:
|
||||
FOGFixture() = default;
|
||||
virtual void SetUp() {
|
||||
nsCString empty;
|
||||
ASSERT_EQ(NS_OK, fog_test_reset(&empty, &empty));
|
||||
}
|
||||
};
|
||||
|
||||
#endif // FOGFixture_h_
|
|
@ -2,7 +2,6 @@
|
|||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
#include "FOGFixture.h"
|
||||
#include "gmock/gmock.h"
|
||||
#include "gtest/gtest.h"
|
||||
#include "mozilla/glean/GleanMetrics.h"
|
||||
|
@ -33,7 +32,24 @@ void GTest_FOG_ExpectFailure(const char* aMessage) {
|
|||
}
|
||||
}
|
||||
|
||||
TEST_F(FOGFixture, BuiltinPingsRegistered) {
|
||||
// Initialize FOG exactly once.
|
||||
// This needs to be the first test to run!
|
||||
TEST(FOG, FogInitDoesntCrash)
|
||||
{
|
||||
Preferences::SetInt("telemetry.fog.test.localhost_port", -1);
|
||||
const nsCString empty;
|
||||
ASSERT_EQ(NS_OK, fog_init(&empty, &empty));
|
||||
ASSERT_EQ(NS_OK, fog_test_reset(&empty, &empty));
|
||||
}
|
||||
|
||||
extern "C" void Rust_MeasureInitializeTime();
|
||||
// Disabled because this depends on the preinit buffer not overflowing,
|
||||
// which currently can't be guaranteed. See bug 1756057 for how to fix it.
|
||||
TEST(FOG, DISABLED_TestMeasureInitializeTime)
|
||||
{ Rust_MeasureInitializeTime(); }
|
||||
|
||||
TEST(FOG, BuiltinPingsRegistered)
|
||||
{
|
||||
Preferences::SetInt("telemetry.fog.test.localhost_port", -1);
|
||||
nsAutoCString metricsPingName("metrics");
|
||||
nsAutoCString baselinePingName("baseline");
|
||||
|
@ -43,7 +59,8 @@ TEST_F(FOGFixture, BuiltinPingsRegistered) {
|
|||
ASSERT_EQ(NS_OK, fog_submit_ping(&eventsPingName));
|
||||
}
|
||||
|
||||
TEST_F(FOGFixture, TestCppCounterWorks) {
|
||||
TEST(FOG, TestCppCounterWorks)
|
||||
{
|
||||
mozilla::glean::test_only::bad_code.Add(42);
|
||||
|
||||
ASSERT_EQ(42, mozilla::glean::test_only::bad_code.TestGetValue("test-ping"_ns)
|
||||
|
@ -53,7 +70,8 @@ TEST_F(FOGFixture, TestCppCounterWorks) {
|
|||
ASSERT_EQ(42, test_only::bad_code.TestGetValue().unwrap().value());
|
||||
}
|
||||
|
||||
TEST_F(FOGFixture, TestCppStringWorks) {
|
||||
TEST(FOG, TestCppStringWorks)
|
||||
{
|
||||
auto kValue = "cheez!"_ns;
|
||||
mozilla::glean::test_only::cheesy_string.Set(kValue);
|
||||
|
||||
|
@ -64,7 +82,8 @@ TEST_F(FOGFixture, TestCppStringWorks) {
|
|||
.get());
|
||||
}
|
||||
|
||||
TEST_F(FOGFixture, TestCppTimespanWorks) {
|
||||
TEST(FOG, TestCppTimespanWorks)
|
||||
{
|
||||
mozilla::glean::test_only::can_we_time_it.Start();
|
||||
PR_Sleep(PR_MillisecondsToInterval(10));
|
||||
mozilla::glean::test_only::can_we_time_it.Stop();
|
||||
|
@ -75,7 +94,8 @@ TEST_F(FOGFixture, TestCppTimespanWorks) {
|
|||
.value() > 0);
|
||||
}
|
||||
|
||||
TEST_F(FOGFixture, TestCppUuidWorks) {
|
||||
TEST(FOG, TestCppUuidWorks)
|
||||
{
|
||||
nsCString kTestUuid("decafdec-afde-cafd-ecaf-decafdecafde");
|
||||
test_only::what_id_it.Set(kTestUuid);
|
||||
ASSERT_STREQ(kTestUuid.get(),
|
||||
|
@ -94,7 +114,8 @@ TEST_F(FOGFixture, TestCppUuidWorks) {
|
|||
.get());
|
||||
}
|
||||
|
||||
TEST_F(FOGFixture, TestCppBooleanWorks) {
|
||||
TEST(FOG, TestCppBooleanWorks)
|
||||
{
|
||||
mozilla::glean::test_only::can_we_flag_it.Set(false);
|
||||
|
||||
ASSERT_EQ(false, mozilla::glean::test_only::can_we_flag_it
|
||||
|
@ -108,7 +129,8 @@ MATCHER_P(BitEq, x, "bit equal") {
|
|||
return std::memcmp(&arg, &x, sizeof(x)) == 0;
|
||||
}
|
||||
|
||||
TEST_F(FOGFixture, TestCppDatetimeWorks) {
|
||||
TEST(FOG, TestCppDatetimeWorks)
|
||||
{
|
||||
PRExplodedTime date{0, 35, 10, 12, 6, 10, 2020, 0, 0, {5 * 60 * 60, 0}};
|
||||
test_only::what_a_date.Set(&date);
|
||||
|
||||
|
@ -122,7 +144,8 @@ using mozilla::Tuple;
|
|||
using mozilla::glean::test_only_ipc::AnEventExtra;
|
||||
using mozilla::glean::test_only_ipc::EventWithExtraExtra;
|
||||
|
||||
TEST_F(FOGFixture, TestCppEventWorks) {
|
||||
TEST(FOG, TestCppEventWorks)
|
||||
{
|
||||
test_only_ipc::no_extra_event.Record();
|
||||
ASSERT_TRUE(test_only_ipc::no_extra_event.TestGetValue("store1"_ns)
|
||||
.unwrap()
|
||||
|
@ -142,7 +165,8 @@ TEST_F(FOGFixture, TestCppEventWorks) {
|
|||
ASSERT_STREQ("can set extras", mozilla::Get<1>(events[0].mExtra[0]).get());
|
||||
}
|
||||
|
||||
TEST_F(FOGFixture, TestCppEventsWithDifferentExtraTypes) {
|
||||
TEST(FOG, TestCppEventsWithDifferentExtraTypes)
|
||||
{
|
||||
EventWithExtraExtra extra = {.extra1 = Some("can set extras"_ns),
|
||||
.extra2 = Some(37),
|
||||
.extra3LongerName = Some(false)};
|
||||
|
@ -173,7 +197,8 @@ TEST_F(FOGFixture, TestCppEventsWithDifferentExtraTypes) {
|
|||
}
|
||||
}
|
||||
|
||||
TEST_F(FOGFixture, TestCppMemoryDistWorks) {
|
||||
TEST(FOG, TestCppMemoryDistWorks)
|
||||
{
|
||||
test_only::do_you_remember.Accumulate(7);
|
||||
test_only::do_you_remember.Accumulate(17);
|
||||
|
||||
|
@ -191,7 +216,8 @@ TEST_F(FOGFixture, TestCppMemoryDistWorks) {
|
|||
}
|
||||
}
|
||||
|
||||
TEST_F(FOGFixture, TestCppCustomDistWorks) {
|
||||
TEST(FOG, TestCppCustomDistWorks)
|
||||
{
|
||||
test_only_ipc::a_custom_dist.AccumulateSamples({7, 268435458});
|
||||
|
||||
DistributionData data =
|
||||
|
@ -206,7 +232,8 @@ TEST_F(FOGFixture, TestCppCustomDistWorks) {
|
|||
}
|
||||
}
|
||||
|
||||
TEST_F(FOGFixture, TestCppPings) {
|
||||
TEST(FOG, TestCppPings)
|
||||
{
|
||||
test_only::one_ping_one_bool.Set(false);
|
||||
const auto& ping = mozilla::glean_pings::OnePingOnly;
|
||||
bool submitted = false;
|
||||
|
@ -220,7 +247,8 @@ TEST_F(FOGFixture, TestCppPings) {
|
|||
<< "Must have actually called the lambda.";
|
||||
}
|
||||
|
||||
TEST_F(FOGFixture, TestCppStringLists) {
|
||||
TEST(FOG, TestCppStringLists)
|
||||
{
|
||||
auto kValue = "cheez!"_ns;
|
||||
auto kValue2 = "cheezier!"_ns;
|
||||
auto kValue3 = "cheeziest."_ns;
|
||||
|
@ -242,7 +270,8 @@ TEST_F(FOGFixture, TestCppStringLists) {
|
|||
ASSERT_STREQ(kValue3.get(), val[2].get());
|
||||
}
|
||||
|
||||
TEST_F(FOGFixture, TestCppTimingDistWorks) {
|
||||
TEST(FOG, TestCppTimingDistWorks)
|
||||
{
|
||||
auto id1 = test_only::what_time_is_it.Start();
|
||||
auto id2 = test_only::what_time_is_it.Start();
|
||||
PR_Sleep(PR_MillisecondsToInterval(5));
|
||||
|
@ -272,7 +301,8 @@ TEST_F(FOGFixture, TestCppTimingDistWorks) {
|
|||
ASSERT_EQ(sampleCount, (uint64_t)2);
|
||||
}
|
||||
|
||||
TEST_F(FOGFixture, TestLabeledBooleanWorks) {
|
||||
TEST(FOG, TestLabeledBooleanWorks)
|
||||
{
|
||||
ASSERT_EQ(mozilla::Nothing(),
|
||||
test_only::mabels_like_balloons.Get("hot_air"_ns)
|
||||
.TestGetValue()
|
||||
|
@ -289,7 +319,8 @@ TEST_F(FOGFixture, TestLabeledBooleanWorks) {
|
|||
.ref());
|
||||
}
|
||||
|
||||
TEST_F(FOGFixture, TestLabeledCounterWorks) {
|
||||
TEST(FOG, TestLabeledCounterWorks)
|
||||
{
|
||||
ASSERT_EQ(mozilla::Nothing(),
|
||||
test_only::mabels_kitchen_counters.Get("marble"_ns)
|
||||
.TestGetValue()
|
||||
|
@ -306,7 +337,8 @@ TEST_F(FOGFixture, TestLabeledCounterWorks) {
|
|||
.ref());
|
||||
}
|
||||
|
||||
TEST_F(FOGFixture, TestLabeledStringWorks) {
|
||||
TEST(FOG, TestLabeledStringWorks)
|
||||
{
|
||||
ASSERT_EQ(mozilla::Nothing(),
|
||||
test_only::mabels_balloon_strings.Get("twine"_ns)
|
||||
.TestGetValue()
|
||||
|
@ -328,7 +360,8 @@ TEST_F(FOGFixture, TestLabeledStringWorks) {
|
|||
.get());
|
||||
}
|
||||
|
||||
TEST_F(FOGFixture, TestCppQuantityWorks) {
|
||||
TEST(FOG, TestCppQuantityWorks)
|
||||
{
|
||||
// This joke only works in base 13.
|
||||
const uint32_t kValue = 6 * 9;
|
||||
mozilla::glean::test_only::meaning_of_life.Set(kValue);
|
||||
|
@ -338,7 +371,8 @@ TEST_F(FOGFixture, TestCppQuantityWorks) {
|
|||
.value());
|
||||
}
|
||||
|
||||
TEST_F(FOGFixture, TestCppRateWorks) {
|
||||
TEST(FOG, TestCppRateWorks)
|
||||
{
|
||||
// 1) Standard rate with internal denominator
|
||||
const int32_t kNum = 22;
|
||||
const int32_t kDen = 7; // because I like pi, even just approximately.
|
||||
|
@ -360,7 +394,8 @@ TEST_F(FOGFixture, TestCppRateWorks) {
|
|||
test_only_ipc::an_external_denominator.TestGetValue().unwrap().extract());
|
||||
}
|
||||
|
||||
TEST_F(FOGFixture, TestCppUrlWorks) {
|
||||
TEST(FOG, TestCppUrlWorks)
|
||||
{
|
||||
auto kValue = "https://example.com/fog/gtest"_ns;
|
||||
mozilla::glean::test_only_ipc::a_url.Set(kValue);
|
||||
|
||||
|
@ -370,6 +405,3 @@ TEST_F(FOGFixture, TestCppUrlWorks) {
|
|||
.value()
|
||||
.get());
|
||||
}
|
||||
|
||||
extern "C" void Rust_TestRustInGTest();
|
||||
TEST_F(FOGFixture, TestRustInGTest) { Rust_TestRustInGTest(); }
|
||||
|
|
|
@ -28,12 +28,17 @@ macro_rules! expect {
|
|||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn Rust_TestRustInGTest() {
|
||||
// Just a smoke test, we show here how tests might work that both
|
||||
// a) Are in Rust, and
|
||||
// b) Require Gecko
|
||||
// This demonstration doesn't actually require Gecko. But we pretend it
|
||||
// does so we remember how to do this rust-in-gtest pattern.
|
||||
fog::metrics::test_only::bad_code.add(12);
|
||||
expect!(fog::metrics::test_only::bad_code.test_get_value(None) == Some(12));
|
||||
pub extern "C" fn Rust_MeasureInitializeTime() {
|
||||
// At this point FOG is already initialized.
|
||||
// We still need for it to finish, as it is running in a separate thread.
|
||||
|
||||
let metric = &*fog::metrics::fog::initialization;
|
||||
while metric.test_get_value("metrics").is_none() {
|
||||
// We _know_ this value is recorded early, so let's just yield
|
||||
// and try again quickly.
|
||||
std::thread::yield_now();
|
||||
}
|
||||
|
||||
let value = metric.test_get_value("metrics").unwrap();
|
||||
expect!(value > 0);
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче