2024-02-16 23:35:08 +08:00
|
|
|
// Copyright 2024 Memgraph Ltd.
|
2021-10-26 14:53:56 +08:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2021-02-23 03:51:46 +08:00
|
|
|
#include <thread>
|
|
|
|
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
|
|
|
#include <utils/memory_tracker.hpp>
|
2021-03-04 19:20:11 +08:00
|
|
|
#include <utils/on_scope_exit.hpp>
|
2021-02-23 03:51:46 +08:00
|
|
|
|
|
|
|
TEST(MemoryTrackerTest, ExceptionEnabler) {
|
2022-02-22 20:33:45 +08:00
|
|
|
memgraph::utils::MemoryTracker memory_tracker;
|
2021-02-23 03:51:46 +08:00
|
|
|
|
2022-03-31 19:52:43 +08:00
|
|
|
static constexpr size_t hard_limit = 10;
|
2021-02-23 03:51:46 +08:00
|
|
|
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)
|
|
|
|
;
|
|
|
|
|
2021-03-04 19:20:11 +08:00
|
|
|
// we use the OnScopeExit so the test doesn't deadlock when
|
|
|
|
// an ASSERT fails
|
2022-02-22 20:33:45 +08:00
|
|
|
memgraph::utils::OnScopeExit thread_notifier{[&] {
|
2021-03-04 19:20:11 +08:00
|
|
|
// tell the second thread it can finish its test
|
|
|
|
can_continue = true;
|
|
|
|
}};
|
|
|
|
|
2024-02-16 23:35:08 +08:00
|
|
|
ASSERT_TRUE(memory_tracker.Alloc(hard_limit + 1));
|
2021-02-23 03:51:46 +08:00
|
|
|
}};
|
|
|
|
|
|
|
|
std::thread t2{[&] {
|
2022-02-22 20:33:45 +08:00
|
|
|
memgraph::utils::MemoryTracker::OutOfMemoryExceptionEnabler exception_enabler;
|
2021-02-23 03:51:46 +08:00
|
|
|
enabler_created = true;
|
2024-02-16 23:35:08 +08:00
|
|
|
ASSERT_FALSE(memory_tracker.Alloc(hard_limit + 1));
|
2021-02-23 03:51:46 +08:00
|
|
|
|
|
|
|
// hold the enabler until the first thread finishes
|
|
|
|
while (!can_continue)
|
|
|
|
;
|
|
|
|
}};
|
|
|
|
|
|
|
|
t1.join();
|
|
|
|
t2.join();
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(MemoryTrackerTest, ExceptionBlocker) {
|
2022-02-22 20:33:45 +08:00
|
|
|
memgraph::utils::MemoryTracker memory_tracker;
|
2021-02-23 03:51:46 +08:00
|
|
|
|
2022-03-31 19:52:43 +08:00
|
|
|
static constexpr size_t hard_limit = 10;
|
2021-02-23 03:51:46 +08:00
|
|
|
memory_tracker.SetHardLimit(hard_limit);
|
|
|
|
|
2022-02-22 20:33:45 +08:00
|
|
|
memgraph::utils::MemoryTracker::OutOfMemoryExceptionEnabler exception_enabler;
|
2021-02-23 03:51:46 +08:00
|
|
|
{
|
2022-02-22 20:33:45 +08:00
|
|
|
memgraph::utils::MemoryTracker::OutOfMemoryExceptionBlocker exception_blocker;
|
2021-02-23 03:51:46 +08:00
|
|
|
|
2024-02-16 23:35:08 +08:00
|
|
|
ASSERT_TRUE(memory_tracker.Alloc(hard_limit + 1));
|
2021-02-23 03:51:46 +08:00
|
|
|
ASSERT_EQ(memory_tracker.Amount(), hard_limit + 1);
|
|
|
|
}
|
2024-02-16 23:35:08 +08:00
|
|
|
ASSERT_FALSE(memory_tracker.Alloc(hard_limit + 1));
|
2021-02-23 03:51:46 +08:00
|
|
|
}
|