File: ProgressReportTest.cpp

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (322 lines) | stat: -rw-r--r-- 12,884 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
//===-- ProgressReportTest.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 "Plugins/Platform/MacOSX/PlatformMacOSX.h"
#include "Plugins/Platform/MacOSX/PlatformRemoteMacOSX.h"
#include "TestingSupport/SubsystemRAII.h"
#include "TestingSupport/TestUtilities.h"
#include "lldb/Core/Debugger.h"
#include "lldb/Core/Progress.h"
#include "lldb/Host/FileSystem.h"
#include "lldb/Host/HostInfo.h"
#include "lldb/Utility/Listener.h"
#include "gtest/gtest.h"
#include <memory>
#include <mutex>

using namespace lldb;
using namespace lldb_private;

static std::chrono::milliseconds TIMEOUT(500);

class ProgressReportTest : public ::testing::Test {
public:
  ListenerSP CreateListenerFor(uint32_t bit) {
    // Set up the debugger, make sure that was done properly.
    ArchSpec arch("x86_64-apple-macosx-");
    Platform::SetHostPlatform(
        PlatformRemoteMacOSX::CreateInstance(true, &arch));

    m_debugger_sp = Debugger::CreateInstance();

    // Get the debugger's broadcaster.
    Broadcaster &broadcaster = m_debugger_sp->GetBroadcaster();

    // Create a listener, make sure it can receive events and that it's
    // listening to the correct broadcast bit.
    m_listener_sp = Listener::MakeListener("progress-listener");
    m_listener_sp->StartListeningForEvents(&broadcaster, bit);
    return m_listener_sp;
  }

protected:
  // The debugger's initialization function can't be called with no arguments
  // so calling it using SubsystemRAII will cause the test build to fail as
  // SubsystemRAII will call Initialize with no arguments. As such we set it up
  // here the usual way.
  void SetUp() override {
    std::call_once(TestUtilities::g_debugger_initialize_flag,
                   []() { Debugger::Initialize(nullptr); });
  };

  DebuggerSP m_debugger_sp;
  ListenerSP m_listener_sp;
  SubsystemRAII<FileSystem, HostInfo, PlatformMacOSX, ProgressManager>
      subsystems;
};

