aboutsummaryrefslogtreecommitdiff
path: root/tests/unit
diff options
context:
space:
mode:
authoreldritch horrors <pennae@lix.systems>2024-03-21 21:27:07 +0100
committereldritch horrors <pennae@lix.systems>2024-03-30 23:40:45 +0000
commit620de98d0ce8d6a9207a6a54c7fc66cfa55f7797 (patch)
treefbfef069e358d20cbbfe53258fe172bab4bed9d5 /tests/unit
parent4b730f328e1ea581d09ffba4b064b5f3949af414 (diff)
libutil: drop Pool resources on exceptional free
if a scope owning a resource does not gracefully drop that resource while handling exceptions from deeper down the call stack we should assume the resource is invalid state and drop it. currently it *is* true that such cases do not cause resources to be freed, but thanks to validator misuses this has so far not caused any larger problem. Change-Id: Ie4f91bcd60a64d05c5ff9d22cc97954816d13b97
Diffstat (limited to 'tests/unit')
-rw-r--r--tests/unit/libutil/pool.cc15
1 files changed, 15 insertions, 0 deletions
diff --git a/tests/unit/libutil/pool.cc b/tests/unit/libutil/pool.cc
index 127e42dda..a3743e601 100644
--- a/tests/unit/libutil/pool.cc
+++ b/tests/unit/libutil/pool.cc
@@ -124,4 +124,19 @@ namespace nix {
ASSERT_NE(h->num, counter);
}
}
+
+ TEST(Pool, throwingOperationDropsResource)
+ {
+ auto createResource = []() { return make_ref<TestResource>(); };
+
+ Pool<TestResource> pool = Pool<TestResource>((size_t)1, createResource);
+
+ ASSERT_THROW({
+ auto _r = pool.get();
+ ASSERT_EQ(pool.count(), 1);
+ throw 1;
+ }, int);
+
+ ASSERT_EQ(pool.count(), 0);
+ }
}