memgraph/tests/unit/utils_memory_tracker.cpp
Gareth Andrew Lloyd 33c400fcc1
Fixup memory e2e tests (#1715)
- Remove the e2e that did concurrent mgp_* calls on the same transaction
  (ATM this is unsupported)
- Fix up the concurrent mgp_global_alloc test to be testing it more precisely
- Reduce the memory limit on detach delete test due to recent memory
  optimizations around deltas.
- No longer throw from hook, through jemalloc C, to our C++ on other
  side. This cause mutex unlocks to not happen.
- No longer allocate error messages while inside the hook. This caused
  recursive entry back inside jamalloc which would try to relock a
  non-recursive mutex.
2024-02-16 15:35:08 +00:00

71 lines
2.1 KiB
C++

// Copyright 2024 Memgraph Ltd.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt; by using this file, you agree to be bound by the terms of the Business Source
// License, and you may not use this file except in compliance with the Business Source License.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
#include <thread>
#include <gtest/gtest.h>
#include <utils/memory_tracker.hpp>
#include <utils/on_scope_exit.hpp>
TEST(MemoryTrackerTest, ExceptionEnabler) {
memgraph::utils::MemoryTracker memory_tracker;
static constexpr size_t hard_limit = 10;
memory_tracker.SetHardLimit(hard_limit);
std::atomic<bool> can_continue{false};
std::atomic<bool> enabler_created{false};
std::thread t1{[&] {
// wait until the second thread creates exception enabler
while (!enabler_created)
;
// we use the OnScopeExit so the test doesn't deadlock when
// an ASSERT fails
memgraph::utils::OnScopeExit thread_notifier{[&] {
// tell the second thread it can finish its test
can_continue = true;
}};
ASSERT_TRUE(memory_tracker.Alloc(hard_limit + 1));
}};
std::thread t2{[&] {
memgraph::utils::MemoryTracker::OutOfMemoryExceptionEnabler exception_enabler;
enabler_created = true;
ASSERT_FALSE(memory_tracker.Alloc(hard_limit + 1));
// hold the enabler until the first thread finishes
while (!can_continue)
;
}};
t1.join();
t2.join();
}
TEST(MemoryTrackerTest, ExceptionBlocker) {
memgraph::utils::MemoryTracker memory_tracker;
static constexpr size_t hard_limit = 10;
memory_tracker.SetHardLimit(hard_limit);
memgraph::utils::MemoryTracker::OutOfMemoryExceptionEnabler exception_enabler;
{
memgraph::utils::MemoryTracker::OutOfMemoryExceptionBlocker exception_blocker;
ASSERT_TRUE(memory_tracker.Alloc(hard_limit + 1));
ASSERT_EQ(memory_tracker.Amount(), hard_limit + 1);
}
ASSERT_FALSE(memory_tracker.Alloc(hard_limit + 1));
}