mirror of
https://github.com/privatevoid-net/nix-super.git
synced 2024-11-10 08:16:15 +02:00
tests/coro-gc: create test for boehm stack patch
Regression test for #7679
This commit is contained in:
parent
92611e6e4c
commit
0fd8f542a8
2 changed files with 100 additions and 1 deletions
99
src/libexpr/tests/coro-gc.cc
Normal file
99
src/libexpr/tests/coro-gc.cc
Normal file
|
@ -0,0 +1,99 @@
|
||||||
|
#include <gtest/gtest.h>
|
||||||
|
#if HAVE_BOEHMGC
|
||||||
|
#include <gc/gc.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include "eval.hh"
|
||||||
|
#include "serialise.hh"
|
||||||
|
|
||||||
|
|
||||||
|
#define guard_gc(x) GC_register_finalizer((void*)x, finalizer, x##_collected, nullptr, nullptr)
|
||||||
|
|
||||||
|
|
||||||
|
namespace nix {
|
||||||
|
#if HAVE_BOEHMGC
|
||||||
|
static bool* uncollectable_bool() {
|
||||||
|
bool* res = (bool*)GC_MALLOC_UNCOLLECTABLE(1);
|
||||||
|
*res = false;
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void finalizer(void *obj, void *data) {
|
||||||
|
//printf("finalizer: obj %p data %p\n", obj, data);
|
||||||
|
*((bool*)data) = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Generate 2 objects, discard one, run gc,
|
||||||
|
// see if one got collected and the other didn't
|
||||||
|
static void testFinalizerCalls() {
|
||||||
|
bool* do_collect_collected = uncollectable_bool();
|
||||||
|
bool* dont_collect_collected = uncollectable_bool();
|
||||||
|
{
|
||||||
|
volatile void* do_collect = GC_MALLOC_ATOMIC(128);
|
||||||
|
guard_gc(do_collect);
|
||||||
|
}
|
||||||
|
volatile void* dont_collect = GC_MALLOC_ATOMIC(128);
|
||||||
|
guard_gc(dont_collect);
|
||||||
|
GC_gcollect();
|
||||||
|
GC_invoke_finalizers();
|
||||||
|
|
||||||
|
ASSERT_TRUE(*do_collect_collected);
|
||||||
|
ASSERT_FALSE(*dont_collect_collected);
|
||||||
|
ASSERT_NE(nullptr, dont_collect);
|
||||||
|
}
|
||||||
|
|
||||||
|
// This test tests that boehm handles coroutine stacks correctly
|
||||||
|
TEST(CoroGC, CoroutineStackNotGCd) {
|
||||||
|
initGC();
|
||||||
|
testFinalizerCalls();
|
||||||
|
|
||||||
|
bool* dont_collect_collected = uncollectable_bool();
|
||||||
|
bool* do_collect_collected = uncollectable_bool();
|
||||||
|
|
||||||
|
volatile void* dont_collect = GC_MALLOC_ATOMIC(128);
|
||||||
|
guard_gc(dont_collect);
|
||||||
|
{
|
||||||
|
volatile void* do_collect = GC_MALLOC_ATOMIC(128);
|
||||||
|
guard_gc(do_collect);
|
||||||
|
}
|
||||||
|
|
||||||
|
auto source = sinkToSource([&](Sink& sink) {
|
||||||
|
testFinalizerCalls();
|
||||||
|
|
||||||
|
bool* dont_collect_inner_collected = uncollectable_bool();
|
||||||
|
bool* do_collect_inner_collected = uncollectable_bool();
|
||||||
|
|
||||||
|
volatile void* dont_collect_inner = GC_MALLOC_ATOMIC(128);
|
||||||
|
guard_gc(dont_collect_inner);
|
||||||
|
{
|
||||||
|
volatile void* do_collect_inner = GC_MALLOC_ATOMIC(128);
|
||||||
|
guard_gc(do_collect_inner);
|
||||||
|
}
|
||||||
|
// pass control to main
|
||||||
|
writeString("foo", sink);
|
||||||
|
|
||||||
|
ASSERT_TRUE(*do_collect_inner_collected);
|
||||||
|
ASSERT_FALSE(*dont_collect_inner_collected);
|
||||||
|
ASSERT_NE(nullptr, dont_collect_inner);
|
||||||
|
|
||||||
|
// pass control to main
|
||||||
|
writeString("bar", sink);
|
||||||
|
});
|
||||||
|
|
||||||
|
// pass control to coroutine
|
||||||
|
std::string foo = readString(*source);
|
||||||
|
ASSERT_EQ(foo, "foo");
|
||||||
|
|
||||||
|
GC_gcollect();
|
||||||
|
GC_invoke_finalizers();
|
||||||
|
|
||||||
|
// pass control to coroutine
|
||||||
|
std::string bar = readString(*source);
|
||||||
|
ASSERT_EQ(bar, "bar");
|
||||||
|
|
||||||
|
ASSERT_FALSE(*dont_collect_collected);
|
||||||
|
ASSERT_TRUE(*do_collect_collected);
|
||||||
|
ASSERT_NE(nullptr, dont_collect);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
}
|
|
@ -16,4 +16,4 @@ libexpr-tests_CXXFLAGS += -I src/libexpr -I src/libutil -I src/libstore -I src/l
|
||||||
|
|
||||||
libexpr-tests_LIBS = libstore-tests libutils-tests libexpr libutil libstore libfetchers
|
libexpr-tests_LIBS = libstore-tests libutils-tests libexpr libutil libstore libfetchers
|
||||||
|
|
||||||
libexpr-tests_LDFLAGS := $(GTEST_LIBS) -lgmock
|
libexpr-tests_LDFLAGS := $(GTEST_LIBS) -lgmock -lboost_context
|
||||||
|
|
Loading…
Reference in a new issue