Bug 1790872 - Part 3: Add a unit test for the IPC sent/recvd message telemetry, r=ipc-reviewers,mccr8

This is done using the gtest framework, and adds a new protocol which has its
messages counted. As the IPDLUnitTest process doesn't count IPC messages with
glean, this only counts messages on the parent process side.

This test also checks that we don't count this information outside of nightly,
and that it isn't counted for messages sent within the same process.

Differential Revision: https://phabricator.services.mozilla.com/D158275
This commit is contained in:
Nika Layzell 2022-11-08 01:11:12 +00:00
Родитель a190397bee
Коммит 798a46bd64
8 изменённых файлов: 212 добавлений и 0 удалений

Просмотреть файл

@ -2470,6 +2470,10 @@ ipc.sent_messages:
- ptemporary_ipcblob.m_operation_failed
- ptemporary_ipcblob.r___delete__
- ptest_basic.m_hello
- ptest_glean_msg_telemetry.m_hello_async
- ptest_glean_msg_telemetry.r_hello_async
- ptest_glean_msg_telemetry.m_hello_sync
- ptest_glean_msg_telemetry.r_hello_sync
- ptest_many_handles.m_many_handles
- ptest_shell_command.m___delete__
- ptest_shell_command.r___delete__

Просмотреть файл

@ -10,6 +10,8 @@
#############################################################
# gtests
[PTestGleanMsgTelemetry::HelloSync]
description = Only used by gtests
[PQuotaTest::Try_Success_CustomErr_QmIpcFail]
description = Only used by gtests
[PQuotaTest::Try_Success_CustomErr_IpcFail]

Просмотреть файл

@ -0,0 +1,17 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* 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/. */
namespace mozilla {
namespace _ipdltest {
sync protocol PTestGleanMsgTelemetry {
child:
async HelloAsync() returns (bool ok);
parent:
sync HelloSync() returns (bool ok);
};
} // namespace _ipdltest
} // namespace mozilla

Просмотреть файл

@ -0,0 +1,123 @@
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* 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 "gtest/gtest.h"
#include "mozilla/_ipdltest/IPDLUnitTest.h"
#include "mozilla/_ipdltest/TestGleanMsgTelemetryChild.h"
#include "mozilla/_ipdltest/TestGleanMsgTelemetryParent.h"
#include "mozilla/FOGIPC.h"
#include "mozilla/glean/GleanMetrics.h"
#include "mozilla/SpinEventLoopUntil.h"
#include "mozilla/ipc/MessageChannel.h"
#include "nsContentUtils.h"
using namespace mozilla::ipc;
namespace mozilla::_ipdltest {
static int GetSentCount(const nsACString& aLabel) {
#ifdef NIGHTLY_BUILD
auto value = mozilla::glean::ipc_sent_messages::parent_inactive.Get(aLabel)
.TestGetValue();
EXPECT_FALSE(value.isErr());
return value.unwrapOr(Nothing()).valueOr(0);
#else
return 0;
#endif
}
static int GetRecvCount(const nsACString& aLabel) {
#ifdef NIGHTLY_BUILD
auto value =
mozilla::glean::ipc_received_messages::parent_inactive.Get(aLabel)
.TestGetValue();
EXPECT_FALSE(value.isErr());
return value.unwrapOr(Nothing()).valueOr(0);
#else
return 0;
#endif
}
static constexpr nsLiteralCString kHelloAsyncKey =
"ptest_glean_msg_telemetry.m_hello_async"_ns;
static constexpr nsLiteralCString kHelloAsyncReplyKey =
"ptest_glean_msg_telemetry.r_hello_async"_ns;
static constexpr nsLiteralCString kHelloSyncKey =
"ptest_glean_msg_telemetry.m_hello_sync"_ns;
static constexpr nsLiteralCString kHelloSyncReplyKey =
"ptest_glean_msg_telemetry.r_hello_sync"_ns;
IPCResult TestGleanMsgTelemetryChild::RecvHelloAsync(
HelloAsyncResolver&& aResolver) {
EXPECT_TRUE(CanSend());
bool result = false;
EXPECT_TRUE(SendHelloSync(&result));
EXPECT_TRUE(result);
aResolver(result);
EXPECT_TRUE(CanSend());
return IPC_OK();
}
IPCResult TestGleanMsgTelemetryParent::RecvHelloSync(bool* aResult) {
EXPECT_EQ(mInitialHelloAsyncSent + mInc, GetSentCount(kHelloAsyncKey));
EXPECT_EQ(mInitialHelloAsyncReplyRecvd, GetRecvCount(kHelloAsyncReplyKey));
EXPECT_EQ(mInitialHelloSyncRecvd + mInc, GetRecvCount(kHelloSyncKey));
EXPECT_EQ(mInitialHelloSyncReplySent, GetSentCount(kHelloSyncReplyKey));
*aResult = true;
return IPC_OK();
}
IPDL_TEST(TestGleanMsgTelemetry) {
EXPECT_FALSE(nsContentUtils::GetUserIsInteracting())
<< "An earlier test did not reset user interaction status";
mozilla::glean::RecordPowerMetrics();
// Telemetry is only recorded for cross-process messages sent on nightly.
#ifdef NIGHTLY_BUILD
int inc = GetTestMode() == TestMode::SameProcess ? 0 : 1;
#else
int inc = 0;
#endif
// Record initial counts.
mActor->mInc = inc;
mActor->mInitialHelloAsyncSent = GetSentCount(kHelloAsyncKey);
mActor->mInitialHelloAsyncReplyRecvd = GetRecvCount(kHelloAsyncReplyKey);
mActor->mInitialHelloSyncRecvd = GetRecvCount(kHelloSyncKey);
mActor->mInitialHelloSyncReplySent = GetSentCount(kHelloSyncReplyKey);
// Send the initial message, and spin the event loop until we've received the
// final async reply.
bool done = false;
mActor->SendHelloAsync(
[&](bool aOk) {
EXPECT_TRUE(aOk) << "Expected response from AsyncHello";
done = true;
},
[&](mozilla::ipc::ResponseRejectReason aReason) {
EXPECT_TRUE(false) << "Unexpected AsyncHello rejection!";
done = true;
});
mozilla::SpinEventLoopUntil("_ipdltest::TestGleanMsgTelemetry"_ns,
[&] { return done; });
EXPECT_TRUE(done) << "Event loop exited early?";
// Check counts after the test is complete. If we're expecting it to
// increment, every test should increment by 1.
EXPECT_EQ(mActor->mInitialHelloAsyncSent + inc, GetSentCount(kHelloAsyncKey));
EXPECT_EQ(mActor->mInitialHelloAsyncReplyRecvd + inc,
GetRecvCount(kHelloAsyncReplyKey));
EXPECT_EQ(mActor->mInitialHelloSyncRecvd + inc, GetRecvCount(kHelloSyncKey));
EXPECT_EQ(mActor->mInitialHelloSyncReplySent + inc,
GetSentCount(kHelloSyncReplyKey));
mActor->Close();
}
} // namespace mozilla::_ipdltest

