Skip to content

Commit 03a0fdc

Browse files
committed
core,server: Fix tests
Some unit tests do custom memory resource set up and parse JSON, these are fixed so that the thread local resource used for parsing is available before test starts. Signed-off-by: Abhijat Malviya <[email protected]>
1 parent ad5097f commit 03a0fdc

File tree

4 files changed

+20
-6
lines changed

4 files changed

+20
-6
lines changed

src/core/compact_object_test.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ static void InitThreadStructs() {
8888
SmallString::InitThreadLocal(tlh);
8989
thread_local MiMemoryResource mi_resource(tlh);
9090
CompactObj::InitThreadLocal(&mi_resource);
91+
InitTLJsonHeap(&mi_resource);
9192
};
9293

9394
static void CheckEverythingDeallocated() {

src/core/json/jsonpath_test.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,11 @@ bool is_array(FlatJson ref) {
8989

9090
class ScannerTest : public ::testing::Test {
9191
protected:
92+
void SetUp() override {
93+
Test::SetUp();
94+
InitTLJsonHeap(PMR_NS::get_default_resource());
95+
}
96+
9297
ScannerTest() {
9398
driver_.lexer()->set_debug(1);
9499
}

src/core/page_usage_stats_test.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ class PageUsageStatsTest : public ::testing::Test {
4545
}
4646

4747
PageUsageStatsTest() : m_(mi_heap_get_backing()) {
48+
InitTLJsonHeap(&m_);
4849
}
4950

5051
void SetUp() override {

src/server/json_family_memory_test.cc

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
#include "base/logging.h"
77
#include "facade/facade_test.h"
88
#include "server/command_registry.h"
9-
#include "server/json_family.h"
109
#include "server/test_utils.h"
1110

1211
using namespace testing;
@@ -19,25 +18,33 @@ namespace dfly {
1918

2019
class JsonFamilyMemoryTest : public BaseFamilyTest {
2120
public:
22-
static dfly::MiMemoryResource* GetMemoryResource() {
23-
static thread_local mi_heap_t* heap = mi_heap_new();
24-
static thread_local dfly::MiMemoryResource memory_resource{heap};
21+
static MiMemoryResource* GetMemoryResource() {
22+
thread_local mi_heap_t* heap = mi_heap_new();
23+
thread_local MiMemoryResource memory_resource{heap};
2524
return &memory_resource;
2625
}
2726

2827
protected:
28+
void SetUp() override {
29+
BaseFamilyTest::SetUp();
30+
// Make the core running the thread use the same resource as the rest of the test. Although
31+
// BaseFamilyTest initializes the heap on shards serving transactions, the core running the test
32+
// needs this initialized explicitly.
33+
InitTLJsonHeap(GetMemoryResource());
34+
}
35+
2936
auto GetJsonMemoryUsageFromDb(std::string_view key) {
3037
return Run({"MEMORY", "USAGE", key, "WITHOUTKEY"});
3138
}
3239
};
3340

3441
size_t GetMemoryUsage() {
35-
return static_cast<MiMemoryResource*>(JsonFamilyMemoryTest::GetMemoryResource())->used();
42+
return JsonFamilyMemoryTest::GetMemoryResource()->used();
3643
}
3744

3845
size_t GetJsonMemoryUsageFromString(std::string_view json_str) {
3946
size_t start = GetMemoryUsage();
40-
auto json = dfly::JsonFromString(json_str, JsonFamilyMemoryTest::GetMemoryResource());
47+
auto json = ParseJsonUsingShardHeap(json_str);
4148
if (!json) {
4249
return 0;
4350
}

0 commit comments

Comments
 (0)