blob: c607fef5255c001076c8c55c8050600786458d47 [file]
// Copyright 2026 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "src/lib/metrics_buffer/metrics_impl.h"
#include <fidl/fuchsia.io/cpp/fidl.h>
#include <fidl/fuchsia.metrics/cpp/wire.h>
#include <lib/async-loop/cpp/loop.h>
#include <lib/async-loop/default.h>
#include <lib/async/cpp/task.h>
#include <lib/component/outgoing/cpp/outgoing_directory.h>
#include <lib/fidl/cpp/wire/server.h>
#include <lib/zx/channel.h>
#include <memory>
#include <optional>
#include <vector>
#include <gtest/gtest.h>
namespace {
// A MetricEventLoggerFactory that rejects every CreateMetricEventLogger request with an error and
// closes the requested logger server end without serving it.
class RejectingLoggerFactory : public fidl::WireServer<fuchsia_metrics::MetricEventLoggerFactory> {
public:
void CreateMetricEventLogger(CreateMetricEventLoggerRequestView request,
CreateMetricEventLoggerCompleter::Sync& completer) override {
++create_logger_count_;
last_project_id_ = request->project_spec.project_id();
// Close the logger server end, so the client's pending LogMetricEvents fails.
request->logger.reset();
completer.ReplyError(fuchsia_metrics::wire::Error::kInvalidArguments);
}
void CreateMetricEventLoggerWithExperiments(
CreateMetricEventLoggerWithExperimentsRequestView request,
CreateMetricEventLoggerWithExperimentsCompleter::Sync& completer) override {
ADD_FAILURE() << "CreateMetricEventLoggerWithExperiments not expected";
completer.ReplyError(fuchsia_metrics::wire::Error::kInvalidArguments);
}
uint32_t create_logger_count() const { return create_logger_count_; }
uint32_t last_project_id() const { return last_project_id_; }
private:
uint32_t create_logger_count_ = 0;
uint32_t last_project_id_ = 0;
};
std::vector<fuchsia_metrics::MetricEvent> OneCountEvent() {
std::vector<fuchsia_metrics::MetricEvent> events;
events.emplace_back(12u, std::vector<uint32_t>{1u},
fuchsia_metrics::MetricEventPayload::WithCount(1u));
return events;
}
class MetricsImplTest : public ::testing::Test {
protected:
MetricsImplTest() : loop_(&kAsyncLoopConfigAttachToCurrentThread) {}
async::Loop& loop() { return loop_; }
private:
async::Loop loop_;
};
// If connecting to MetricEventLoggerFactory through the directory fails, the events are dropped
// without a crash, and the dispatcher keeps running later tasks.
TEST_F(MetricsImplTest, ConnectFailureDropsEvents) {
auto [directory_client, directory_server] = fidl::Endpoints<fuchsia_io::Directory>::Create();
// Without ZX_RIGHT_WRITE, the connect request can't be written to the directory channel, so
// connecting fails synchronously.
zx::channel read_only;
ASSERT_EQ(ZX_OK, directory_client.TakeChannel().replace(
ZX_RIGHT_READ | ZX_RIGHT_TRANSFER | ZX_RIGHT_WAIT, &read_only));
cobalt::MetricsImpl metrics(loop().dispatcher(),
fidl::ClientEnd<fuchsia_io::Directory>(std::move(read_only)), 42);
metrics.LogMetricEvents(OneCountEvent());
bool later_task_ran = false;
ASSERT_EQ(ZX_OK,
async::PostTask(loop().dispatcher(), [&later_task_ran] { later_task_ran = true; }));
EXPECT_EQ(ZX_OK, loop().RunUntilIdle());
EXPECT_TRUE(later_task_ran);
// The directory never received a connect request.
zx_signals_t pending = 0;
EXPECT_EQ(ZX_ERR_TIMED_OUT, directory_server.channel().wait_one(
ZX_CHANNEL_READABLE, zx::time::infinite_past(), &pending));
}
// If the factory rejects CreateMetricEventLogger and closes the logger, both the create error and
// the failed LogMetricEvents call are handled, and the factory saw the configured project id.
TEST_F(MetricsImplTest, CreateLoggerErrorAndLogFailureAreHandled) {
RejectingLoggerFactory factory;
component::OutgoingDirectory outgoing(loop().dispatcher());
ASSERT_TRUE(
outgoing
.AddUnmanagedProtocol<fuchsia_metrics::MetricEventLoggerFactory>(
[this, &factory](fidl::ServerEnd<fuchsia_metrics::MetricEventLoggerFactory> server) {
fidl::BindServer(loop().dispatcher(), std::move(server), &factory);
})
.is_ok());
auto [root_client, root_server] = fidl::Endpoints<fuchsia_io::Directory>::Create();
ASSERT_TRUE(outgoing.Serve(std::move(root_server)).is_ok());
auto [directory_client, directory_server] = fidl::Endpoints<fuchsia_io::Directory>::Create();
ASSERT_TRUE(fidl::WireCall(root_client)
->Open("svc", fuchsia_io::kPermReadable, {}, directory_server.TakeChannel())
.ok());
{
cobalt::MetricsImpl metrics(loop().dispatcher(), std::move(directory_client), 42);
metrics.LogMetricEvents(OneCountEvent());
EXPECT_EQ(ZX_OK, loop().RunUntilIdle());
}
// Let the client teardown finish while `factory` and `outgoing` are still alive.
EXPECT_EQ(ZX_OK, loop().RunUntilIdle());
EXPECT_EQ(1u, factory.create_logger_count());
EXPECT_EQ(42u, factory.last_project_id());
}
} // namespace