Skip to content
This repository was archived by the owner on Apr 3, 2025. It is now read-only.

Commit 6fadff5

Browse files
author
Luc Sinet
committed
develop #comment Use chrono literals to make the tests easier to read.
1 parent ec6dc3d commit 6fadff5

7 files changed

Lines changed: 48 additions & 49 deletions

File tree

test/Async.hh

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
#ifndef TASK_TEST_ASYNC_HH
22
#define TASK_TEST_ASYNC_HH
33

4-
#include <future>
5-
#include <memory>
6-
#include <thread>
4+
#include <chrono>
75

86
namespace Async {
97

10-
constexpr int kTestTimeout = 30'000;
8+
constexpr std::chrono::milliseconds kTestTimeout = std::chrono::milliseconds(30'000);
119

1210
} /* namespace Async */
1311

test/ManagerTest.cpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include "test/ManagerTest.hh"
22

3+
using namespace std::chrono_literals;
4+
35
namespace Task {
46

57
TEST_F(ManagerTest, launchOne) {
@@ -41,11 +43,11 @@ TEST_F(ManagerTest, launchDependentInParallel) {
4143
this->addTasks({
4244
[&p2, &f1] {
4345
p2.set_value();
44-
EXPECT_EQ(std::future_status::ready, f1.wait_for(std::chrono::milliseconds(Async::kTestTimeout)));
46+
EXPECT_EQ(std::future_status::ready, f1.wait_for(Async::kTestTimeout));
4547
},
4648
[&p1, &f2] {
4749
p1.set_value();
48-
EXPECT_EQ(std::future_status::ready, f2.wait_for(std::chrono::milliseconds(Async::kTestTimeout)));
50+
EXPECT_EQ(std::future_status::ready, f2.wait_for(Async::kTestTimeout));
4951
},
5052
});
5153
this->runTasks();
@@ -61,11 +63,11 @@ TEST_F(ManagerTest, launchDependentSequentially) {
6163
this->addTasks({
6264
[&p2, &f1] {
6365
p2.set_value();
64-
EXPECT_EQ(std::future_status::timeout, f1.wait_for(std::chrono::milliseconds(1)));
66+
EXPECT_EQ(std::future_status::timeout, f1.wait_for(1ns));
6567
},
6668
[&p1, &f2] {
6769
p1.set_value();
68-
EXPECT_EQ(std::future_status::ready, f2.wait_for(std::chrono::milliseconds(Async::kTestTimeout)));
70+
EXPECT_EQ(std::future_status::ready, f2.wait_for(Async::kTestTimeout));
6971
},
7072
});
7173
this->runTasks();
@@ -80,7 +82,7 @@ TEST_F(ManagerTest, launchNested) {
8082

8183
_manager->launch([promise = std::move(promise)]() mutable { promise.set_value(); });
8284

83-
EXPECT_EQ(std::future_status::ready, future.wait_for(std::chrono::milliseconds(Async::kTestTimeout)));
85+
EXPECT_EQ(std::future_status::ready, future.wait_for(Async::kTestTimeout));
8486
},
8587
});
8688

@@ -92,7 +94,7 @@ TEST_F(ManagerTest, stop) {
9294
this->addTasks({
9395
// We make the worker sleep for a short period of time so that when it wakes up, if the stop wasn't waiting for
9496
// all tasks to complete, it would access to a destroyed manager.
95-
[] { std::this_thread::sleep_for(std::chrono::milliseconds(5)); },
97+
[] { std::this_thread::sleep_for(5ms); },
9698
});
9799
_futures.clear(); // Don't wait for the task to finish before stopping.
98100
this->runTasks();
@@ -136,9 +138,9 @@ TEST_F(ManagerTest, multipleManagers) {
136138
});
137139
}
138140

139-
EXPECT_EQ(std::future_status::ready, managerA->stop().wait_for(std::chrono::milliseconds(Async::kTestTimeout)));
140-
EXPECT_EQ(std::future_status::ready, managerB->stop().wait_for(std::chrono::milliseconds(Async::kTestTimeout)));
141-
EXPECT_EQ(std::future_status::ready, managerC->stop().wait_for(std::chrono::milliseconds(Async::kTestTimeout)));
141+
EXPECT_EQ(std::future_status::ready, managerA->stop().wait_for(Async::kTestTimeout));
142+
EXPECT_EQ(std::future_status::ready, managerB->stop().wait_for(Async::kTestTimeout));
143+
EXPECT_EQ(std::future_status::ready, managerC->stop().wait_for(Async::kTestTimeout));
142144
}
143145

144146
} /* namespace Task */

test/ManagerTest.hh

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class ManagerTest : public Test {
2121
~ManagerTest() {
2222
if (_manager) {
2323
auto done = _manager->stop();
24-
EXPECT_EQ(std::future_status::ready, done.wait_for(std::chrono::milliseconds(Async::kTestTimeout)));
24+
EXPECT_EQ(std::future_status::ready, done.wait_for(Async::kTestTimeout));
2525
}
2626
};
2727

@@ -35,9 +35,7 @@ class ManagerTest : public Test {
3535
_manager = std::make_shared<Manager>(_threadpool, managerWorkersCount);
3636

3737
for (size_t i = 0; i < managerWorkersCount; ++i) {
38-
auto task = [this] {
39-
EXPECT_EQ(std::future_status::ready, _lock.second.wait_for(std::chrono::milliseconds(Async::kTestTimeout)));
40-
};
38+
auto task = [this] { EXPECT_EQ(std::future_status::ready, _lock.second.wait_for(Async::kTestTimeout)); };
4139
_manager->launch(std::move(task));
4240
}
4341
}
@@ -52,7 +50,7 @@ class ManagerTest : public Test {
5250
_lock.first->set_value();
5351
}
5452
for (auto& future : _futures) {
55-
EXPECT_EQ(std::future_status::ready, future.wait_for(std::chrono::milliseconds(Async::kTestTimeout)));
53+
EXPECT_EQ(std::future_status::ready, future.wait_for(Async::kTestTimeout));
5654
}
5755
_futures.clear();
5856
}

test/SchedulerTest.cpp

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
#include <atomic>
44

5+
using namespace std::chrono_literals;
6+
57
namespace Task {
68

79
TEST_F(SchedulerTest, scheduleOne) {
@@ -58,14 +60,14 @@ TEST_F(SchedulerTest, scheduleDependentInParallel) {
5860
this->addTasks({
5961
{ [&p2, &f1] {
6062
p2.set_value();
61-
EXPECT_EQ(std::future_status::ready, f1.wait_for(std::chrono::milliseconds(Async::kTestTimeout)));
63+
EXPECT_EQ(std::future_status::ready, f1.wait_for(Async::kTestTimeout));
6264
},
63-
std::chrono::microseconds(0) },
65+
0us },
6466
{ [&p1, &f2] {
6567
p1.set_value();
66-
EXPECT_EQ(std::future_status::ready, f2.wait_for(std::chrono::milliseconds(Async::kTestTimeout)));
68+
EXPECT_EQ(std::future_status::ready, f2.wait_for(Async::kTestTimeout));
6769
},
68-
std::chrono::microseconds(0) },
70+
0us },
6971
});
7072
this->runTasks();
7173
}
@@ -82,12 +84,12 @@ TEST_F(SchedulerTest, scheduleDependentSequentially) {
8284
p2.set_value();
8385
EXPECT_EQ(std::future_status::timeout, f1.wait_for(std::chrono::milliseconds(1)));
8486
},
85-
std::chrono::microseconds(0) },
87+
0us },
8688
{ [&p1, &f2] {
8789
p1.set_value();
88-
EXPECT_EQ(std::future_status::ready, f2.wait_for(std::chrono::milliseconds(Async::kTestTimeout)));
90+
EXPECT_EQ(std::future_status::ready, f2.wait_for(Async::kTestTimeout));
8991
},
90-
std::chrono::microseconds(0) },
92+
0us },
9193
});
9294
this->runTasks();
9395
}
@@ -99,12 +101,11 @@ TEST_F(SchedulerTest, scheduleNested) {
99101
std::promise<void> promise;
100102
auto future = promise.get_future();
101103

102-
_scheduler->scheduleIn(
103-
"0", std::chrono::microseconds(0), [promise = std::move(promise)]() mutable { promise.set_value(); });
104+
_scheduler->scheduleIn("0", 0us, [promise = std::move(promise)]() mutable { promise.set_value(); });
104105

105-
EXPECT_EQ(std::future_status::ready, future.wait_for(std::chrono::milliseconds(Async::kTestTimeout)));
106+
EXPECT_EQ(std::future_status::ready, future.wait_for(Async::kTestTimeout));
106107
},
107-
std::chrono::microseconds(0) },
108+
0us },
108109
});
109110

110111
this->runTasks();
@@ -124,8 +125,8 @@ TEST_F(SchedulerTest, schedulePeriodic) {
124125
}
125126
});
126127

127-
EXPECT_EQ(std::future_status::ready, f.wait_for(std::chrono::milliseconds(Async::kTestTimeout)));
128-
EXPECT_EQ(std::future_status::ready, scheduler->stop().wait_for(std::chrono::milliseconds(Async::kTestTimeout)));
128+
EXPECT_EQ(std::future_status::ready, f.wait_for(Async::kTestTimeout));
129+
EXPECT_EQ(std::future_status::ready, scheduler->stop().wait_for(Async::kTestTimeout));
129130
}
130131

131132
TEST_F(SchedulerTest, multiplePeriodicTasksOneWorker) {
@@ -154,22 +155,22 @@ TEST_F(SchedulerTest, multiplePeriodicTasksOneWorker) {
154155
});
155156
// Unblock the scheduler.
156157
this->runTasks();
157-
EXPECT_EQ(std::future_status::ready, f.wait_for(std::chrono::milliseconds(Async::kTestTimeout)));
158+
EXPECT_EQ(std::future_status::ready, f.wait_for(Async::kTestTimeout));
158159

159160
EXPECT_EQ(expected, final);
160-
EXPECT_EQ(std::future_status::ready, scheduler->stop().wait_for(std::chrono::milliseconds(Async::kTestTimeout)));
161+
EXPECT_EQ(std::future_status::ready, scheduler->stop().wait_for(Async::kTestTimeout));
161162
}
162163

