aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authoreldritch horrors <pennae@lix.systems>2024-09-24 00:21:16 +0200
committereldritch horrors <pennae@lix.systems>2024-09-26 16:32:02 +0000
commitca9256a789b413b71f424405c8a0d7d37ca36696 (patch)
tree10cefd166045faada804d94104651b746c35a036 /tests
parent4b66e1e24f4952a2e96d680c6b37de2c2c1c76d1 (diff)
libutil: add an async semaphore implementation
like a normal semaphore, but with awaitable acquire actions. this is primarily intended as an intermediate concurrency limiting device in the Worker code, but it may find other uses over time. we do not use std::counting_semaphore as a base because the counter of that is not inspectable as will be needed for Worker. we also do not need atomic operations for cross-thread consistency since we don't have multiple threads (thanks to kj event loops being confined to a single thread) Change-Id: Ie2bcb107f3a2c0185138330f7cbba4cec6cbdd95
Diffstat (limited to 'tests')
-rw-r--r--tests/unit/libutil/async-semaphore.cc74
-rw-r--r--tests/unit/meson.build2
2 files changed, 76 insertions, 0 deletions
diff --git a/tests/unit/libutil/async-semaphore.cc b/tests/unit/libutil/async-semaphore.cc
new file mode 100644
index 000000000..12b52885d
--- /dev/null
+++ b/tests/unit/libutil/async-semaphore.cc
@@ -0,0 +1,74 @@
+#include "async-semaphore.hh"
+
+#include <gtest/gtest.h>
+#include <kj/async.h>
+
+namespace nix {
+
+TEST(AsyncSemaphore, counting)
+{
+ kj::EventLoop loop;
+ kj::WaitScope waitScope(loop);
+
+ AsyncSemaphore sem(2);
+
+ ASSERT_EQ(sem.available(), 2);
+ ASSERT_EQ(sem.used(), 0);
+
+ auto a = kj::evalNow([&] { return sem.acquire(); });
+ ASSERT_EQ(sem.available(), 1);
+ ASSERT_EQ(sem.used(), 1);
+ auto b = kj::evalNow([&] { return sem.acquire(); });
+ ASSERT_EQ(sem.available(), 0);
+ ASSERT_EQ(sem.used(), 2);
+
+ auto c = kj::evalNow([&] { return sem.acquire(); });
+ auto d = kj::evalNow([&] { return sem.acquire(); });
+
+ ASSERT_TRUE(a.poll(waitScope));
+ ASSERT_TRUE(b.poll(waitScope));
+ ASSERT_FALSE(c.poll(waitScope));
+ ASSERT_FALSE(d.poll(waitScope));
+
+ a = nullptr;
+ ASSERT_TRUE(c.poll(waitScope));
+ ASSERT_FALSE(d.poll(waitScope));
+
+ {
+ auto lock = b.wait(waitScope);
+ ASSERT_FALSE(d.poll(waitScope));
+ }
+
+ ASSERT_TRUE(d.poll(waitScope));
+
+ ASSERT_EQ(sem.available(), 0);
+ ASSERT_EQ(sem.used(), 2);
+ c = nullptr;
+ ASSERT_EQ(sem.available(), 1);
+ ASSERT_EQ(sem.used(), 1);
+ d = nullptr;
+ ASSERT_EQ(sem.available(), 2);
+ ASSERT_EQ(sem.used(), 0);
+}
+
+TEST(AsyncSemaphore, cancelledWaiter)
+{
+ kj::EventLoop loop;
+ kj::WaitScope waitScope(loop);
+
+ AsyncSemaphore sem(1);
+
+ auto a = kj::evalNow([&] { return sem.acquire(); });
+ auto b = kj::evalNow([&] { return sem.acquire(); });
+ auto c = kj::evalNow([&] { return sem.acquire(); });
+
+ ASSERT_TRUE(a.poll(waitScope));
+ ASSERT_FALSE(b.poll(waitScope));
+
+ b = nullptr;
+ a = nullptr;
+
+ ASSERT_TRUE(c.poll(waitScope));
+}
+
+}
diff --git a/tests/unit/meson.build b/tests/unit/meson.build
index 8ff0b5ec5..5baf10de2 100644
--- a/tests/unit/meson.build
+++ b/tests/unit/meson.build
@@ -39,6 +39,7 @@ liblixutil_test_support = declare_dependency(
)
libutil_tests_sources = files(
+ 'libutil/async-semaphore.cc',
'libutil/canon-path.cc',
'libutil/checked-arithmetic.cc',
'libutil/chunked-vector.cc',
@@ -76,6 +77,7 @@ libutil_tester = executable(
liblixexpr_mstatic,
liblixutil_test_support,
nlohmann_json,
+ kj,
],
cpp_pch : cpp_pch,
)