================ @@ -0,0 +1,74 @@ +//===-- TelemetryTest.cpp ------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "llvm/Config/llvm-config.h" + +#ifdef LLVM_BUILD_TELEMETRY + +#include "lldb/Core/PluginInterface.h" +#include "lldb/Core/PluginManager.h" +#include "lldb/Core/Telemetry.h" +#include "llvm/ADT/StringRef.h" +#include "llvm/Support/Error.h" +#include "llvm/Telemetry/Telemetry.h" +#include "gtest/gtest.h" + +#include <memory> + +namespace lldb_private { + +struct FakeTelemetryInfo : public llvm::telemetry::TelemetryInfo { + std::string msg; +}; + +class FakePlugin : public telemetry::TelemetryManager { +public: + FakePlugin() + : telemetry::TelemetryManager( + std::make_unique<llvm::telemetry::Config>(true)) {} + + // TelemetryManager interface + llvm::Error dispatch(llvm::telemetry::TelemetryInfo *entry) override { + if (auto *fake_entry = llvm::dyn_cast<FakeTelemetryInfo>(entry)) { + fake_entry->msg = "In FakePlugin"; + } + + return llvm::Error::success(); + } + + // Plugin interface + llvm::StringRef GetPluginName() override { return "FakeTelemetryPlugin"; } + + static void Initialize() { + telemetry::TelemetryManager::setInstance(std::make_unique<FakePlugin>()); + } + + static void Terminate() { telemetry::TelemetryManager::setInstance(nullptr); } +}; + +} // namespace lldb_private + +TEST(TelemetryTest, PluginTest) { + // This would have been called by the plugin reg in a "real" plugin + // For tests, we just call it directly. + lldb_private::FakePlugin::Initialize(); + + auto ins = lldb_private::telemetry::TelemetryManager::getInstance(); + + ASSERT_NE(ins, nullptr); + lldb_private::FakeTelemetryInfo entry; + entry.msg = ""; + + auto stat = ins->dispatch(&entry); + ASSERT_FALSE(stat); + ASSERT_EQ("In FakePlugin", entry.msg); ---------------- labath wrote:
I think this is fine for what this patch does, but this is a bit of a strange test, as it's checking a side effect that the caller should not really care about (I am somewhat surprised that the manager can even modify the callers telemetry entry, as I think of it more like "consuming" it). For the event tests, I'd like to create slightly more realistic setup, where the fake/mock manager actually send the entries to a Destination object (which then saves it to some array or something) and then we check the events that were received there. https://github.com/llvm/llvm-project/pull/126588 _______________________________________________ lldb-commits mailing list lldb-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits