1 | #include "lldb/DataFormatters/TypeSummary.h" |
---|---|
2 | #include "lldb/Target/Statistics.h" |
3 | #include "lldb/Utility/Stream.h" |
4 | #include "lldb/lldb-forward.h" |
5 | #include "lldb/lldb-private-enumerations.h" |
6 | #include "lldb/lldb-private.h" |
7 | #include "llvm/Testing/Support/Error.h" |
8 | #include "gtest/gtest.h" |
9 | #include <thread> |
10 | |
11 | using namespace lldb_private; |
12 | using Duration = std::chrono::duration<double>; |
13 | |
14 | class DummySummaryImpl : public lldb_private::TypeSummaryImpl { |
15 | public: |
16 | DummySummaryImpl() |
17 | : TypeSummaryImpl(TypeSummaryImpl::Kind::eSummaryString, |
18 | TypeSummaryImpl::Flags()) {} |
19 | |
20 | std::string GetName() override { return "DummySummary"; } |
21 | |
22 | std::string GetSummaryKindName() override { return "dummy"; } |
23 | |
24 | std::string GetDescription() override { return ""; } |
25 | |
26 | bool FormatObject(ValueObject *valobj, std::string &dest, |
27 | const TypeSummaryOptions &options) override { |
28 | return false; |
29 | } |
30 | }; |
31 | |
32 | TEST(MultithreadFormatting, Multithread) { |
33 | SummaryStatisticsCache statistics_cache; |
34 | DummySummaryImpl summary; |
35 | std::vector<std::thread> threads; |
36 | for (int i = 0; i < 10; ++i) { |
37 | threads.emplace_back(args: std::thread([&statistics_cache, &summary]() { |
38 | auto sp = statistics_cache.GetSummaryStatisticsForProvider(provider&: summary); |
39 | { |
40 | SummaryStatistics::SummaryInvocation invocation(sp); |
41 | std::this_thread::sleep_for(rtime: Duration(1)); |
42 | } |
43 | })); |
44 | } |
45 | |
46 | for (auto &thread : threads) |
47 | thread.join(); |
48 | |
49 | auto sp = statistics_cache.GetSummaryStatisticsForProvider(provider&: summary); |
50 | ASSERT_TRUE(sp->GetDurationReference().get().count() > 10); |
51 | ASSERT_TRUE(sp->GetSummaryCount() == 10); |
52 | |
53 | std::string stats_as_json; |
54 | llvm::raw_string_ostream ss(stats_as_json); |
55 | ss << sp->ToJSON(); |
56 | ASSERT_THAT(stats_as_json, ::testing::HasSubstr("\"name\":\"DummySummary\"")); |
57 | ASSERT_THAT(stats_as_json, ::testing::HasSubstr("\"type\":\"dummy\"")); |
58 | } |
59 |