163164
TEST_F(SchedulerTest, erase) {
164165
size_t n = 0;
165166

166167
this->setup(1, 1);
167168
this->addTasks({
168-
{ [&n] { ++n; }, std::chrono::microseconds(0) },
169-
{ [&n] { ++n; }, std::chrono::microseconds(1) },
170-
{ [&n] { ++n; }, std::chrono::microseconds(2) },
171-
{ [&n] { ++n; }, std::chrono::microseconds(3) },
172-
{ [&n] { ++n; }, std::chrono::microseconds(4) },
169+
{ [&n] { ++n; }, 0us },
170+
{ [&n] { ++n; }, 1us },
171+
{ [&n] { ++n; }, 2us },
172+
{ [&n] { ++n; }, 3us },
173+
{ [&n] { ++n; }, 4us },
173174
});
174175
_scheduler->remove("1");
175176
_scheduler->remove("4");
@@ -182,7 +183,7 @@ TEST_F(SchedulerTest, erase) {
182183
TEST_F(SchedulerTest, checkTaskIsScheduled) {
183184
this->setup(1, 1);
184185
this->addTasks({
185-
{ [] {}, std::chrono::microseconds(0) },
186+
{ [] {}, 0us },
186187
});
187188
EXPECT_TRUE(_scheduler->isScheduled("0"));
188189
this->runTasks();
@@ -193,21 +194,21 @@ TEST_F(SchedulerTest, stopAndDiscard) {
193194

194195
this->setup(1, 1);
195196
this->addTasks({
196-
{ [&n] { n = 1; }, std::chrono::minutes(1) },
197+
{ [&n] { n = 1; }, 1min },
197198
});
198199
auto done = _scheduler->stop(true);
199200

200201
this->runTasks();
201-
EXPECT_EQ(std::future_status::ready, done.wait_for(std::chrono::milliseconds(Async::kTestTimeout)));
202+
EXPECT_EQ(std::future_status::ready, done.wait_for(Async::kTestTimeout));
202203
EXPECT_EQ(0, n);
203204
}
204205

205206
TEST_F(SchedulerTest, scheduleOnStopped) {
206207
auto threadpool = std::make_shared<Detail::Threadpool>(2);
207208
auto scheduler = std::make_shared<Scheduler>(threadpool, 1);
208209

209-
EXPECT_EQ(std::future_status::ready, scheduler->stop().wait_for(std::chrono::milliseconds(Async::kTestTimeout)));
210-
auto future = scheduler->scheduleIn("0", std::chrono::milliseconds(0), [] {});
210+
EXPECT_EQ(std::future_status::ready, scheduler->stop().wait_for(Async::kTestTimeout));
211+
auto future = scheduler->scheduleIn("0", 0us, [] {});
211212

212213
EXPECT_FALSE(scheduler->isScheduled("0"));
213214
EXPECT_TRUE(future.valid());

test/SchedulerTest.hh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class SchedulerTest : public Test {
2222
~SchedulerTest() {
2323
if (_scheduler) {
2424
auto done = _scheduler->stop();
25-
EXPECT_EQ(std::future_status::ready, done.wait_for(std::chrono::milliseconds(Async::kTestTimeout)));
25+
EXPECT_EQ(std::future_status::ready, done.wait_for(Async::kTestTimeout));
2626
}
2727
};
2828

@@ -40,7 +40,7 @@ class SchedulerTest : public Test {
4040
auto scheduler = std::make_shared<Scheduler>(_threadpool, schedulerWorkersCount);
4141
for (size_t i = 0; i < schedulerWorkersCount; ++i) {
4242
auto task = [this] {
43-
EXPECT_EQ(std::future_status::ready, _lock.second.wait_for(std::chrono::milliseconds(Async::kTestTimeout)));
43+
EXPECT_EQ(std::future_status::ready, _lock.second.wait_for(Async::kTestTimeout));
4444
};
4545
scheduler->scheduleIn("setup_" + std::to_string(i), -std::chrono::hours(1), std::move(task));
4646
}
@@ -59,7 +59,7 @@ class SchedulerTest : public Test {
5959
_lock.first->set_value();
6060
}
6161
for (auto& future : _futures) {
62-
EXPECT_EQ(std::future_status::ready, future.wait_for(std::chrono::milliseconds(Async::kTestTimeout)));
62+
EXPECT_EQ(std::future_status::ready, future.wait_for(Async::kTestTimeout));
6363
}
6464
_futures.clear();
6565
}

test/detail/ThreadpoolTest.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ TEST_F(DetailThreadpool, UseThreadpoolFromTask) {
5353
};
5454
_threadpool.execute(TimedTask{ std::move(task), Clock::now() });
5555

56-
EXPECT_EQ(std::future_status::ready, future.wait_for(std::chrono::milliseconds(Async::kTestTimeout)));
56+
EXPECT_EQ(std::future_status::ready, future.wait_for(Async::kTestTimeout));
5757
}
5858

5959
} /* namespace Detail */

test/detail/ThreadpoolTest.hh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class DetailThreadpool : public Test {
2727
std::shared_future<void> future = lock->get_future();
2828

2929
auto task = [future = std::move(future)] {
30-
EXPECT_EQ(std::future_status::ready, future.wait_for(std::chrono::milliseconds(Async::kTestTimeout)));
30+
EXPECT_EQ(std::future_status::ready, future.wait_for(Async::kTestTimeout));
3131
};
3232
_threadpool.execute(TimedTask{ std::move(task), Clock::now() - std::chrono::hours(1) }); // arbitrary value.
3333

@@ -57,7 +57,7 @@ class DetailThreadpool : public Test {
5757

5858
lock->set_value();
5959
for (size_t i = 0; i < futures.size(); ++i) {
60-
EXPECT_EQ(std::future_status::ready, futures[i].wait_for(std::chrono::milliseconds(Async::kTestTimeout)));
60+
EXPECT_EQ(std::future_status::ready, futures[i].wait_for(Async::kTestTimeout));
6161
EXPECT_TRUE(measuredDelay[i] >= delays[i]);
6262
}
6363

0 commit comments

Comments
 (0)