Skip to content

Commit

Permalink
Tests cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
gershnik committed Aug 11, 2024
1 parent e847090 commit d03bf31
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 62 deletions.
24 changes: 12 additions & 12 deletions test/CoDispatchTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -700,19 +700,19 @@ static auto checkIO() -> DispatchTask<> {
co_await resumeOnMainQueue();
}

static DispatchTask<> runTests() {
co_await checkReturnPropagation();
co_await checkDispatchToDifferentQueue();
co_await checkMakeAwaitable();
co_await checkTasks();
co_await checkGenerator();
co_await checkIO();
finishAsyncTest();
}

TEST_CASE("CoDispatchTests") {
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();
}();
waitForAsyncTest(^ {
runTests();
});
waitForNoAsync();
}

6 changes: 2 additions & 4 deletions test/CoDispatchTestsCpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,14 +151,12 @@ static DispatchTask<> runTests() {
}

co_await checkIO();
endAsync();
finishAsyncTest();
}

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

21 changes: 10 additions & 11 deletions test/CoDispatchTestsNoexcept.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,17 @@

#include "TestGlobal.h"

static DispatchTask<> runTests() {
auto i = co_await co_dispatch([]() {
return 7;
});
CHECK(i == 7);
finishAsyncTest();
}


TEST_CASE("CoDispatchTestsNoExcept") {

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

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

build/TestGlobal.o: TestGlobal.cpp TestGlobal.h build
$(CLANG) $(CPPFLAGS) -c -o $@ $<
Expand All @@ -32,7 +32,7 @@ build/CoDispatchTestsNoexcept.o: CoDispatchTestsNoexcept.cpp \
build
$(CLANG) $(CPPFLAGS) -fno-exceptions -c -o $@ $<

build/test: build/main-linux.o \
build/test: build/main.o \
build/TestGlobal.o \
build/BlockUtilTestCpp.o \
build/CoDispatchTestsCpp.o \
Expand Down
24 changes: 19 additions & 5 deletions test/TestGlobal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,41 @@
static int g_AsyncCount = 0;
static std::mutex g_AsyncCountMutex;
static std::condition_variable g_AsyncCountCond;
dispatch_queue_t g_TestQueue;
int g_IsMainKey;
static int g_IsMainKey;

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

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

}

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

void waitForAsyncTest(void (^block)()) {
startAsync();
dispatch_async(dispatch_get_main_queue(), block);
waitForNoAsync();
}

void finishAsyncTest() {
endAsync();
}

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

void runMainQueue() {
dispatch_queue_set_specific(dispatch_get_main_queue(), &g_IsMainKey, (void*)intptr_t(1), nullptr);
dispatch_main();
}

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

void startAsync();
void endAsync();
void waitForNoAsync();
void waitForAsyncTest(void (^block)());
void finishAsyncTest();

bool isMainQueue();
void runMainQueue();

#endif
18 changes: 0 additions & 18 deletions test/main-linux.cpp

This file was deleted.

21 changes: 17 additions & 4 deletions test/main.mm
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,32 @@

#include <dispatch/dispatch.h>

extern dispatch_queue_t g_TestQueue;
extern int g_IsMainKey;
#include "TestGlobal.h"

static dispatch_queue_t g_TestQueue;

int main(int argc, const char * argv[]) {
//We run tests on a separate serial queue because we can then block it waiting for async operations
//launched from a test to finish. We cannot do this from main queue since some async tests must themselves
//run on main queue. There is no way to portably have "modal loop" with libdispatch. On Mac only
//we could run a runloop but not on Linux.
#ifdef __OBJC__
@autoreleasepool {
#endif
g_TestQueue = dispatch_queue_create("tests", DISPATCH_QUEUE_SERIAL);
dispatch_async(g_TestQueue, ^ {
#ifdef __OBJC__
@autoreleasepool {
#endif
auto ret = doctest::Context(argc, argv).run();
exit(ret);
#ifdef __OBJC__
}
#endif
});
dispatch_queue_set_specific(dispatch_get_main_queue(), &g_IsMainKey, (void*)1, nullptr);
dispatch_main();
runMainQueue();

#ifdef __OBJC__
}
#endif
}
2 changes: 1 addition & 1 deletion test/test.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@
isa = PBXProject;
attributes = {
BuildIndependentTargetsInParallel = 1;
LastUpgradeCheck = 1500;
LastUpgradeCheck = 1600;
TargetAttributes = {
441779112B20136E0036AF9F = {
CreatedOnToolsVersion = 15.0;
Expand Down
2 changes: 1 addition & 1 deletion test/test.xcodeproj/xcshareddata/xcschemes/test.xcscheme
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1500"
LastUpgradeVersion = "1600"
version = "1.7">
<BuildAction
parallelizeBuildables = "YES"
Expand Down

0 comments on commit d03bf31

Please sign in to comment.