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
2021extern " C" {
2122#include " redis/zmalloc.h"
2223}
2324
25+ ABSL_DECLARE_FLAG (bool , experimental_flat_json);
26+
2427using namespace dfly ;
2528
2629class 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