TEST_F(ProgressReportTest, TestReportCreation) {
  ListenerSP listener_sp = CreateListenerFor(lldb::eBroadcastBitProgress);
  EventSP event_sp;
  const ProgressEventData *data;

  // Scope this for RAII on the progress objects.
  // Create progress reports and check that their respective events for having
  // started and ended are broadcasted.
  {
    Progress progress1("Progress report 1", "Starting report 1");
    Progress progress2("Progress report 2", "Starting report 2");
    Progress progress3("Progress report 3", "Starting report 3");
  }

  // Start popping events from the queue, they should have been recevied
  // in this order:
  // Starting progress: 1, 2, 3
  // Ending progress: 3, 2, 1
  ASSERT_TRUE(listener_sp->GetEvent(event_sp, TIMEOUT));
  data = ProgressEventData::GetEventDataFromEvent(event_sp.get());

  EXPECT_EQ(data->GetDetails(), "Starting report 1");
  EXPECT_FALSE(data->IsFinite());
  EXPECT_FALSE(data->GetCompleted());
  EXPECT_EQ(data->GetTotal(), Progress::kNonDeterministicTotal);
  EXPECT_EQ(data->GetMessage(), "Progress report 1: Starting report 1");

  ASSERT_TRUE(listener_sp->GetEvent(event_sp, TIMEOUT));
  data = ProgressEventData::GetEventDataFromEvent(event_sp.get());

  EXPECT_EQ(data->GetDetails(), "Starting report 2");
  EXPECT_FALSE(data->IsFinite());
  EXPECT_FALSE(data->GetCompleted());
  EXPECT_EQ(data->GetTotal(), Progress::kNonDeterministicTotal);
  EXPECT_EQ(data->GetMessage(), "Progress report 2: Starting report 2");

  ASSERT_TRUE(listener_sp->GetEvent(event_sp, TIMEOUT));
  data = ProgressEventData::GetEventDataFromEvent(event_sp.get());

  EXPECT_EQ(data->GetDetails(), "Starting report 3");
  EXPECT_FALSE(data->IsFinite());
  EXPECT_FALSE(data->GetCompleted());
  EXPECT_EQ(data->GetTotal(), Progress::kNonDeterministicTotal);
  EXPECT_EQ(data->GetMessage(), "Progress report 3: Starting report 3");

  // Progress report objects should be destroyed at this point so
  // get each report from the queue and check that they've been
  // destroyed in reverse order.
  ASSERT_TRUE(listener_sp->GetEvent(event_sp, TIMEOUT));
  data = ProgressEventData::GetEventDataFromEvent(event_sp.get());

  EXPECT_EQ(data->GetTitle(), "Progress report 3");
  EXPECT_TRUE(data->GetCompleted());
  EXPECT_FALSE(data->IsFinite());
  EXPECT_EQ(data->GetMessage(), "Progress report 3: Starting report 3");

  ASSERT_TRUE(listener_sp->GetEvent(event_sp, TIMEOUT));
  data = ProgressEventData::GetEventDataFromEvent(event_sp.get());

  EXPECT_EQ(data->GetTitle(), "Progress report 2");
  EXPECT_TRUE(data->GetCompleted());
  EXPECT_FALSE(data->IsFinite());
  EXPECT_EQ(data->GetMessage(), "Progress report 2: Starting report 2");

  ASSERT_TRUE(listener_sp->GetEvent(event_sp, TIMEOUT));
  data = ProgressEventData::GetEventDataFromEvent(event_sp.get());

  EXPECT_EQ(data->GetTitle(), "Progress report 1");
  EXPECT_TRUE(data->GetCompleted());
  EXPECT_FALSE(data->IsFinite());
  EXPECT_EQ(data->GetMessage(), "Progress report 1: Starting report 1");
}

