Skip to content

Commit 159b4ef

Browse files
authored
Merge pull request #22058 from compnerd/chrono
SourceKit: use `std::chrono` in unittests
2 parents b2d68b8 + 2bc1ec5 commit 159b4ef

File tree

1 file changed

+17
-13
lines changed

1 file changed

+17
-13
lines changed

unittests/SourceKit/SwiftLang/EditingTest.cpp

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,11 @@
1919
#include "llvm/Support/Path.h"
2020
#include "llvm/Support/TargetSelect.h"
2121
#include "gtest/gtest.h"
22-
#include <mutex>
22+
23+
#include <chrono>
2324
#include <condition_variable>
25+
#include <mutex>
26+
#include <thread>
2427

2528
using namespace SourceKit;
2629
using namespace llvm;
@@ -177,7 +180,7 @@ class EditTest : public ::testing::Test {
177180
DocUpdState->HasUpdate = false;
178181
}
179182

180-
void doubleOpenWithDelay(useconds_t delay, bool close);
183+
void doubleOpenWithDelay(std::chrono::microseconds delay, bool close);
181184

182185
private:
183186
std::vector<const char *> makeArgs(const char *DocName,
@@ -229,7 +232,8 @@ TEST_F(EditTest, DiagsAfterEdit) {
229232
EXPECT_EQ(SemaDiagStage, Consumer.DiagStage);
230233
}
231234

232-
void EditTest::doubleOpenWithDelay(useconds_t delay, bool closeDoc) {
235+
void EditTest::doubleOpenWithDelay(std::chrono::microseconds delay,
236+
bool closeDoc) {
233237
const char *DocName = "/test.swift";
234238
const char *Contents =
235239
"func foo() { _ = unknown_name }\n";
@@ -239,8 +243,8 @@ void EditTest::doubleOpenWithDelay(useconds_t delay, bool closeDoc) {
239243
open(DocName, Contents, Args, Consumer);
240244
ASSERT_EQ(0u, Consumer.Diags.size());
241245
// Open again without closing; this reinitializes the semantic info on the doc
242-
if (delay)
243-
usleep(delay);
246+
if (delay > std::chrono::microseconds(0))
247+
std::this_thread::sleep_for(delay);
244248
if (closeDoc)
245249
close(DocName);
246250
reset(Consumer);
@@ -277,19 +281,19 @@ TEST_F(EditTest, DISABLED_DiagsAfterCloseAndReopen) {
277281
// The middle case in particular verifies the ASTManager is only calling the
278282
// correct ASTConsumers.
279283

280-
doubleOpenWithDelay(0, true);
281-
doubleOpenWithDelay(1000, true); // 1 ms
282-
doubleOpenWithDelay(10000, true); // 10 ms
283-
doubleOpenWithDelay(100000, true); // 100 ms
284+
doubleOpenWithDelay(std::chrono::microseconds(0), true);
285+
doubleOpenWithDelay(std::chrono::milliseconds(1), true);
286+
doubleOpenWithDelay(std::chrono::milliseconds(10), true);
287+
doubleOpenWithDelay(std::chrono::milliseconds(100), true);
284288
}
285289

286290
TEST_F(EditTest, DiagsAfterReopen) {
287291
// See description of DiagsAfterCloseAndReopen, but in this case we don't
288292
// close the original document, causing it to reinitialize instead of create
289293
// a fresh document.
290294

291-
doubleOpenWithDelay(0, false);
292-
doubleOpenWithDelay(1000, false); // 1 ms
293-
doubleOpenWithDelay(10000, false); // 10 ms
294-
doubleOpenWithDelay(100000, false); // 100 ms
295+
doubleOpenWithDelay(std::chrono::microseconds(0), false);
296+
doubleOpenWithDelay(std::chrono::milliseconds(1), false);
297+
doubleOpenWithDelay(std::chrono::milliseconds(10), false);
298+
doubleOpenWithDelay(std::chrono::milliseconds(100), false);
295299
}

0 commit comments

Comments
 (0)