Просмотреть файл

@ -0,0 +1,26 @@
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* 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/. */
#ifndef mozilla__ipdltest_TestGleanMsgTelemetryChild_h
#define mozilla__ipdltest_TestGleanMsgTelemetryChild_h
#include "mozilla/_ipdltest/PTestGleanMsgTelemetryChild.h"
namespace mozilla::_ipdltest {
class TestGleanMsgTelemetryChild : public PTestGleanMsgTelemetryChild {
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(TestGleanMsgTelemetryChild, override)
public:
mozilla::ipc::IPCResult RecvHelloAsync(HelloAsyncResolver&&);
private:
~TestGleanMsgTelemetryChild() = default;
};
} // namespace mozilla::_ipdltest
#endif // mozilla__ipdltest_TestGleanMsgTelemetryChild_h

Просмотреть файл

@ -0,0 +1,33 @@
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* 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/. */
#ifndef mozilla__ipdltest_TestGleanMsgTelemetryParent_h
#define mozilla__ipdltest_TestGleanMsgTelemetryParent_h
#include "mozilla/_ipdltest/PTestGleanMsgTelemetryParent.h"
namespace mozilla::_ipdltest {
class TestGleanMsgTelemetryParent : public PTestGleanMsgTelemetryParent {
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(TestGleanMsgTelemetryParent, override)
public:
mozilla::ipc::IPCResult RecvHelloSync(bool* aResult);
int mInc = 0;
int mInitialHelloAsyncSent = 0;
int mInitialHelloAsyncReplyRecvd = 0;
int mInitialHelloSyncRecvd = 0;
int mInitialHelloSyncReplySent = 0;
private:
~TestGleanMsgTelemetryParent() = default;
};
} // namespace mozilla::_ipdltest
#endif // mozilla__ipdltest_TestGleanMsgTelemetryParent_h

Просмотреть файл

@ -12,17 +12,21 @@ EXPORTS.mozilla._ipdltest += [
"IPDLUnitTestParent.h",
"TestBasicChild.h",
"TestBasicParent.h",
"TestGleanMsgTelemetryChild.h",
"TestGleanMsgTelemetryParent.h",
]
SOURCES += [
"IPDLUnitTest.cpp",
"TestBasic.cpp",
"TestGleanMsgTelemetry.cpp",
"TestManyHandles.cpp",
]
IPDL_SOURCES += [
"PIPDLUnitTest.ipdl",
"PTestBasic.ipdl",
"PTestGleanMsgTelemetry.ipdl",
"PTestManyHandles.ipdl",
]

Просмотреть файл

@ -85,6 +85,9 @@ void AvailableMemoryChecker::Shutdown() {
mTimer->Cancel();
}
Preferences::ClearUser("browser.low_commit_space_threshold_percent");
if (nsCOMPtr<nsIObserverService> obsSvc = services::GetObserverService()) {
obsSvc->NotifyObservers(nullptr, "user-interaction-inactive", nullptr);
}
}
// Timer callback to increase the pref threshold.