TEST_F(ProgressReportTest, TestReportDestructionWithPartialProgress) {
  ListenerSP listener_sp = CreateListenerFor(lldb::eBroadcastBitProgress);
  EventSP event_sp;
  const ProgressEventData *data;

  // Create a finite progress report and only increment to a non-completed
  // state before destruction.
  {
    Progress progress("Finite progress", "Report 1", 100);
    progress.Increment(3);
  }

  // Verify that the progress in the events are:
  // 1. At construction: 0 out of 100
  // 2. At increment: 3 out of 100
  // 3. At destruction: 100 out of 100
  ASSERT_TRUE(listener_sp->GetEvent(event_sp, TIMEOUT));
  data = ProgressEventData::GetEventDataFromEvent(event_sp.get());
  EXPECT_EQ(data->GetDetails(), "Report 1");
  EXPECT_TRUE(data->IsFinite());
  EXPECT_EQ(data->GetCompleted(), (uint64_t)0);
  EXPECT_EQ(data->GetTotal(), (uint64_t)100);
  EXPECT_EQ(data->GetMessage(), "Finite progress: Report 1");

  ASSERT_TRUE(listener_sp->GetEvent(event_sp, TIMEOUT));
  data = ProgressEventData::GetEventDataFromEvent(event_sp.get());
  EXPECT_EQ(data->GetDetails(), "Report 1");
  EXPECT_TRUE(data->IsFinite());
  EXPECT_EQ(data->GetCompleted(), (uint64_t)3);
  EXPECT_EQ(data->GetTotal(), (uint64_t)100);
  EXPECT_EQ(data->GetMessage(), "Finite progress: Report 1");

  ASSERT_TRUE(listener_sp->GetEvent(event_sp, TIMEOUT));
  data = ProgressEventData::GetEventDataFromEvent(event_sp.get());
  EXPECT_EQ(data->GetDetails(), "Report 1");
  EXPECT_TRUE(data->IsFinite());
  EXPECT_EQ(data->GetCompleted(), (uint64_t)100);
  EXPECT_EQ(data->GetTotal(), (uint64_t)100);
  EXPECT_EQ(data->GetMessage(), "Finite progress: Report 1");

  // Create an infinite progress report and increment by some amount.
  {
    Progress progress("Infinite progress", "Report 2");
    progress.Increment(3);
  }

  // Verify that the progress in the events are:
  // 1. At construction: 0
  // 2. At increment: 3
  // 3. At destruction: Progress::kNonDeterministicTotal
  ASSERT_TRUE(listener_sp->GetEvent(event_sp, TIMEOUT));
  data = ProgressEventData::GetEventDataFromEvent(event_sp.get());
  EXPECT_EQ(data->GetDetails(), "Report 2");
  EXPECT_FALSE(data->IsFinite());
  EXPECT_EQ(data->GetCompleted(), (uint64_t)0);
  EXPECT_EQ(data->GetTotal(), Progress::kNonDeterministicTotal);
  EXPECT_EQ(data->GetMessage(), "Infinite progress: Report 2");

  ASSERT_TRUE(listener_sp->GetEvent(event_sp, TIMEOUT));
  data = ProgressEventData::GetEventDataFromEvent(event_sp.get());
  EXPECT_EQ(data->GetDetails(), "Report 2");
  EXPECT_FALSE(data->IsFinite());
  EXPECT_EQ(data->GetCompleted(), (uint64_t)3);
  EXPECT_EQ(data->GetTotal(), Progress::kNonDeterministicTotal);
  EXPECT_EQ(data->GetMessage(), "Infinite progress: Report 2");

  ASSERT_TRUE(listener_sp->GetEvent(event_sp, TIMEOUT));
  data = ProgressEventData::GetEventDataFromEvent(event_sp.get());
  EXPECT_EQ(data->GetDetails(), "Report 2");
  EXPECT_FALSE(data->IsFinite());
  EXPECT_EQ(data->GetCompleted(), Progress::kNonDeterministicTotal);
  EXPECT_EQ(data->GetTotal(), Progress::kNonDeterministicTotal);
  EXPECT_EQ(data->GetMessage(), "Infinite progress: Report 2");
}

TEST_F(ProgressReportTest, TestProgressManager) {
  ListenerSP listener_sp =
      CreateListenerFor(lldb::eBroadcastBitProgressCategory);
  EventSP event_sp;
  const ProgressEventData *data;

  // Create three progress events with the same category then try to pop 2
  // events from the queue in a row before the progress reports are destroyed.
  // Since only 1 event should've been broadcast for this category, the second
  // GetEvent() call should return false.
  {
    Progress progress1("Progress report 1", "Starting report 1");
    Progress progress2("Progress report 1", "Starting report 2");
    Progress progress3("Progress report 1", "Starting report 3");
    ASSERT_TRUE(listener_sp->GetEvent(event_sp, TIMEOUT));
    ASSERT_FALSE(listener_sp->GetEvent(event_sp, TIMEOUT));
  }

  data = ProgressEventData::GetEventDataFromEvent(event_sp.get());

  EXPECT_EQ(data->GetDetails(), "");
  EXPECT_FALSE(data->IsFinite());
  EXPECT_FALSE(data->GetCompleted());
  EXPECT_EQ(data->GetTotal(), Progress::kNonDeterministicTotal);
  EXPECT_EQ(data->GetMessage(), "Progress report 1");

  // Pop another event from the queue, this should be the event for the final
  // report for this category.
  ASSERT_TRUE(listener_sp->GetEvent(event_sp, TIMEOUT));
  data = ProgressEventData::GetEventDataFromEvent(event_sp.get());

  EXPECT_EQ(data->GetDetails(), "");
  EXPECT_FALSE(data->IsFinite());
  EXPECT_TRUE(data->GetCompleted());
  EXPECT_EQ(data->GetTotal(), Progress::kNonDeterministicTotal);
  EXPECT_EQ(data->GetMessage(), "Progress report 1");
}

