Skip to content
Open
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
13 changes: 10 additions & 3 deletions openfeature/client_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,15 @@ template <typename ResolutionDetailsType, typename ValueType,
std::unique_ptr<ResolutionDetailsType> ClientAPI::EvaluateFlag(
ValueType default_value, const std::optional<EvaluationContext>& ctx,
ProviderCallable provider_call) {
ProviderStatus status = GetProviderStatus();
std::shared_ptr<FeatureProviderStatusManager> manager =
provider_repository_.GetFeatureProviderStatusManager(domain_);
if (!manager) {
return std::make_unique<ResolutionDetailsType>(
default_value, Reason::kError, std::nullopt, FlagMetadata(),
ErrorCode::kGeneral, "Provider status manager not found for domain");
}

ProviderStatus status = manager->GetStatus();
Comment thread
NeaguGeorgiana23 marked this conversation as resolved.
if (status == ProviderStatus::kNotReady) {
return std::make_unique<ResolutionDetailsType>(
default_value, Reason::kError, std::nullopt, FlagMetadata(),
Expand All @@ -125,8 +133,7 @@ std::unique_ptr<ResolutionDetailsType> ClientAPI::EvaluateFlag(
ErrorCode::kProviderFatal, "Provider is in fatal error state");
}

std::shared_ptr<FeatureProvider> provider =
provider_repository_.GetProvider(domain_);
std::shared_ptr<FeatureProvider> provider = manager->GetProvider();
if (!provider) {
return std::make_unique<ResolutionDetailsType>(
default_value, Reason::kError, std::nullopt, FlagMetadata(),
Expand Down
59 changes: 59 additions & 0 deletions test/client_api_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@
#include <gmock/gmock.h>
#include <gtest/gtest.h>

#include <atomic>
#include <chrono>
#include <future>
#include <memory>
#include <string>
#include <thread>

#include "absl/status/status.h"
#include "mocks/mock_feature_provider.h"
Expand Down Expand Up @@ -42,6 +46,7 @@ class ClientAPITest : public ::testing::Test {
};

constexpr int kUnknownExceptionError = 43;
constexpr int kSleepTimeMs = 10;

// Test that the constructor correctly sets the domain in the metadata.
TEST_F(ClientAPITest, ConstructorSetsDomainMetadata) {
Expand Down Expand Up @@ -381,3 +386,57 @@ TEST_F(ClientAPITest, EvaluateFlagProceedsWhenProviderInStaleState) {

EXPECT_TRUE(client.GetBooleanValue("flag", false));
}

TEST_F(ClientAPITest, ParallelProviderSwapRaceCondition) {
std::string domain = "race-domain";
ClientAPI client(repo_, domain);
std::atomic<bool> running{true};

auto ready_provider = std::make_shared<NiceMock<MockFeatureProvider>>();
ON_CALL(*ready_provider, Init(_)).WillByDefault(Return(absl::OkStatus()));
ON_CALL(*ready_provider, GetBooleanEvaluation(_, _, _))
.WillByDefault(testing::Invoke(
[](std::string_view, bool, const EvaluationContext&)
-> absl::StatusOr<std::unique_ptr<BoolResolutionDetails>> {
return std::make_unique<BoolResolutionDetails>(
true, Reason::kTargetingMatch, std::nullopt, FlagMetadata());
}));
repo_.SetProvider(domain, ready_provider,
EvaluationContext::Builder().build(), true);

std::thread evaluation_thread([&]() {
while (running) {
client.GetBooleanValue("flag", false);
std::this_thread::yield(); // Friendly to single-core CI runners
}
});

auto not_ready_provider = std::make_shared<StrictMock<MockFeatureProvider>>();

auto init_called = std::make_shared<std::promise<void>>();
auto proceed_init = std::make_shared<std::promise<void>>();
std::shared_future<void> proceed_future = proceed_init->get_future().share();

EXPECT_CALL(*not_ready_provider, Init(_))
.WillOnce(testing::Invoke([init_called, proceed_future](
const EvaluationContext&) -> absl::Status {
init_called->set_value();
proceed_future.wait();
return absl::OkStatus();
}));

EXPECT_CALL(*not_ready_provider, GetBooleanEvaluation(_, _, _)).Times(0);
EXPECT_CALL(*not_ready_provider, Shutdown())
.Times(testing::AtMost(1))
.WillOnce(Return(absl::OkStatus()));

repo_.SetProvider(domain, not_ready_provider,
EvaluationContext::Builder().build(), false);

init_called->get_future().wait();

std::this_thread::sleep_for(std::chrono::milliseconds(kSleepTimeMs));
running = false;
evaluation_thread.join();
proceed_init->set_value();
}