Skip to content

Commit 84d996f

Browse files
committed
Proper sequencing of tests
1 parent 2765807 commit 84d996f

9 files changed

+128
-36
lines changed

test/CoDispatchTests.mm

+30-23
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
#include <filesystem>
1212
#include <chrono>
1313

14+
#include "TestGlobal.h"
15+
1416
using namespace std::literals;
1517
using namespace std::chrono;
1618

@@ -336,41 +338,41 @@ static auto checkDispatchToDifferentQueue() -> DispatchTask<> {
336338
return 1;
337339
});
338340
CHECK(i == 1);
339-
CHECK(!NSThread.isMainThread);
341+
CHECK(!isMainQueue());
340342

341343
i = co_await co_dispatch(conq, []() {
342344
[NSThread sleepForTimeInterval:0.2];
343345
return 2;
344346
}).resumeOnMainQueue();
345347
CHECK(i == 2);
346-
CHECK(NSThread.isMainThread);
348+
CHECK(isMainQueue());
347349

348350
co_await resumeOn(conq);
349-
CHECK(!NSThread.isMainThread);
351+
CHECK(!isMainQueue());
350352

351353
co_await resumeOnMainQueue();
352-
CHECK(NSThread.isMainThread);
354+
CHECK(isMainQueue());
353355

354356
co_await resumeOn(conq);
355-
CHECK(!NSThread.isMainThread);
357+
CHECK(!isMainQueue());
356358

357359
co_await resumeOnMainQueue(dispatch_time(DISPATCH_TIME_NOW, nanoseconds(200ms).count()));
358-
CHECK(NSThread.isMainThread);
360+
CHECK(isMainQueue());
359361

360362
co_await resumeOn(conq, dispatch_time(DISPATCH_TIME_NOW, nanoseconds(200ms).count()));
361-
CHECK(!NSThread.isMainThread);
363+
CHECK(!isMainQueue());
362364

363365
i = co_await delay(0.2, co_dispatch(dispatch_get_main_queue(), []() {
364366
return 3;
365367
}));
366368
CHECK(i == 3);
367-
CHECK(!NSThread.isMainThread);
369+
CHECK(!isMainQueue());
368370

369371
i = co_await delay(0.2, co_dispatch(conq, []() {
370372
return 4;
371373
}).resumeOn(dispatch_get_main_queue()));
372374
CHECK(i == 4);
373-
CHECK(NSThread.isMainThread);
375+
CHECK(isMainQueue());
374376
}
375377

376378
static auto checkMakeAwaitable() -> DispatchTask<> {
@@ -497,7 +499,7 @@ static auto checkTasks() -> DispatchTask<> {
497499
}
498500

499501
co_await resumeOnMainQueue();
500-
CHECK(NSThread.isMainThread);
502+
CHECK(isMainQueue());
501503

502504

503505
auto coro = []() -> DispatchTask<int> {
@@ -508,13 +510,13 @@ static auto checkTasks() -> DispatchTask<> {
508510
};
509511

510512
co_await coro().resumeOnMainQueue();
511-
CHECK(NSThread.isMainThread);
513+
CHECK(isMainQueue());
512514

513515
co_await coro().resumeOnMainQueue(dispatch_time(DISPATCH_TIME_NOW, nanoseconds(200ms).count()));
514-
CHECK(NSThread.isMainThread);
516+
CHECK(isMainQueue());
515517

516518
co_await delay(0.2, coro().resumeOnMainQueue(dispatch_time(DISPATCH_TIME_NOW, nanoseconds(1ms).count())));
517-
CHECK(NSThread.isMainThread);
519+
CHECK(isMainQueue());
518520
}
519521

520522
static auto checkGenerator() -> DispatchTask<> {
@@ -531,7 +533,7 @@ static auto checkGenerator() -> DispatchTask<> {
531533

532534
std::vector<int> res;
533535
for (auto it = co_await generate().resumingOnMainQueue().beginOn(conq); it; co_await it.next()) {
534-
CHECK(NSThread.isMainThread);
536+
CHECK(isMainQueue());
535537
res.push_back(*it);
536538
}
537539
CHECK(res == std::vector{1, 2, 3});
@@ -543,7 +545,7 @@ static auto checkGenerator() -> DispatchTask<> {
543545
};
544546
std::vector<int> res;
545547
for (auto it = co_await generate().begin(); it; co_await it.next()) {
546-
CHECK(NSThread.isMainThread);
548+
CHECK(isMainQueue());
547549
res.push_back(*it);
548550
}
549551
CHECK(res.empty());
@@ -699,13 +701,18 @@ static auto checkIO() -> DispatchTask<> {
699701
}
700702

701703
TEST_CASE("CoDispatchTests") {
702-
[]() -> DispatchTask<> {
703-
co_await checkReturnPropagation();
704-
co_await checkDispatchToDifferentQueue();
705-
co_await checkMakeAwaitable();
706-
co_await checkTasks();
707-
co_await checkGenerator();
708-
co_await checkIO();
709-
}();
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+
}();
715+
});
716+
waitForNoAsync();
710717
}
711718

test/CoDispatchTestsCpp.cpp

