Skip to content

Commit d03bf31

Browse files
committed
Tests cleanup
1 parent e847090 commit d03bf31

10 files changed

+69
-62
lines changed

test/CoDispatchTests.mm

+12-12
Original file line numberDiff line numberDiff line change
@@ -700,19 +700,19 @@ static auto checkIO() -> DispatchTask<> {
700700
co_await resumeOnMainQueue();
701701
}
702702

703+
static DispatchTask<> runTests() {
704+
co_await checkReturnPropagation();
705+
co_await checkDispatchToDifferentQueue();
706+
co_await checkMakeAwaitable();
707+
co_await checkTasks();
708+
co_await checkGenerator();
709+
co_await checkIO();
710+
finishAsyncTest();
711+
}
712+
703713
TEST_CASE("CoDispatchTests") {
704-
startAsync();
705-
dispatch_async(dispatch_get_main_queue(), ^ {
706-
[]() -> DispatchTask<> {
707-
co_await checkReturnPropagation();
708-
co_await checkDispatchToDifferentQueue();
709-
co_await checkMakeAwaitable();
710-
co_await checkTasks();
711-
co_await checkGenerator();
712-
co_await checkIO();
713-
endAsync();
714-
}();
714+
waitForAsyncTest(^ {
715+
runTests();
715716
});
716-
waitForNoAsync();
717717
}
718718

test/CoDispatchTestsCpp.cpp

+2-4
Original file line numberDiff line numberDiff line change
@@ -151,14 +151,12 @@ static DispatchTask<> runTests() {
151151
}
152152

153153
co_await checkIO();
154-
endAsync();
154+
finishAsyncTest();
155155
}
156156

157157
TEST_CASE("CoDispatchTestsCpp") {
158-
startAsync();
159-
dispatch_async(dispatch_get_main_queue(), ^ {
158+
waitForAsyncTest(^ {
160159
runTests();
161160
});
162-
waitForNoAsync();
163161
}
164162

test/CoDispatchTestsNoexcept.cpp

+10-11
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,17 @@
44

55
#include "TestGlobal.h"
66

7+
static DispatchTask<> runTests() {
8+
auto i = co_await co_dispatch([]() {
9+
return 7;
10+
});
11+
CHECK(i == 7);
12+
finishAsyncTest();
13+
}
14+
715

816
TEST_CASE("CoDispatchTestsNoExcept") {
9-
10-
startAsync();
11-
dispatch_async(dispatch_get_main_queue(), ^ {
12-
[]() -> DispatchTask<> {
13-
auto i = co_await co_dispatch([]() {
14-
return 7;
15-
});
16-
CHECK(i == 7);
17-
endAsync();
18-
}();
17+
waitForAsyncTest(^ {
18+
runTests();
1919
});
20-
waitForNoAsync();
2120
}

test/Makefile

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ LDFLAGS:=--std=c++20 -fblocks -ldispatch -lBlocksRuntime
99
build:
1010
mkdir $@
1111

12-
build/main-linux.o: main-linux.cpp ../include/objc-helpers/BlockUtil.h doctest.h build
13-
$(CLANG) $(CPPFLAGS) -c -o $@ $<
12+
build/main.o: main.mm ../include/objc-helpers/BlockUtil.h doctest.h build
13+
$(CLANG) $(CPPFLAGS) -c -o $@ --language 'c++' $<
1414

1515
build/TestGlobal.o: TestGlobal.cpp TestGlobal.h build
1616
$(CLANG) $(CPPFLAGS) -c -o $@ $<
@@ -32,7 +32,7 @@ build/CoDispatchTestsNoexcept.o: CoDispatchTestsNoexcept.cpp \
3232
build
3333
$(CLANG) $(CPPFLAGS) -fno-exceptions -c -o $@ $<
3434

35-
build/test: build/main-linux.o \
35+
build/test: build/main.o \
3636
build/TestGlobal.o \
3737
build/BlockUtilTestCpp.o \
3838
build/CoDispatchTestsCpp.o \

test/TestGlobal.cpp

+19-5
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,41 @@
99
static int g_AsyncCount = 0;
1010
static std::mutex g_AsyncCountMutex;
1111
static std::condition_variable g_AsyncCountCond;
12-
dispatch_queue_t g_TestQueue;
13-
int g_IsMainKey;
12+
static int g_IsMainKey;
1413

15-
void startAsync() {
14+
static void startAsync() {
1615
std::lock_guard lk(g_AsyncCountMutex);
1716
++g_AsyncCount;
1817
}
1918

20-
void endAsync() {
19+
static void endAsync() {
2120
std::lock_guard lk(g_AsyncCountMutex);
2221
if (--g_AsyncCount == 0)
2322
g_AsyncCountCond.notify_one();
2423

2524
}
2625

27-
void waitForNoAsync() {
26+
static void waitForNoAsync() {
2827
std::unique_lock lk(g_AsyncCountMutex);
2928
g_AsyncCountCond.wait(lk, []{ return g_AsyncCount == 0; });
3029
}
3130

31+
void waitForAsyncTest(void (^block)()) {
32+
startAsync();
33+
dispatch_async(dispatch_get_main_queue(), block);
34+
waitForNoAsync();
35+
}
36+
37+
void finishAsyncTest() {
38+
endAsync();
39+
}
40+
3241
bool isMainQueue() {
3342
return (intptr_t)dispatch_get_specific(&g_IsMainKey) == 1;
3443
}
3544

45+
void runMainQueue() {
46+
dispatch_queue_set_specific(dispatch_get_main_queue(), &g_IsMainKey, (void*)intptr_t(1), nullptr);
47+
dispatch_main();
48+
}
49+

test/TestGlobal.h

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
#ifndef HEADER_TEST_GLOBAL_H_INCLUDED
22
#define HEADER_TEST_GLOBAL_H_INCLUDED
33

4-
void startAsync();
5-
void endAsync();
6-
void waitForNoAsync();
4+
void waitForAsyncTest(void (^block)());
5+
void finishAsyncTest();
6+
77
bool isMainQueue();
8+
void runMainQueue();
89

910
#endif

test/main-linux.cpp

-18
This file was deleted.

test/main.mm

+17-4
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,32 @@
44

55
#include <dispatch/dispatch.h>
66

7-
extern dispatch_queue_t g_TestQueue;
8-
extern int g_IsMainKey;
7+
#include "TestGlobal.h"
8+
9+
static dispatch_queue_t g_TestQueue;
910

1011
int main(int argc, const char * argv[]) {
12+
//We run tests on a separate serial queue because we can then block it waiting for async operations
13+
//launched from a test to finish. We cannot do this from main queue since some async tests must themselves
14+
//run on main queue. There is no way to portably have "modal loop" with libdispatch. On Mac only
15+
//we could run a runloop but not on Linux.
16+
#ifdef __OBJC__
1117
@autoreleasepool {
18+
#endif
1219
g_TestQueue = dispatch_queue_create("tests", DISPATCH_QUEUE_SERIAL);
1320
dispatch_async(g_TestQueue, ^ {
21+
#ifdef __OBJC__
1422
@autoreleasepool {
23+
#endif
1524
auto ret = doctest::Context(argc, argv).run();
1625
exit(ret);
26+
#ifdef __OBJC__
1727
}
28+
#endif
1829
});
19-
dispatch_queue_set_specific(dispatch_get_main_queue(), &g_IsMainKey, (void*)1, nullptr);
20-
dispatch_main();
30+
runMainQueue();
31+
32+
#ifdef __OBJC__
2133
}
34+
#endif
2235
}

test/test.xcodeproj/project.pbxproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@
181181
isa = PBXProject;
182182
attributes = {
183183
BuildIndependentTargetsInParallel = 1;
184-
LastUpgradeCheck = 1500;
184+
LastUpgradeCheck = 1600;
185185
TargetAttributes = {
186186
441779112B20136E0036AF9F = {
187187
CreatedOnToolsVersion = 15.0;

test/test.xcodeproj/xcshareddata/xcschemes/test.xcscheme

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<Scheme
3-
LastUpgradeVersion = "1500"
3+
LastUpgradeVersion = "1600"
44
version = "1.7">
55
<BuildAction
66
parallelizeBuildables = "YES"

0 commit comments

Comments
 (0)