|
| 1 | +//===-- TelemetryTest.cpp ------------------------------------------------===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | + |
| 9 | +#include "llvm/Config/llvm-config.h" |
| 10 | + |
| 11 | +#ifdef LLVM_BUILD_TELEMETRY |
| 12 | + |
| 13 | +#include "lldb/Core/PluginInterface.h" |
| 14 | +#include "lldb/Core/PluginManager.h" |
| 15 | +#include "lldb/Core/Telemetry.h" |
| 16 | +#include "llvm/ADT/StringRef.h" |
| 17 | +#include "llvm/Support/Error.h" |
| 18 | +#include "llvm/Telemetry/Telemetry.h" |
| 19 | +#include "llvm/Testing/Support/Error.h" |
| 20 | +#include "gtest/gtest.h" |
| 21 | +#include <memory> |
| 22 | +#include <vector> |
| 23 | + |
| 24 | +namespace lldb_private { |
| 25 | + |
| 26 | +struct FakeTelemetryInfo : public llvm::telemetry::TelemetryInfo { |
| 27 | + std::string msg; |
| 28 | +}; |
| 29 | + |
| 30 | +class TestDestination : public llvm::telemetry::Destination { |
| 31 | +public: |
| 32 | + TestDestination(std::vector<const llvm::telemetry::TelemetryInfo *> *entries) |
| 33 | + : received_entries(entries) {} |
| 34 | + |
| 35 | + llvm::Error |
| 36 | + receiveEntry(const llvm::telemetry::TelemetryInfo *entry) override { |
| 37 | + received_entries->push_back(entry); |
| 38 | + return llvm::Error::success(); |
| 39 | + } |
| 40 | + |
| 41 | + llvm::StringLiteral name() const override { return "TestDestination"; } |
| 42 | + |
| 43 | +private: |
| 44 | + std::vector<const llvm::telemetry::TelemetryInfo *> *received_entries; |
| 45 | +}; |
| 46 | + |
| 47 | +class FakePlugin : public telemetry::TelemetryManager { |
| 48 | +public: |
| 49 | + FakePlugin() |
| 50 | + : telemetry::TelemetryManager( |
| 51 | + std::make_unique<llvm::telemetry::Config>(true)) {} |
| 52 | + |
| 53 | + // TelemetryManager interface |
| 54 | + llvm::Error preDispatch(llvm::telemetry::TelemetryInfo *entry) override { |
| 55 | + if (auto *fake_entry = llvm::dyn_cast<FakeTelemetryInfo>(entry)) |
| 56 | + fake_entry->msg = "In FakePlugin"; |
| 57 | + |
| 58 | + return llvm::Error::success(); |
| 59 | + } |
| 60 | + |
| 61 | + llvm::StringRef GetInstanceName() const override { |
| 62 | + return "FakeTelemetryPlugin"; |
| 63 | + } |
| 64 | + |
| 65 | + static void Initialize() { |
| 66 | + telemetry::TelemetryManager::setInstance(std::make_unique<FakePlugin>()); |
| 67 | + } |
| 68 | + |
| 69 | + static void Terminate() { telemetry::TelemetryManager::setInstance(nullptr); } |
| 70 | +}; |
| 71 | + |
| 72 | +} // namespace lldb_private |
| 73 | + |
| 74 | +TEST(TelemetryTest, PluginTest) { |
| 75 | + // This would have been called by the plugin reg in a "real" plugin |
| 76 | + // For tests, we just call it directly. |
| 77 | + lldb_private::FakePlugin::Initialize(); |
| 78 | + |
| 79 | + auto *ins = lldb_private::telemetry::TelemetryManager::getInstance(); |
| 80 | + ASSERT_NE(ins, nullptr); |
| 81 | + |
| 82 | + std::vector<const ::llvm::telemetry::TelemetryInfo *> expected_entries; |
| 83 | + ins->addDestination( |
| 84 | + std::make_unique<lldb_private::TestDestination>(&expected_entries)); |
| 85 | + |
| 86 | + lldb_private::FakeTelemetryInfo entry; |
| 87 | + entry.msg = ""; |
| 88 | + |
| 89 | + ASSERT_THAT_ERROR(ins->dispatch(&entry), ::llvm::Succeeded()); |
| 90 | + ASSERT_EQ(1, expected_entries.size()); |
| 91 | + EXPECT_EQ("In FakePlugin", |
| 92 | + llvm::dyn_cast<lldb_private::FakeTelemetryInfo>(expected_entries[0]) |
| 93 | + ->msg); |
| 94 | + |
| 95 | + ASSERT_EQ("FakeTelemetryPlugin", ins->GetInstanceName()); |
| 96 | +} |
| 97 | + |
| 98 | +#endif // LLVM_BUILD_TELEMETRY |
0 commit comments