Skip to content

Commit e717440

Browse files
committed
core: Add tests for json defrag
Signed-off-by: Abhijat Malviya <[email protected]>
1 parent 16f261d commit e717440

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

src/core/compact_object.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -723,7 +723,7 @@ void RobjWrapper::MakeInnerRoom(size_t current_cap, size_t desired, MemoryResour
723723
} // namespace detail
724724

725725
uint32_t JsonEnconding() {
726-
static thread_local uint32_t json_enc =
726+
thread_local uint32_t json_enc =
727727
absl::GetFlag(FLAGS_experimental_flat_json) ? kEncodingJsonFlat : kEncodingJsonCons;
728728
return json_enc;
729729
}

src/core/page_usage_stats_test.cc

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,26 @@
44

55
#include "core/page_usage_stats.h"
66

7+
#include <absl/flags/reflection.h>
78
#include <gmock/gmock-matchers.h>
89

910
#include "base/gtest.h"
1011
#include "base/logging.h"
1112
#include "core/compact_object.h"
12-
#include "core/mi_memory_resource.h"
1313
#include "core/qlist.h"
1414
#include "core/score_map.h"
1515
#include "core/small_string.h"
1616
#include "core/sorted_map.h"
1717
#include "core/string_map.h"
1818
#include "core/string_set.h"
19+
#include "redis/redis_aux.h"
1920

2021
extern "C" {
2122
#include "redis/zmalloc.h"
2223
}
2324

25+
ABSL_DECLARE_FLAG(bool, experimental_flat_json);
26+
2427
using namespace dfly;
2528

2629
class PageUsageStatsTest : public ::testing::Test {
@@ -45,6 +48,8 @@ class PageUsageStatsTest : public ::testing::Test {
4548
}
4649

4750
void SetUp() override {
51+
CompactObj::InitThreadLocal(&m_);
52+
4853
score_map_ = std::make_unique<ScoreMap>(&m_);
4954
sorted_map_ = std::make_unique<detail::SortedMap>(&m_);
5055
string_set_ = std::make_unique<StringSet>(&m_);
@@ -176,3 +181,34 @@ TEST_F(PageUsageStatsTest, StatCollection) {
176181
const CollectedPageStats::ShardUsageSummary expected{{50, 65}, {90, 85}, {99, 89}};
177182
EXPECT_EQ(usage.at(1), expected);
178183
}
184+
185+
TEST_F(PageUsageStatsTest, JSONCons) {
186+
// Because of the static encoding it is not possible to easily test the flat encoding. Once the
187+
// encoding flag is set, it is not re-read. If friend class is used to access the compact object
188+
// inner fields and call `DefragIfNeeded` directly on the flat variant of the union, the test will
189+
// still fail. This is because freeing the compact object code path takes the wrong branch based
190+
// on encoding. The flat encoding was tested manually adjusting this same test with changed
191+
// encoding.
192+
std::string_view data{R"#({"data": "some", "count": 1, "checked": false})#"};
193+
194+
auto parsed = JsonFromString(data, &m_);
195+
EXPECT_TRUE(parsed.has_value());
196+
197+
c_obj_.SetJson(std::move(parsed.value()));
198+
199+
PageUsage p{CollectPageStats::YES, 0.1};
200+
p.SetForceReallocate(true);
201+
202+
c_obj_.DefragIfNeeded(&p);
203+
204+
const auto stats = p.CollectedStats();
205+
EXPECT_GT(stats.pages_scanned, 0);
206+
EXPECT_EQ(stats.objects_skipped_not_required, 0);
207+
208+
EXPECT_EQ(c_obj_.ObjType(), OBJ_JSON);
209+
210+
auto json_obj = c_obj_.GetJson();
211+
EXPECT_EQ(json_obj->at("data").as_string_view(), "some");
212+
EXPECT_EQ(json_obj->at("count").as_integer<uint8_t>(), 1);
213+
EXPECT_EQ(json_obj->at("checked").as_bool(), false);
214+
}

0 commit comments

Comments
 (0)