+8-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
#include <filesystem>
1010
#include <vector>
1111

12+
#include "TestGlobal.h"
13+
1214
static auto checkIO() -> DispatchTask<> {
1315

1416
auto conq = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0);
@@ -149,9 +151,14 @@ static DispatchTask<> runTests() {
149151
}
150152

151153
co_await checkIO();
154+
endAsync();
152155
}
153156

154157
TEST_CASE("CoDispatchTestsCpp") {
155-
runTests();
158+
startAsync();
159+
dispatch_async(dispatch_get_main_queue(), ^ {
160+
runTests();
161+
});
162+
waitForNoAsync();
156163
}
157164

test/CoDispatchTestsNoexcept.cpp

+12-6
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,20 @@
22

33
#include "doctest.h"
44

5+
#include "TestGlobal.h"
56

67

78
TEST_CASE("CoDispatchTestsNoExcept") {
89

9-
[]() -> DispatchTask<> {
10-
auto i = co_await co_dispatch([]() {
11-
return 7;
12-
});
13-
CHECK(i == 7);
14-
}();
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+
}();
19+
});
20+
waitForNoAsync();
1521
}

test/Makefile

+16-4
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,31 @@ 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 build
12+
build/main-linux.o: main-linux.cpp ../include/objc-helpers/BlockUtil.h doctest.h build
1313
$(CLANG) $(CPPFLAGS) -c -o $@ $<
1414

15-
build/BlockUtilTestCpp.o: BlockUtilTestCpp.cpp ../include/objc-helpers/BlockUtil.h build
15+
build/TestGlobal.o: TestGlobal.cpp TestGlobal.h build
1616
$(CLANG) $(CPPFLAGS) -c -o $@ $<
1717

18-
build/CoDispatchTestsCpp.o: CoDispatchTestsCpp.cpp ../include/objc-helpers/CoDispatch.h build
18+
build/BlockUtilTestCpp.o: BlockUtilTestCpp.cpp ../include/objc-helpers/BlockUtil.h doctest.h build
1919
$(CLANG) $(CPPFLAGS) -c -o $@ $<
2020

21-
build/CoDispatchTestsNoexcept.o: CoDispatchTestsNoexcept.cpp ../include/objc-helpers/CoDispatch.h build
21+
build/CoDispatchTestsCpp.o: CoDispatchTestsCpp.cpp \
22+
../include/objc-helpers/CoDispatch.h \
23+
TestGlobal.h \
24+
doctest.h \
25+
build
26+
$(CLANG) $(CPPFLAGS) -c -o $@ $<
27+
28+
build/CoDispatchTestsNoexcept.o: CoDispatchTestsNoexcept.cpp \
29+
../include/objc-helpers/CoDispatch.h \
30+
TestGlobal.h \
31+
doctest.h \
32+
build
2233
$(CLANG) $(CPPFLAGS) -fno-exceptions -c -o $@ $<
2334

2435
build/test: build/main-linux.o \
36+
build/TestGlobal.o \
2537
build/BlockUtilTestCpp.o \
2638
build/CoDispatchTestsCpp.o \
2739
build/CoDispatchTestsNoexcept.o

test/TestGlobal.cpp

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include "TestGlobal.h"
2+
3+
#include <mutex>
4+
#include <condition_variable>
5+
6+
#include <dispatch/dispatch.h>
7+
8+
9+
static int g_AsyncCount = 0;
10+
static std::mutex g_AsyncCountMutex;
11+
static std::condition_variable g_AsyncCountCond;
12+
dispatch_queue_t g_TestQueue;
13+
int g_IsMainKey;
14+
15+
void startAsync() {
16+
std::lock_guard lk(g_AsyncCountMutex);
17+
++g_AsyncCount;
18+
}
19+
20+
void endAsync() {
21+
std::lock_guard lk(g_AsyncCountMutex);
22+
if (--g_AsyncCount == 0)
23+
g_AsyncCountCond.notify_one();
24+
25+
}
26+
27+
void waitForNoAsync() {
28+
std::unique_lock lk(g_AsyncCountMutex);
29+
g_AsyncCountCond.wait(lk, []{ return g_AsyncCount == 0; });
30+
}
31+
32+
bool isMainQueue() {
33+
return (intptr_t)dispatch_get_specific(&g_IsMainKey) == 1;
34+
}
35+

test/TestGlobal.h

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#ifndef HEADER_TEST_GLOBAL_H_INCLUDED
2+
#define HEADER_TEST_GLOBAL_H_INCLUDED
3+
4+
void startAsync();
5+
void endAsync();
6+
void waitForNoAsync();
7+
bool isMainQueue();
8+
9+
#endif

test/main-linux.cpp

+6-1
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,15 @@
44

55
#include <dispatch/dispatch.h>
66

