Skip to content

Commit

Permalink
Proper sequencing of tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gershnik committed Aug 9, 2024
1 parent 2765807 commit 84d996f
Show file tree
Hide file tree
Showing 9 changed files with 128 additions and 36 deletions.
53 changes: 30 additions & 23 deletions test/CoDispatchTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
#include <filesystem>
#include <chrono>

#include "TestGlobal.h"

using namespace std::literals;
using namespace std::chrono;

Expand Down Expand Up @@ -336,41 +338,41 @@ static auto checkDispatchToDifferentQueue() -> DispatchTask<> {
return 1;
});
CHECK(i == 1);
CHECK(!NSThread.isMainThread);
CHECK(!isMainQueue());

i = co_await co_dispatch(conq, []() {
[NSThread sleepForTimeInterval:0.2];
return 2;
}).resumeOnMainQueue();
CHECK(i == 2);
CHECK(NSThread.isMainThread);
CHECK(isMainQueue());

co_await resumeOn(conq);
CHECK(!NSThread.isMainThread);
CHECK(!isMainQueue());

co_await resumeOnMainQueue();
CHECK(NSThread.isMainThread);
CHECK(isMainQueue());

co_await resumeOn(conq);
CHECK(!NSThread.isMainThread);
CHECK(!isMainQueue());

co_await resumeOnMainQueue(dispatch_time(DISPATCH_TIME_NOW, nanoseconds(200ms).count()));
CHECK(NSThread.isMainThread);
CHECK(isMainQueue());

co_await resumeOn(conq, dispatch_time(DISPATCH_TIME_NOW, nanoseconds(200ms).count()));
CHECK(!NSThread.isMainThread);
CHECK(!isMainQueue());

i = co_await delay(0.2, co_dispatch(dispatch_get_main_queue(), []() {
return 3;
}));
CHECK(i == 3);
CHECK(!NSThread.isMainThread);
CHECK(!isMainQueue());

i = co_await delay(0.2, co_dispatch(conq, []() {
return 4;
}).resumeOn(dispatch_get_main_queue()));
CHECK(i == 4);
CHECK(NSThread.isMainThread);
CHECK(isMainQueue());
}

static auto checkMakeAwaitable() -> DispatchTask<> {
Expand Down Expand Up @@ -497,7 +499,7 @@ static auto checkTasks() -> DispatchTask<> {
}

co_await resumeOnMainQueue();
CHECK(NSThread.isMainThread);
CHECK(isMainQueue());


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

co_await coro().resumeOnMainQueue();
CHECK(NSThread.isMainThread);
CHECK(isMainQueue());

co_await coro().resumeOnMainQueue(dispatch_time(DISPATCH_TIME_NOW, nanoseconds(200ms).count()));
CHECK(NSThread.isMainThread);
CHECK(isMainQueue());

co_await delay(0.2, coro().resumeOnMainQueue(dispatch_time(DISPATCH_TIME_NOW, nanoseconds(1ms).count())));
CHECK(NSThread.isMainThread);
CHECK(isMainQueue());
}

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

std::vector<int> res;
for (auto it = co_await generate().resumingOnMainQueue().beginOn(conq); it; co_await it.next()) {
CHECK(NSThread.isMainThread);
CHECK(isMainQueue());
res.push_back(*it);
}
CHECK(res == std::vector{1, 2, 3});
Expand All @@ -543,7 +545,7 @@ static auto checkGenerator() -> DispatchTask<> {
};
std::vector<int> res;
for (auto it = co_await generate().begin(); it; co_await it.next()) {
CHECK(NSThread.isMainThread);
CHECK(isMainQueue());
res.push_back(*it);
}
CHECK(res.empty());
Expand Down Expand Up @@ -699,13 +701,18 @@ static auto checkIO() -> DispatchTask<> {
}

TEST_CASE("CoDispatchTests") {
[]() -> DispatchTask<> {
co_await checkReturnPropagation();
co_await checkDispatchToDifferentQueue();
co_await checkMakeAwaitable();
co_await checkTasks();
co_await checkGenerator();
co_await checkIO();
}();
startAsync();
dispatch_async(dispatch_get_main_queue(), ^ {
[]() -> DispatchTask<> {
co_await checkReturnPropagation();
co_await checkDispatchToDifferentQueue();
co_await checkMakeAwaitable();
co_await checkTasks();
co_await checkGenerator();
co_await checkIO();
endAsync();
}();
});
waitForNoAsync();
}

