Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix crash on empty segment. (uplift to 1.36.x) #12276

Merged
merged 1 commit into from
Feb 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ SegmentList GetEligibleSegments() {
EpsilonGreedyBanditArmMap GetEligibleArms(
const EpsilonGreedyBanditArmMap& arms) {
const SegmentList eligible_segments = GetEligibleSegments();
if (eligible_segments.empty()) {
return {};
}

EpsilonGreedyBanditArmMap eligible_arms;

Expand Down Expand Up @@ -119,7 +122,7 @@ ArmList GetTopArms(const ArmBucketList& buckets, const size_t count) {
ArmList arms = bucket.second;
if (arms.size() > available_arms) {
// Sample without replacement
base::RandomShuffle(begin(arms), end(arms));
base::RandomShuffle(std::begin(arms), std::end(arms));
arms.resize(available_arms);
}

Expand All @@ -136,8 +139,10 @@ SegmentList ExploreSegments(const EpsilonGreedyBanditArmMap& arms) {
segments.push_back(arm.first);
}

base::RandomShuffle(begin(segments), end(segments));
segments.resize(kTopArmCount);
if (segments.size() > kTopArmCount) {
base::RandomShuffle(std::begin(segments), std::end(segments));
segments.resize(kTopArmCount);
}

BLOG(2, "Exploring epsilon greedy bandit segments:");
for (const auto& segment : segments) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,22 @@ TEST_F(BatAdsEpsilonGreedyBanditModelTest,
EXPECT_TRUE(segments.empty());
}

TEST_F(BatAdsEpsilonGreedyBanditModelTest, EligableSegmentsAreEmpty) {
// Arrange
base::test::ScopedFeatureList scoped_feature_list;
scoped_feature_list.InitAndEnableFeatureWithParameters(
features::kEpsilonGreedyBandit, {{"epsilon_value", "0.5"}});

processor::EpsilonGreedyBandit processor;

// Act
EpsilonGreedyBandit model;
const SegmentList segments = model.GetSegments();

// Assert
EXPECT_TRUE(segments.empty());
}

TEST_F(BatAdsEpsilonGreedyBanditModelTest, GetSegmentsIfNeverProcessed) {
// Arrange
SaveAllSegments();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,33 @@
#include "bat/ads/internal/segments/segments_json_reader.h"

#include "base/json/json_reader.h"
#include "base/notreached.h"
#include "base/values.h"
#include "third_party/abseil-cpp/absl/types/optional.h"

namespace ads {
namespace JSONReader {

SegmentList ReadSegments(const std::string& json) {
SegmentList segments;

absl::optional<base::Value> value = base::JSONReader::Read(json);
if (!value) {
return segments;
return {};
}

base::ListValue* list = nullptr;
if (!value->GetAsList(&list)) {
return segments;
return {};
}

SegmentList segments;
for (const auto& element : list->GetList()) {
if (!element.is_string()) {
NOTREACHED();
continue;
return {};
}

const std::string segment = element.GetString();
DCHECK(!segment.empty());
if (segment.empty()) {
return {};
}

segments.push_back(segment);
}
Expand Down