summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authormyaaaaaaaaa <103326468+myaaaaaaaaa@users.noreply.github.com>2023-02-15 12:34:26 -0500
committermyaaaaaaaaa <103326468+myaaaaaaaaa@users.noreply.github.com>2023-05-17 17:33:08 -0400
commitc9968634640c77faadeebe961c8a25e3847b86ce (patch)
tree552416488f27ed1fd95d9666e05debff6dc20bd1 /tests
parent964a5353dbf5fc0dca4c4b7b9cfa5631dd5ab568 (diff)
downloadredot-engine-c9968634640c77faadeebe961c8a25e3847b86ce.tar.gz
Streamline WorkerThreadPool tests and make them more robust
Diffstat (limited to 'tests')
-rw-r--r--tests/core/threads/test_worker_thread_pool.h157
-rw-r--r--tests/test_main.cpp12
2 files changed, 64 insertions, 105 deletions
diff --git a/tests/core/threads/test_worker_thread_pool.h b/tests/core/threads/test_worker_thread_pool.h
index 3d5372658e..e9a762b57b 100644
--- a/tests/core/threads/test_worker_thread_pool.h
+++ b/tests/core/threads/test_worker_thread_pool.h
@@ -37,120 +37,73 @@
namespace TestWorkerThreadPool {
-int u32scmp(const char32_t *l, const char32_t *r) {
- for (; *l == *r && *l && *r; l++, r++) {
- // Continue.
- }
- return *l - *r;
-}
+static LocalVector<SafeNumeric<int>> counter;
static void static_test(void *p_arg) {
- SafeNumeric<uint32_t> *counter = (SafeNumeric<uint32_t> *)p_arg;
- counter->increment();
+ counter[(uint64_t)p_arg].increment();
+ counter[0].add(2);
}
-
-static SafeNumeric<uint32_t> callable_counter;
-
static void static_callable_test() {
- callable_counter.increment();
-}
-
-TEST_CASE("[WorkerThreadPool] Process 256 threads using native task") {
- const int count = 256;
- SafeNumeric<uint32_t> counter;
- WorkerThreadPool::TaskID tasks[count];
- for (int i = 0; i < count; i++) {
- tasks[i] = WorkerThreadPool::get_singleton()->add_native_task(static_test, &counter, true);
- }
- for (int i = 0; i < count; i++) {
- WorkerThreadPool::get_singleton()->wait_for_task_completion(tasks[i]);
- }
-
- CHECK(counter.get() == count);
-}
-
-TEST_CASE("[WorkerThreadPool] Process 256 threads using native low priority") {
- const int count = 256;
- SafeNumeric<uint32_t> counter = SafeNumeric<uint32_t>(0);
- WorkerThreadPool::TaskID tasks[count];
- for (int i = 0; i < count; i++) {
- tasks[i] = WorkerThreadPool::get_singleton()->add_native_task(static_test, &counter, false);
- }
- for (int i = 0; i < count; i++) {
- WorkerThreadPool::get_singleton()->wait_for_task_completion(tasks[i]);
- }
-
- CHECK(counter.get() == count);
-}
-
-TEST_CASE("[WorkerThreadPool] Process 256 threads using callable") {
- const int count = 256;
- WorkerThreadPool::TaskID tasks[count];
- callable_counter.set(0);
- for (int i = 0; i < count; i++) {
- tasks[i] = WorkerThreadPool::get_singleton()->add_task(callable_mp_static(static_callable_test), true);
- }
- for (int i = 0; i < count; i++) {
- WorkerThreadPool::get_singleton()->wait_for_task_completion(tasks[i]);
- }
-
- CHECK(callable_counter.get() == count);
+ counter[0].sub(2);
}
-
-TEST_CASE("[WorkerThreadPool] Process 256 threads using callable low priority") {
- const int count = 256;
- WorkerThreadPool::TaskID tasks[count];
- callable_counter.set(0);
- for (int i = 0; i < count; i++) {
- tasks[i] = WorkerThreadPool::get_singleton()->add_task(callable_mp_static(static_callable_test), false);
+TEST_CASE("[WorkerThreadPool] Process threads using individual tasks") {
+ for (int iterations = 0; iterations < 500; iterations++) {
+ const int count = Math::pow(2.0f, Math::random(0.0f, 5.0f));
+ const bool low_priority = Math::rand() % 2;
+
+ LocalVector<WorkerThreadPool::TaskID> tasks1;
+ LocalVector<WorkerThreadPool::TaskID> tasks2;
+ tasks1.resize(count);
+ tasks2.resize(count);
+
+ counter.clear();
+ counter.resize(count);
+ for (int i = 0; i < count; i++) {
+ tasks1[i] = WorkerThreadPool::get_singleton()->add_native_task(static_test, (void *)(uintptr_t)i, low_priority);
+ tasks2[i] = WorkerThreadPool::get_singleton()->add_task(callable_mp_static(static_callable_test), !low_priority);
+ }
+ for (int i = 0; i < count; i++) {
+ WorkerThreadPool::get_singleton()->wait_for_task_completion(tasks1[i]);
+ WorkerThreadPool::get_singleton()->wait_for_task_completion(tasks2[i]);
+ }
+
+ bool all_run_once = true;
+ for (int i = 0; i < count; i++) {
+ //Reduce number of check messages
+ all_run_once &= counter[i].get() == 1;
+ }
+ CHECK(all_run_once);
}
- for (int i = 0; i < count; i++) {
- WorkerThreadPool::get_singleton()->wait_for_task_completion(tasks[i]);
- }
-
- CHECK(callable_counter.get() == count);
}
static void static_group_test(void *p_arg, uint32_t p_index) {
- SafeNumeric<uint32_t> *counter = (SafeNumeric<uint32_t> *)p_arg;
- counter->exchange_if_greater(p_index);
-}
-
-TEST_CASE("[WorkerThreadPool] Process 256 elements on native task group") {
- const int count = 256;
- SafeNumeric<uint32_t> counter;
- WorkerThreadPool::GroupID group = WorkerThreadPool::get_singleton()->add_native_group_task(static_group_test, &counter, count, -1, true);
- WorkerThreadPool::get_singleton()->wait_for_group_task_completion(group);
- CHECK(counter.get() == count - 1);
+ counter[p_index].increment();
+ counter[0].add((uintptr_t)p_arg);
}
-
-TEST_CASE("[WorkerThreadPool] Process 256 elements on native task group low priority") {
- const int count = 256;
- SafeNumeric<uint32_t> counter;
- WorkerThreadPool::GroupID group = WorkerThreadPool::get_singleton()->add_native_group_task(static_group_test, &counter, count, -1, false);
- WorkerThreadPool::get_singleton()->wait_for_group_task_completion(group);
- CHECK(counter.get() == count - 1);
-}
-
-static SafeNumeric<uint32_t> callable_group_counter;
-
static void static_callable_group_test(uint32_t p_index) {
- callable_group_counter.exchange_if_greater(p_index);
+ counter[p_index].increment();
+ counter[0].sub(2);
}
-
-TEST_CASE("[WorkerThreadPool] Process 256 elements on native task group") {
- const int count = 256;
- WorkerThreadPool::GroupID group = WorkerThreadPool::get_singleton()->add_group_task(callable_mp_static(static_callable_group_test), count, -1, true);
- WorkerThreadPool::get_singleton()->wait_for_group_task_completion(group);
- CHECK(callable_group_counter.get() == count - 1);
-}
-
-TEST_CASE("[WorkerThreadPool] Process 256 elements on native task group low priority") {
- const int count = 256;
- callable_group_counter.set(0);
- WorkerThreadPool::GroupID group = WorkerThreadPool::get_singleton()->add_group_task(callable_mp_static(static_callable_group_test), count, -1, false);
- WorkerThreadPool::get_singleton()->wait_for_group_task_completion(group);
- CHECK(callable_group_counter.get() == count - 1);
+TEST_CASE("[WorkerThreadPool] Process elements using group tasks") {
+ for (int iterations = 0; iterations < 500; iterations++) {
+ const int count = Math::pow(2.0f, Math::random(0.0f, 5.0f));
+ const int tasks = Math::pow(2.0f, Math::random(0.0f, 5.0f));
+ const bool low_priority = Math::rand() % 2;
+
+ counter.clear();
+ counter.resize(count);
+ WorkerThreadPool::GroupID group1 = WorkerThreadPool::get_singleton()->add_native_group_task(static_group_test, (void *)2, count, tasks, !low_priority);
+ WorkerThreadPool::GroupID group2 = WorkerThreadPool::get_singleton()->add_group_task(callable_mp_static(static_callable_group_test), count, tasks, low_priority);
+ WorkerThreadPool::get_singleton()->wait_for_group_task_completion(group1);
+ WorkerThreadPool::get_singleton()->wait_for_group_task_completion(group2);
+
+ bool all_run_once = true;
+ for (int i = 0; i < count; i++) {
+ //Reduce number of check messages
+ all_run_once &= counter[i].get() == 2;
+ }
+ CHECK(all_run_once);
+ }
}
} // namespace TestWorkerThreadPool
diff --git a/tests/test_main.cpp b/tests/test_main.cpp
index 6b6257e25d..05d7df038c 100644
--- a/tests/test_main.cpp
+++ b/tests/test_main.cpp
@@ -202,7 +202,7 @@ struct GodotTestCaseListener : public doctest::IReporter {
ThemeDB *theme_db = nullptr;
void test_case_start(const doctest::TestCaseData &p_in) override {
- SignalWatcher::get_singleton()->_clear_signals();
+ reinitialize();
String name = String(p_in.m_name);
String suite_name = String(p_in.m_test_suite);
@@ -343,11 +343,11 @@ struct GodotTestCaseListener : public doctest::IReporter {
}
void test_case_reenter(const doctest::TestCaseData &) override {
- SignalWatcher::get_singleton()->_clear_signals();
+ reinitialize();
}
void subcase_start(const doctest::SubcaseSignature &) override {
- SignalWatcher::get_singleton()->_clear_signals();
+ reinitialize();
}
void report_query(const doctest::QueryData &) override {}
@@ -357,6 +357,12 @@ struct GodotTestCaseListener : public doctest::IReporter {
void log_assert(const doctest::AssertData &in) override {}
void log_message(const doctest::MessageData &) override {}
void test_case_skipped(const doctest::TestCaseData &) override {}
+
+private:
+ void reinitialize() {
+ Math::seed(0x60d07);
+ SignalWatcher::get_singleton()->_clear_signals();
+ }
};
REGISTER_LISTENER("GodotTestCaseListener", 1, GodotTestCaseListener);