summaryrefslogtreecommitdiffstats
path: root/staging_vespalib/src/tests/singleexecutor/singleexecutor_test.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'staging_vespalib/src/tests/singleexecutor/singleexecutor_test.cpp')
-rw-r--r--staging_vespalib/src/tests/singleexecutor/singleexecutor_test.cpp80
1 files changed, 80 insertions, 0 deletions
diff --git a/staging_vespalib/src/tests/singleexecutor/singleexecutor_test.cpp b/staging_vespalib/src/tests/singleexecutor/singleexecutor_test.cpp
new file mode 100644
index 00000000000..5dacaa5d204
--- /dev/null
+++ b/staging_vespalib/src/tests/singleexecutor/singleexecutor_test.cpp
@@ -0,0 +1,80 @@
+// Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+
+#include <vespa/vespalib/testkit/testapp.h>
+
+#include <vespa/vespalib/util/singleexecutor.h>
+#include <vespa/vespalib/util/lambdatask.h>
+#include <atomic>
+
+using namespace vespalib;
+
+TEST("test that all tasks are executed") {
+
+ std::atomic<uint64_t> counter(0);
+ SingleExecutor executor(10);
+
+ for (uint64_t i(0); i < 10; i++) {
+ executor.execute(makeLambdaTask([&counter] {counter++;}));
+ }
+ executor.sync();
+ EXPECT_EQUAL(10u, counter);
+
+ counter = 0;
+ for (uint64_t i(0); i < 10000; i++) {
+ executor.execute(makeLambdaTask([&counter] {counter++;}));
+ }
+ executor.sync();
+ EXPECT_EQUAL(10000u, counter);
+}
+
+void verifyResizeTaskLimit(bool up) {
+ Monitor lock;
+ std::atomic<uint64_t> started(0);
+ std::atomic<uint64_t> allowed(0);
+ SingleExecutor executor(10);
+
+ uint32_t targetTaskLimit = up ? 20 : 5;
+ uint32_t roundedTaskLimit = roundUp2inN(targetTaskLimit);
+ EXPECT_NOT_EQUAL(16u, roundedTaskLimit);
+
+ for (uint64_t i(0); i < 10; i++) {
+ executor.execute(makeLambdaTask([&lock, &started, &allowed] {
+ started++;
+ MonitorGuard guard(lock);
+ while (allowed < started) {
+ guard.wait(1ms);
+ }
+ }));
+ }
+ while (started < 1);
+ EXPECT_EQUAL(1u, started);
+ executor.setTaskLimit(targetTaskLimit);
+ EXPECT_EQUAL(16u, executor.getTaskLimit());
+ allowed = 5;
+ while (started < 6);
+ EXPECT_EQUAL(6u, started);
+ EXPECT_EQUAL(16u, executor.getTaskLimit());
+ allowed = 10;
+ while (started < 10);
+ EXPECT_EQUAL(10u, started);
+ EXPECT_EQUAL(16u, executor.getTaskLimit());
+ executor.execute(makeLambdaTask([&lock, &started, &allowed] {
+ started++;
+ MonitorGuard guard(lock);
+ while (allowed < started) {
+ guard.wait(1ms);
+ }
+ }));
+ while (started < 11);
+ EXPECT_EQUAL(11u, started);
+ EXPECT_EQUAL(roundedTaskLimit, executor.getTaskLimit());
+ allowed = 11;
+}
+TEST("test that resizing up and down works") {
+ TEST_DO(verifyResizeTaskLimit(true));
+ TEST_DO(verifyResizeTaskLimit(false));
+
+
+}
+
+TEST_MAIN() { TEST_RUN_ALL(); }