Skip to content

Commit 4509757

Browse files
committed
Added tests for memory manager
1 parent 89bfec8 commit 4509757

File tree

2 files changed

+111
-0
lines changed

2 files changed

+111
-0
lines changed

tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ set(IReSearch_tests_sources
121121
./utils/fst_utils_test.cpp
122122
./utils/fst_string_weight_test.cpp
123123
./utils/ngram_match_utils_tests.cpp
124+
./memory/IResearchMemoryManager_tests.cpp
124125
./tests_param.cpp
125126
./tests_main.cpp
126127
)
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
////////////////////////////////////////////////////////////////////////////////
2+
/// DISCLAIMER
3+
///
4+
/// Copyright 2014-2025 ArangoDB GmbH, Cologne, Germany
5+
/// Copyright 2004-2014 triAGENS GmbH, Cologne, Germany
6+
///
7+
/// Licensed under the Apache License, Version 2.0 (the "License");
8+
/// you may not use this file except in compliance with the License.
9+
/// You may obtain a copy of the License at
10+
///
11+
/// http://www.apache.org/licenses/LICENSE-2.0
12+
///
13+
/// Unless required by applicable law or agreed to in writing, software
14+
/// distributed under the License is distributed on an "AS IS" BASIS,
15+
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
/// See the License for the specific language governing permissions and
17+
/// limitations under the License.
18+
///
19+
/// Copyright holder is ArangoDB GmbH, Cologne, Germany
20+
///
21+
/// @author Koushal Kawade
22+
////////////////////////////////////////////////////////////////////////////////
23+
24+
#include <iostream>
25+
#include <memory>
26+
#include "tests_shared.hpp"
27+
#include "utils/managed_allocator.hpp"
28+
#include "resource_manager.hpp"
29+
30+
using namespace irs;
31+
32+
// Wrapper class around IResearchMemoryManager.
33+
// We use it to track memory increase/decrease.
34+
// To be used mostly for debugging purposes.
35+
struct IResearchMemoryManagerAccessor : public irs::IResearchMemoryManager {
36+
37+
IResearchMemoryManagerAccessor() = default;
38+
virtual ~IResearchMemoryManagerAccessor() = default;
39+
40+
virtual void Increase([[maybe_unused]] uint64_t value) override {
41+
42+
// std::cout << "Increase: " << value << std::endl;
43+
IResearchMemoryManager::Increase(value);
44+
}
45+
46+
virtual void Decrease([[maybe_unused]] uint64_t value) noexcept override {
47+
// std::cout << "Decrease: " << value << std::endl;
48+
IResearchMemoryManager::Decrease(value);
49+
}
50+
51+
static std::shared_ptr<IResearchMemoryManagerAccessor> GetInstance() {
52+
if (!_accessor)
53+
_accessor.reset(new IResearchMemoryManagerAccessor());
54+
55+
return _accessor;
56+
}
57+
58+
static void setMemoryLimit(uint64_t limit) {
59+
irs::IResearchMemoryManager::_memory_limit.store(limit);
60+
}
61+
62+
private:
63+
static inline std::shared_ptr<IResearchMemoryManagerAccessor> _accessor;
64+
};
65+
66+
// An allocator using IResearchMemoryManagerAccessor
67+
// so that we can track the allocations/deallocations
68+
// during the test.
69+
template<typename T>
70+
struct ManagedTypedAllocatorTest
71+
: ManagedAllocator<std::allocator<T>, IResearchMemoryManagerAccessor> {
72+
using Base = ManagedAllocator<std::allocator<T>, IResearchMemoryManagerAccessor>;
73+
explicit ManagedTypedAllocatorTest()
74+
: Base(
75+
*IResearchMemoryManagerAccessor::GetInstance()
76+
) {
77+
}
78+
using Base::Base;
79+
};
80+
81+
TEST(IResearch_memory_limit_test, managed_allocator_smoke_test) {
82+
83+
IResearchMemoryManagerAccessor::setMemoryLimit(3);
84+
auto memoryMgr = IResearchMemoryManagerAccessor::GetInstance();
85+
86+
using type = char;
87+
std::vector<type, ManagedTypedAllocatorTest<type>> arr(*memoryMgr.get());
88+
89+
arr.push_back('a');
90+
arr.push_back('b');
91+
ASSERT_THROW(arr.push_back('c'), std::bad_alloc);
92+
}
93+
94+
TEST(IResearch_memory_limit_test, memory_manager_smoke_test) {
95+
96+
// set limit
97+
IResearchMemoryManagerAccessor::setMemoryLimit(47);
98+
99+
// allocate vector
100+
ManagedVector<uint64_t> vec;
101+
vec.push_back(10); // Allocate 8 bytes
102+
vec.push_back(11); // Allocate 16, Free previous 8, Copy both elements in the new array.
103+
104+
ASSERT_THROW(vec.push_back(12), std::bad_alloc); // Allocate 32 while holding previous 16, total 48 (bad_alloc)
105+
106+
// Increase memory limit to accommodate the 3rd element.
107+
IResearchMemoryManagerAccessor::setMemoryLimit(48);
108+
109+
ASSERT_NO_THROW(vec.push_back(12));
110+
}

0 commit comments

Comments
 (0)