From 741ab5b28f26a62e77af295eb94c1c556e0673ac Mon Sep 17 00:00:00 2001 From: Arkadiusz Szczepkowicz Date: Tue, 13 Jun 2023 17:14:28 +0200 Subject: [PATCH] #2162: Fix vt aborting when lb file dont have metadata --- .../vrt/collection/balance/lb_data_holder.cc | 64 ++++++++++--------- tests/unit/collection/test_lb_data_holder.cc | 27 ++++++++ 2 files changed, 60 insertions(+), 31 deletions(-) diff --git a/src/vt/vrt/collection/balance/lb_data_holder.cc b/src/vt/vrt/collection/balance/lb_data_holder.cc index d591773d71..c2da1c2bf5 100644 --- a/src/vt/vrt/collection/balance/lb_data_holder.cc +++ b/src/vt/vrt/collection/balance/lb_data_holder.cc @@ -374,41 +374,43 @@ LBDataHolder::LBDataHolder(nlohmann::json const& j) } void LBDataHolder::readMetadata(nlohmann::json const& j) { - auto metadata = j["metadata"]; - if (metadata.find("phases") != metadata.end()) { - auto phases = metadata["phases"]; - // load count - vtAssertExpr(phases["count"].is_number()); - count_ = phases["count"]; - // load all skipped phases - auto sl = phases["skipped"]["list"]; - if(sl.is_array()) { - for(PhaseType skipped : sl) { - skipped_phases_.insert(skipped); + if (j.find("metadata") != j.end()) { + auto metadata = j["metadata"]; + if (metadata.find("phases") != metadata.end()) { + auto phases = metadata["phases"]; + // load count + vtAssertExpr(phases["count"].is_number()); + count_ = phases["count"]; + // load all skipped phases + auto sl = phases["skipped"]["list"]; + if(sl.is_array()) { + for(PhaseType skipped : sl) { + skipped_phases_.insert(skipped); + } } - } - auto sr = phases["skipped"]["range"]; - if(sr.is_array()) { - for(auto const& pair : sr) { - vtAssertExpr(pair.is_array()); - for(PhaseType i = pair[0]; i <= pair[1]; i++){ - skipped_phases_.insert(i); + auto sr = phases["skipped"]["range"]; + if(sr.is_array()) { + for(auto const& pair : sr) { + vtAssertExpr(pair.is_array()); + for(PhaseType i = pair[0]; i <= pair[1]; i++){ + skipped_phases_.insert(i); + } } } - } - // load all identical phases - auto il = phases["identical_to_previous"]["list"]; - if(il.is_array()) { - for(PhaseType identical : il) { - identical_phases_.insert(identical); + // load all identical phases + auto il = phases["identical_to_previous"]["list"]; + if(il.is_array()) { + for(PhaseType identical : il) { + identical_phases_.insert(identical); + } } - } - auto ir = phases["identical_to_previous"]["range"]; - if(ir.is_array()) { - for(auto const& pair : ir) { - vtAssertExpr(pair.is_array()); - for(PhaseType i = pair[0]; i <= pair[1]; i++){ - identical_phases_.insert(i); + auto ir = phases["identical_to_previous"]["range"]; + if(ir.is_array()) { + for(auto const& pair : ir) { + vtAssertExpr(pair.is_array()); + for(PhaseType i = pair[0]; i <= pair[1]; i++){ + identical_phases_.insert(i); + } } } } diff --git a/tests/unit/collection/test_lb_data_holder.cc b/tests/unit/collection/test_lb_data_holder.cc index 9733baa84b..9b2bec90c5 100644 --- a/tests/unit/collection/test_lb_data_holder.cc +++ b/tests/unit/collection/test_lb_data_holder.cc @@ -90,6 +90,33 @@ void addPhasesDataToJson(nlohmann::json& json, PhaseType amountOfPhasesToAdd, st json["phases"] = phases; } +TEST_F(TestOptionalPhasesMetadata, test_no_metadata) { + using LBDataHolder = vt::vrt::collection::balance::LBDataHolder; + + nlohmann::json json; + addPhasesDataToJson(json, num_phases, {}); + + LBDataHolder testObj(json); + EXPECT_EQ(testObj.count_, num_phases); + std::set expectedSkipped = {}; + EXPECT_EQ(testObj.skipped_phases_, expectedSkipped); + std::set expectedIdentical = {}; + EXPECT_EQ(testObj.identical_phases_, expectedIdentical); + + auto outJsonPtr = testObj.metadataToJson(); + ASSERT_TRUE(outJsonPtr != nullptr); + + EXPECT_EQ((*outJsonPtr)["count"], num_phases); + std::vector expectedSkippedList = {}; + EXPECT_EQ((*outJsonPtr)["skipped"]["list"], expectedSkippedList); + std::vector> expectedSkippedRanges = {}; + EXPECT_EQ((*outJsonPtr)["skipped"]["range"], expectedSkippedRanges); + std::vector expectedIdenticalList = {}; + EXPECT_EQ((*outJsonPtr)["identical_to_previous"]["list"], expectedIdenticalList); + std::vector> expectedIdenticalRanges = {}; + EXPECT_EQ((*outJsonPtr)["identical_to_previous"]["range"], expectedIdenticalRanges); +} + TEST_F(TestOptionalPhasesMetadata, test_no_lb_phases_metadata) { using LBDataHolder = vt::vrt::collection::balance::LBDataHolder;