9 changes: 8 additions & 1 deletion test/CoDispatchTestsCpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#include <filesystem>
#include <vector>

#include "TestGlobal.h"

static auto checkIO() -> DispatchTask<> {

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

co_await checkIO();
endAsync();
}

TEST_CASE("CoDispatchTestsCpp") {
runTests();
startAsync();
dispatch_async(dispatch_get_main_queue(), ^ {
runTests();
});
waitForNoAsync();
}

18 changes: 12 additions & 6 deletions test/CoDispatchTestsNoexcept.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,20 @@

#include "doctest.h"

#include "TestGlobal.h"


TEST_CASE("CoDispatchTestsNoExcept") {

[]() -> DispatchTask<> {
auto i = co_await co_dispatch([]() {
return 7;
});
CHECK(i == 7);
}();
startAsync();
dispatch_async(dispatch_get_main_queue(), ^ {
[]() -> DispatchTask<> {
auto i = co_await co_dispatch([]() {
return 7;
});
CHECK(i == 7);
endAsync();
}();
});
waitForNoAsync();
}
20 changes: 16 additions & 4 deletions test/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,31 @@ LDFLAGS:=--std=c++20 -fblocks -ldispatch -lBlocksRuntime
build:
mkdir $@

build/main-linux.o: main-linux.cpp ../include/objc-helpers/BlockUtil.h build
build/main-linux.o: main-linux.cpp ../include/objc-helpers/BlockUtil.h doctest.h build
$(CLANG) $(CPPFLAGS) -c -o $@ $<

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

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

build/CoDispatchTestsNoexcept.o: CoDispatchTestsNoexcept.cpp ../include/objc-helpers/CoDispatch.h build
build/CoDispatchTestsCpp.o: CoDispatchTestsCpp.cpp \
../include/objc-helpers/CoDispatch.h \
TestGlobal.h \
doctest.h \
build
$(CLANG) $(CPPFLAGS) -c -o $@ $<

build/CoDispatchTestsNoexcept.o: CoDispatchTestsNoexcept.cpp \
../include/objc-helpers/CoDispatch.h \
TestGlobal.h \
doctest.h \
build
$(CLANG) $(CPPFLAGS) -fno-exceptions -c -o $@ $<

build/test: build/main-linux.o \
build/TestGlobal.o \
build/BlockUtilTestCpp.o \
build/CoDispatchTestsCpp.o \
build/CoDispatchTestsNoexcept.o
Expand Down
35 changes: 35 additions & 0 deletions test/TestGlobal.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include "TestGlobal.h"

#include <mutex>
#include <condition_variable>

#include <dispatch/dispatch.h>


static int g_AsyncCount = 0;
static std::mutex g_AsyncCountMutex;
static std::condition_variable g_AsyncCountCond;
dispatch_queue_t g_TestQueue;
int g_IsMainKey;

void startAsync() {
std::lock_guard lk(g_AsyncCountMutex);
++g_AsyncCount;
}

void endAsync() {
std::lock_guard lk(g_AsyncCountMutex);
if (--g_AsyncCount == 0)
g_AsyncCountCond.notify_one();

}

void waitForNoAsync() {
std::unique_lock lk(g_AsyncCountMutex);
g_AsyncCountCond.wait(lk, []{ return g_AsyncCount == 0; });
}

bool isMainQueue() {
return (intptr_t)dispatch_get_specific(&g_IsMainKey) == 1;
}

9 changes: 9 additions & 0 deletions test/TestGlobal.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#ifndef HEADER_TEST_GLOBAL_H_INCLUDED
#define HEADER_TEST_GLOBAL_H_INCLUDED

void startAsync();
void endAsync();
void waitForNoAsync();
bool isMainQueue();

#endif
7 changes: 6 additions & 1 deletion test/main-linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@

#include <dispatch/dispatch.h>

extern dispatch_queue_t g_TestQueue;
extern int g_IsMainKey;