TEST_F(ProgressReportTest, TestOverlappingEvents) {
  ListenerSP listener_sp =
      CreateListenerFor(lldb::eBroadcastBitProgressCategory);
  EventSP event_sp;
  const ProgressEventData *data;

  // Create two progress reports of the same category that overlap with each
  // other. Here we want to ensure that the ID broadcasted for the initial and
  // final reports for this category are the same.
  std::unique_ptr<Progress> overlap_progress1 =
      std::make_unique<Progress>("Overlapping report 1", "Starting report 1");
  std::unique_ptr<Progress> overlap_progress2 =
      std::make_unique<Progress>("Overlapping report 1", "Starting report 2");
  overlap_progress1.reset();

  ASSERT_TRUE(listener_sp->GetEvent(event_sp, TIMEOUT));
  data = ProgressEventData::GetEventDataFromEvent(event_sp.get());
  // Get the ID used in the first report for this category.
  uint64_t expected_progress_id = data->GetID();

  EXPECT_EQ(data->GetDetails(), "");
  EXPECT_FALSE(data->IsFinite());
  EXPECT_FALSE(data->GetCompleted());
  EXPECT_EQ(data->GetTotal(), Progress::kNonDeterministicTotal);
  EXPECT_EQ(data->GetMessage(), "Overlapping report 1");

  overlap_progress2.reset();

  ASSERT_TRUE(listener_sp->GetEvent(event_sp, TIMEOUT));
  data = ProgressEventData::GetEventDataFromEvent(event_sp.get());

  EXPECT_EQ(data->GetDetails(), "");
  EXPECT_FALSE(data->IsFinite());
  EXPECT_TRUE(data->GetCompleted());
  EXPECT_EQ(data->GetTotal(), Progress::kNonDeterministicTotal);
  EXPECT_EQ(data->GetMessage(), "Overlapping report 1");
  // The progress ID for the final report should be the same as that for the
  // initial report.
  EXPECT_EQ(data->GetID(), expected_progress_id);
}

TEST_F(ProgressReportTest, TestProgressManagerDisjointReports) {
  ListenerSP listener_sp =
      CreateListenerFor(lldb::eBroadcastBitProgressCategory);
  EventSP event_sp;
  const ProgressEventData *data;
  uint64_t expected_progress_id;

  { Progress progress("Coalesced report 1", "Starting report 1"); }
  { Progress progress("Coalesced report 1", "Starting report 2"); }
  { Progress progress("Coalesced report 1", "Starting report 3"); }

  ASSERT_TRUE(listener_sp->GetEvent(event_sp, TIMEOUT));
  data = ProgressEventData::GetEventDataFromEvent(event_sp.get());
  expected_progress_id = data->GetID();

  EXPECT_EQ(data->GetDetails(), "");
  EXPECT_FALSE(data->IsFinite());
  EXPECT_FALSE(data->GetCompleted());
  EXPECT_EQ(data->GetTotal(), Progress::kNonDeterministicTotal);
  EXPECT_EQ(data->GetMessage(), "Coalesced report 1");

  ASSERT_TRUE(listener_sp->GetEvent(event_sp, TIMEOUT));
  data = ProgressEventData::GetEventDataFromEvent(event_sp.get());

  EXPECT_EQ(data->GetID(), expected_progress_id);
  EXPECT_EQ(data->GetDetails(), "");
  EXPECT_FALSE(data->IsFinite());
  EXPECT_TRUE(data->GetCompleted());
  EXPECT_EQ(data->GetTotal(), Progress::kNonDeterministicTotal);
  EXPECT_EQ(data->GetMessage(), "Coalesced report 1");

  ASSERT_FALSE(listener_sp->GetEvent(event_sp, TIMEOUT));
}