7+
extern dispatch_queue_t g_TestQueue;
8+
extern int g_IsMainKey;
9+
710
int main(int argc, const char * argv[]) {
8-
dispatch_async(dispatch_get_main_queue(), ^ {
11+
g_TestQueue = dispatch_queue_create("tests", DISPATCH_QUEUE_SERIAL);
12+
dispatch_async(g_TestQueue, ^ {
913
auto ret = doctest::Context(argc, argv).run();
1014
exit(ret);
1115
});
16+
dispatch_queue_set_specific(dispatch_get_main_queue(), &g_IsMainKey, (void*)1, nullptr);
1217
dispatch_main();
1318
}

test/main.mm

+6-1
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,19 @@
44

55
#include <dispatch/dispatch.h>
66

7+
extern dispatch_queue_t g_TestQueue;
8+
extern int g_IsMainKey;
9+
710
int main(int argc, const char * argv[]) {
811
@autoreleasepool {
9-
dispatch_async(dispatch_get_main_queue(), ^ {
12+
g_TestQueue = dispatch_queue_create("tests", DISPATCH_QUEUE_SERIAL);
13+
dispatch_async(g_TestQueue, ^ {
1014
@autoreleasepool {
1115
auto ret = doctest::Context(argc, argv).run();
1216
exit(ret);
1317
}
1418
});
19+
dispatch_queue_set_specific(dispatch_get_main_queue(), &g_IsMainKey, (void*)1, nullptr);
1520
dispatch_main();
1621
}
1722
}

test/test.xcodeproj/project.pbxproj

+6
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
441779372B24C4930036AF9F /* NSNumberUtilTests.mm in Sources */ = {isa = PBXBuildFile; fileRef = 441779362B24C4930036AF9F /* NSNumberUtilTests.mm */; };
3030
441779392B24C6B00036AF9F /* NSObjectUtilTests.mm in Sources */ = {isa = PBXBuildFile; fileRef = 441779382B24C6B00036AF9F /* NSObjectUtilTests.mm */; };
3131
4417793B2B26FEA70036AF9F /* CoDispatchTestsNoexcept.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4417793A2B26FEA60036AF9F /* CoDispatchTestsNoexcept.cpp */; settings = {COMPILER_FLAGS = "-fno-exceptions"; }; };
32+
4481ACCA2C65B3B6009521DB /* TestGlobal.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4481ACC92C65B3B1009521DB /* TestGlobal.cpp */; };
3233
448D57292B4E88A200A135E9 /* BlockUtilTestCpp.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 448D57282B4E88A200A135E9 /* BlockUtilTestCpp.cpp */; };
3334
448D572B2B50D28500A135E9 /* BlockUtilTest.mm in Sources */ = {isa = PBXBuildFile; fileRef = 448D572A2B50D28500A135E9 /* BlockUtilTest.mm */; };
3435
448D572E2B583C8400A135E9 /* NSStringUtilTestsCpp.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 448D572D2B583C8300A135E9 /* NSStringUtilTestsCpp.cpp */; };
@@ -74,6 +75,8 @@
7475
441779362B24C4930036AF9F /* NSNumberUtilTests.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = NSNumberUtilTests.mm; sourceTree = "<group>"; };
7576
441779382B24C6B00036AF9F /* NSObjectUtilTests.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = NSObjectUtilTests.mm; sourceTree = "<group>"; };
7677
4417793A2B26FEA60036AF9F /* CoDispatchTestsNoexcept.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = CoDispatchTestsNoexcept.cpp; sourceTree = "<group>"; };
78+
4481ACC82C65B35F009521DB /* TestGlobal.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = TestGlobal.h; sourceTree = "<group>"; };
79+
4481ACC92C65B3B1009521DB /* TestGlobal.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = TestGlobal.cpp; sourceTree = "<group>"; };
7780
448D57282B4E88A200A135E9 /* BlockUtilTestCpp.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = BlockUtilTestCpp.cpp; sourceTree = "<group>"; };
7881
448D572A2B50D28500A135E9 /* BlockUtilTest.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = BlockUtilTest.mm; sourceTree = "<group>"; };
7982
448D572D2B583C8300A135E9 /* NSStringUtilTestsCpp.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = NSStringUtilTestsCpp.cpp; sourceTree = "<group>"; };
@@ -102,6 +105,8 @@
102105
441779212B202F3E0036AF9F /* Library */,
103106
4417791C2B2013E00036AF9F /* doctest.h */,
104107
441779152B20136E0036AF9F /* main.mm */,
108+
4481ACC82C65B35F009521DB /* TestGlobal.h */,
109+
4481ACC92C65B3B1009521DB /* TestGlobal.cpp */,
105110
448D57282B4E88A200A135E9 /* BlockUtilTestCpp.cpp */,
106111
448D572A2B50D28500A135E9 /* BlockUtilTest.mm */,
107112
44B947E62B477A2700B68C7E /* BoxUtilTests.mm */,
@@ -231,6 +236,7 @@
231236
isa = PBXSourcesBuildPhase;
232237
buildActionMask = 2147483647;
233238
files = (
239+
4481ACCA2C65B3B6009521DB /* TestGlobal.cpp in Sources */,
234240
441779202B202DA30036AF9F /* CoDispatchTests.mm in Sources */,
235241
441779162B20136E0036AF9F /* main.mm in Sources */,
236242
441779372B24C4930036AF9F /* NSNumberUtilTests.mm in Sources */,

0 commit comments

Comments
 (0)