int main(int argc, const char * argv[]) {
dispatch_async(dispatch_get_main_queue(), ^ {
g_TestQueue = dispatch_queue_create("tests", DISPATCH_QUEUE_SERIAL);
dispatch_async(g_TestQueue, ^ {
auto ret = doctest::Context(argc, argv).run();
exit(ret);
});
dispatch_queue_set_specific(dispatch_get_main_queue(), &g_IsMainKey, (void*)1, nullptr);
dispatch_main();
}
7 changes: 6 additions & 1 deletion test/main.mm
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,19 @@

#include <dispatch/dispatch.h>

extern dispatch_queue_t g_TestQueue;
extern int g_IsMainKey;

int main(int argc, const char * argv[]) {
@autoreleasepool {
dispatch_async(dispatch_get_main_queue(), ^ {
g_TestQueue = dispatch_queue_create("tests", DISPATCH_QUEUE_SERIAL);
dispatch_async(g_TestQueue, ^ {
@autoreleasepool {
auto ret = doctest::Context(argc, argv).run();
exit(ret);
}
});
dispatch_queue_set_specific(dispatch_get_main_queue(), &g_IsMainKey, (void*)1, nullptr);
dispatch_main();
}
}
6 changes: 6 additions & 0 deletions test/test.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
441779372B24C4930036AF9F /* NSNumberUtilTests.mm in Sources */ = {isa = PBXBuildFile; fileRef = 441779362B24C4930036AF9F /* NSNumberUtilTests.mm */; };
441779392B24C6B00036AF9F /* NSObjectUtilTests.mm in Sources */ = {isa = PBXBuildFile; fileRef = 441779382B24C6B00036AF9F /* NSObjectUtilTests.mm */; };
4417793B2B26FEA70036AF9F /* CoDispatchTestsNoexcept.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4417793A2B26FEA60036AF9F /* CoDispatchTestsNoexcept.cpp */; settings = {COMPILER_FLAGS = "-fno-exceptions"; }; };
4481ACCA2C65B3B6009521DB /* TestGlobal.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4481ACC92C65B3B1009521DB /* TestGlobal.cpp */; };
448D57292B4E88A200A135E9 /* BlockUtilTestCpp.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 448D57282B4E88A200A135E9 /* BlockUtilTestCpp.cpp */; };
448D572B2B50D28500A135E9 /* BlockUtilTest.mm in Sources */ = {isa = PBXBuildFile; fileRef = 448D572A2B50D28500A135E9 /* BlockUtilTest.mm */; };
448D572E2B583C8400A135E9 /* NSStringUtilTestsCpp.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 448D572D2B583C8300A135E9 /* NSStringUtilTestsCpp.cpp */; };
Expand Down Expand Up @@ -74,6 +75,8 @@
441779362B24C4930036AF9F /* NSNumberUtilTests.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = NSNumberUtilTests.mm; sourceTree = "<group>"; };
441779382B24C6B00036AF9F /* NSObjectUtilTests.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = NSObjectUtilTests.mm; sourceTree = "<group>"; };
4417793A2B26FEA60036AF9F /* CoDispatchTestsNoexcept.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = CoDispatchTestsNoexcept.cpp; sourceTree = "<group>"; };
4481ACC82C65B35F009521DB /* TestGlobal.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = TestGlobal.h; sourceTree = "<group>"; };
4481ACC92C65B3B1009521DB /* TestGlobal.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = TestGlobal.cpp; sourceTree = "<group>"; };
448D57282B4E88A200A135E9 /* BlockUtilTestCpp.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = BlockUtilTestCpp.cpp; sourceTree = "<group>"; };
448D572A2B50D28500A135E9 /* BlockUtilTest.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = BlockUtilTest.mm; sourceTree = "<group>"; };
448D572D2B583C8300A135E9 /* NSStringUtilTestsCpp.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = NSStringUtilTestsCpp.cpp; sourceTree = "<group>"; };
Expand Down Expand Up @@ -102,6 +105,8 @@
441779212B202F3E0036AF9F /* Library */,
4417791C2B2013E00036AF9F /* doctest.h */,
441779152B20136E0036AF9F /* main.mm */,
4481ACC82C65B35F009521DB /* TestGlobal.h */,
4481ACC92C65B3B1009521DB /* TestGlobal.cpp */,
448D57282B4E88A200A135E9 /* BlockUtilTestCpp.cpp */,
448D572A2B50D28500A135E9 /* BlockUtilTest.mm */,
44B947E62B477A2700B68C7E /* BoxUtilTests.mm */,
Expand Down Expand Up @@ -231,6 +236,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
4481ACCA2C65B3B6009521DB /* TestGlobal.cpp in Sources */,
441779202B202DA30036AF9F /* CoDispatchTests.mm in Sources */,
441779162B20136E0036AF9F /* main.mm in Sources */,
441779372B24C4930036AF9F /* NSNumberUtilTests.mm in Sources */,
Expand Down

0 comments on commit 84d996f

Please sign in to comment.