From 240ac77e69786b0485afa03bfd5389e2c957a087 Mon Sep 17 00:00:00 2001 From: NeaguGeorgiana23 Date: Tue, 27 Jan 2026 11:14:50 +0000 Subject: [PATCH 1/3] feat: add global context manager Signed-off-by: NeaguGeorgiana23 --- .bazelversion | 1 + openfeature/BUILD | 12 ++++ openfeature/global_context_manager.cpp | 23 +++++++ openfeature/global_context_manager.h | 34 ++++++++++ test/BUILD | 11 ++++ test/global_context_manager_test.cpp | 89 ++++++++++++++++++++++++++ 6 files changed, 170 insertions(+) create mode 100644 .bazelversion create mode 100644 openfeature/global_context_manager.cpp create mode 100644 openfeature/global_context_manager.h create mode 100644 test/global_context_manager_test.cpp diff --git a/.bazelversion b/.bazelversion new file mode 100644 index 0000000..6b0e58e --- /dev/null +++ b/.bazelversion @@ -0,0 +1 @@ +7.4.1 \ No newline at end of file diff --git a/openfeature/BUILD b/openfeature/BUILD index f05a514..fa0e587 100644 --- a/openfeature/BUILD +++ b/openfeature/BUILD @@ -1,3 +1,5 @@ +load("@rules_cc//cc:defs.bzl", "cc_library") + package( default_visibility = ["//visibility:public"], ) @@ -55,6 +57,16 @@ cc_library( include_prefix = "openfeature", ) +cc_library( + name = "global_context_manager", + srcs = ["global_context_manager.cpp"], + hdrs = ["global_context_manager.h"], + include_prefix = "openfeature", + deps = [ + ":evaluation_context", + ], +) + cc_library( name = "metadata", hdrs = ["metadata.h"], diff --git a/openfeature/global_context_manager.cpp b/openfeature/global_context_manager.cpp new file mode 100644 index 0000000..c356e22 --- /dev/null +++ b/openfeature/global_context_manager.cpp @@ -0,0 +1,23 @@ +#include "openfeature/global_context_manager.h" + +#include + +namespace openfeature { + +GlobalContextManager& GlobalContextManager::GetInstance() { + static GlobalContextManager instance; + return instance; +} + +void GlobalContextManager::SetGlobalEvaluationContext( + const EvaluationContext& ctx) { + std::unique_lock lock(context_mutex_); + global_evaluation_context_ = ctx; +} + +EvaluationContext GlobalContextManager::GetGlobalEvaluationContext() const { + std::shared_lock lock(context_mutex_); + return global_evaluation_context_; +} + +} // namespace openfeature \ No newline at end of file diff --git a/openfeature/global_context_manager.h b/openfeature/global_context_manager.h new file mode 100644 index 0000000..53a24f2 --- /dev/null +++ b/openfeature/global_context_manager.h @@ -0,0 +1,34 @@ +#ifndef CPP_SDK_INCLUDE_OPENFEATURE_GLOBAL_CONTEXT_MANAGER_H_ +#define CPP_SDK_INCLUDE_OPENFEATURE_GLOBAL_CONTEXT_MANAGER_H_ + +#include +#include + +#include "openfeature/evaluation_context.h" + +namespace openfeature { + +// Manages the global Evaluation Context for the OpenFeature SDK. +// This data is static across the application (unless explicitly changed) +class GlobalContextManager { + public: + static GlobalContextManager& GetInstance(); + + GlobalContextManager(const GlobalContextManager&) = delete; + GlobalContextManager& operator=(const GlobalContextManager&) = delete; + + // Updates the global evaluation context. + void SetGlobalEvaluationContext(const EvaluationContext& ctx); + + // Retrieves the current global evaluation context. + EvaluationContext GetGlobalEvaluationContext() const; + + private: + GlobalContextManager() = default; + EvaluationContext global_evaluation_context_; + mutable std::shared_mutex context_mutex_; +}; + +} // namespace openfeature + +#endif // CPP_SDK_INCLUDE_OPENFEATURE_GLOBAL_CONTEXT_MANAGER_H_ \ No newline at end of file diff --git a/test/BUILD b/test/BUILD index bddb116..3d38a78 100644 --- a/test/BUILD +++ b/test/BUILD @@ -1,3 +1,5 @@ +load("@rules_cc//cc:defs.bzl", "cc_library", "cc_test") + cc_library( name = "mock_feature_provider", testonly = True, @@ -46,3 +48,12 @@ cc_test( "@googletest//:gtest_main", ], ) + +cc_test( + name = "global_context_manager_test", + srcs = ["global_context_manager_test.cpp"], + deps = [ + "//openfeature:global_context_manager", + "@googletest//:gtest_main", + ], +) diff --git a/test/global_context_manager_test.cpp b/test/global_context_manager_test.cpp new file mode 100644 index 0000000..9986f4e --- /dev/null +++ b/test/global_context_manager_test.cpp @@ -0,0 +1,89 @@ +#include "openfeature/global_context_manager.h" + +#include + +#include +#include +#include + +#include "openfeature/evaluation_context.h" + +using namespace openfeature; + +class GlobalContextManagerTest : public ::testing::Test { + protected: + // Reset to a clean state before every test. + void SetUp() override { + GlobalContextManager::GetInstance().SetGlobalEvaluationContext( + EvaluationContext{}); + } +}; + +TEST_F(GlobalContextManagerTest, ReturnsSameInstance) { + GlobalContextManager& instance1 = GlobalContextManager::GetInstance(); + GlobalContextManager& instance2 = GlobalContextManager::GetInstance(); + + EXPECT_EQ(&instance1, &instance2); +} + +TEST_F(GlobalContextManagerTest, SetAndGetContext) { + GlobalContextManager& manager = GlobalContextManager::GetInstance(); + EvaluationContext input_ctx; + + EXPECT_NO_THROW(manager.SetGlobalEvaluationContext(input_ctx)); + + EvaluationContext output_ctx = manager.GetGlobalEvaluationContext(); + EXPECT_NO_THROW(output_ctx = manager.GetGlobalEvaluationContext()); + + // NOTE: Since EvaluationContext is currently an empty class, + // we cannot assert EQ or check member values. + // Currently, the test simply proves the API calls succeed and copy semantics + // work. + + // TODO: Add assertions. +} + +TEST_F(GlobalContextManagerTest, ThreadSafetyStressTest) { + GlobalContextManager& manager = GlobalContextManager::GetInstance(); + std::atomic stop{false}; + + // Writer Thread: Continuously updates the context. + std::thread writer([&]() { + while (!stop) { + EvaluationContext ctx; + // In a real scenario, we would populate ctx with different data here. + manager.SetGlobalEvaluationContext(ctx); + std::this_thread::sleep_for(std::chrono::milliseconds(1)); + } + }); + + // Reader Threads: Continuously read the context. + std::vector readers; + for (int i = 0; i < 10; ++i) { + readers.emplace_back([&]() { + while (!stop) { + // Just calling the getter to ensure locks work and no race conditions + // occur resulting in a crash (segfault). + EvaluationContext ctx = manager.GetGlobalEvaluationContext(); + + // Prevent optimization from removing the call. + volatile size_t size = sizeof(ctx); + (void)size; + } + }); + } + + // Let the chaos run for a short duration. + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + + stop = true; + writer.join(); + for (auto& t : readers) { + t.join(); + } +} + +int main(int argc, char** argv) { + testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} \ No newline at end of file From 856330694ad737b011825a800c14970646d5ee52 Mon Sep 17 00:00:00 2001 From: NeaguGeorgiana23 Date: Tue, 27 Jan 2026 12:34:36 +0000 Subject: [PATCH 2/3] feat: add client api and boolean evaluation Signed-off-by: NeaguGeorgiana23 --- openfeature/BUILD | 20 +++++++ openfeature/client_api.cpp | 71 +++++++++++++++++++++++++ openfeature/client_api.h | 69 +++++++++++++++++++++++++ test/BUILD | 10 ++++ test/client_api_test.cpp | 103 +++++++++++++++++++++++++++++++++++++ 5 files changed, 273 insertions(+) create mode 100644 openfeature/client_api.cpp create mode 100644 openfeature/client_api.h create mode 100644 test/client_api_test.cpp diff --git a/openfeature/BUILD b/openfeature/BUILD index fa0e587..2c82f6e 100644 --- a/openfeature/BUILD +++ b/openfeature/BUILD @@ -16,6 +16,26 @@ cc_library( ], ) +cc_library( + name = "client_api", + srcs = ["client_api.cpp"], + hdrs = ["client_api.h"], + include_prefix = "openfeature", + deps = [ + ":client", + ":evaluation_context", + ":features", + ":flag_metadata", + ":global_context_manager", + ":metadata", + ":provider", + ":provider_repository", + ":provider_status", + ":reason", + ":resolution_details", + ], +) + cc_library( name = "error_code", hdrs = ["error_code.h"], diff --git a/openfeature/client_api.cpp b/openfeature/client_api.cpp new file mode 100644 index 0000000..93ca102 --- /dev/null +++ b/openfeature/client_api.cpp @@ -0,0 +1,71 @@ +#include "client_api.h" + +#include + +#include "openfeature/flag_metadata.h" +#include "openfeature/global_context_manager.h" +#include "openfeature/reason.h" + +namespace openfeature { + +ClientAPI::ClientAPI(ProviderRepository& repository, std::string_view domain) + : provider_repository_(repository), domain_(domain) {} + +Metadata ClientAPI::GetMetadata() { return Metadata{domain_}; } + +EvaluationContext ClientAPI::GetEvaluationContext() { + std::lock_guard lock(context_mutex_); + return evaluation_context_; +} + +void ClientAPI::SetEvaluationContext(const EvaluationContext& ctx) { + std::lock_guard lock(context_mutex_); + evaluation_context_ = ctx; +} + +ProviderStatus ClientAPI::GetProviderStatus() { + return provider_repository_.GetProviderStatus(domain_); +} + +bool ClientAPI::GetBooleanValue(std::string_view flag_key, bool default_value) { + return EvaluateBooleanFlag(flag_key, default_value, std::nullopt)->GetValue(); +} + +bool ClientAPI::GetBooleanValue(std::string_view flag_key, bool default_value, + const EvaluationContext& ctx) { + return EvaluateBooleanFlag(flag_key, default_value, ctx)->GetValue(); +} + +std::unique_ptr ClientAPI::EvaluateBooleanFlag( + std::string_view flag_key, bool default_value, + const std::optional& ctx) { + if (GetProviderStatus() != ProviderStatus::kReady) { + return std::make_unique( + default_value, Reason::kError, std::nullopt, FlagMetadata(), + ErrorCode::kProviderNotReady, "Provider is not ready"); + } + + std::shared_ptr provider = + provider_repository_.GetProvider(domain_); + if (!provider) { + return std::make_unique( + default_value, Reason::kError, std::nullopt, FlagMetadata(), + ErrorCode::kProviderFatal, "Provider not found for domain"); + } + + EvaluationContext merged_context = MergeContexts(ctx); + return provider->GetBooleanEvaluation(flag_key, default_value, + merged_context); +} + +EvaluationContext ClientAPI::MergeContexts( + const std::optional& invocation_ctx) { + // TODO: Add context merging logic after EvaluationContext is implemented. + + if (invocation_ctx) { + return *invocation_ctx; + } + return GetEvaluationContext(); +} + +} // namespace openfeature \ No newline at end of file diff --git a/openfeature/client_api.h b/openfeature/client_api.h new file mode 100644 index 0000000..4a3d1ed --- /dev/null +++ b/openfeature/client_api.h @@ -0,0 +1,69 @@ +#ifndef CPP_SDK_INCLUDE_OPENFEATURE_CLIENT_API_H_ +#define CPP_SDK_INCLUDE_OPENFEATURE_CLIENT_API_H_ + +#include +#include +#include +#include +#include + +#include "openfeature/client.h" +#include "openfeature/evaluation_context.h" +#include "openfeature/features.h" +#include "openfeature/global_context_manager.h" +#include "openfeature/metadata.h" +#include "openfeature/provider.h" +#include "openfeature/provider_repository.h" +#include "openfeature/provider_status.h" +#include "openfeature/resolution_details.h" + +namespace openfeature { + +// OpenFeature client implementation. +class ClientAPI : public Client { + public: + ClientAPI(ProviderRepository& repository, std::string_view domain); + + ~ClientAPI() override = default; + + ClientAPI(const ClientAPI&) = delete; + ClientAPI& operator=(const ClientAPI&) = delete; + + Metadata GetMetadata() override; + + // Return an optional client-level evaluation context. + EvaluationContext GetEvaluationContext() override; + + // Set the client-level evaluation context. + void SetEvaluationContext(const EvaluationContext& ctx) override; + + // Returns the current status of the associated provider. + ProviderStatus GetProviderStatus() override; + + bool GetBooleanValue(std::string_view flag_key, bool default_value) override; + bool GetBooleanValue(std::string_view flag_key, bool default_value, + const EvaluationContext& ctx) override; + + // TODO: Add methods to get and set Hooks. + // TODO: Add methods for flag evaluation for other types (e.g. string, int, + // float, object). + // TODO: Add methods for detailed flag evaluation. + // TODO: Overload method "GetBooleanValue" to accept "Evaluation Options". + + private: + std::unique_ptr EvaluateBooleanFlag( + std::string_view flag_key, bool default_value, + const std::optional& ctx); + + EvaluationContext MergeContexts( + const std::optional& invocation_ctx); + + ProviderRepository& provider_repository_; + std::string domain_; + EvaluationContext evaluation_context_; + mutable std::mutex context_mutex_; +}; + +} // namespace openfeature + +#endif // CPP_SDK_INCLUDE_OPENFEATURE_CLIENT_API_H_ diff --git a/test/BUILD b/test/BUILD index 3d38a78..620c958 100644 --- a/test/BUILD +++ b/test/BUILD @@ -11,6 +11,16 @@ cc_library( ], ) +cc_test( + name = "client_api_test", + srcs = ["client_api_test.cpp"], + deps = [ + ":mock_feature_provider", + "//openfeature:client_api", + "@googletest//:gtest_main", + ], +) + cc_test( name = "feature_provider_status_manager_test", srcs = ["feature_provider_status_manager_test.cpp"], diff --git a/test/client_api_test.cpp b/test/client_api_test.cpp new file mode 100644 index 0000000..8c5039a --- /dev/null +++ b/test/client_api_test.cpp @@ -0,0 +1,103 @@ +#include "openfeature/client_api.h" + +#include +#include + +#include +#include + +#include "absl/status/status.h" +#include "mocks/mock_feature_provider.h" +#include "openfeature/evaluation_context.h" +#include "openfeature/global_context_manager.h" +#include "openfeature/provider_status.h" + +using namespace openfeature; +using ::testing::_; +using ::testing::NiceMock; +using ::testing::Return; +using ::testing::StrictMock; + +class ClientAPITest : public ::testing::Test { + protected: + void SetUp() override { + // Reset the Global Context to a clean state before each test. + GlobalContextManager::GetInstance().SetGlobalEvaluationContext( + EvaluationContext{}); + } + ProviderRepository repo_; +}; + +// Test that the constructor correctly sets the domain in the metadata. +TEST_F(ClientAPITest, ConstructorSetsDomainMetadata) { + std::string domain = "test-domain"; + ClientAPI client(repo_, domain); + + Metadata metadata = client.GetMetadata(); + EXPECT_EQ(metadata.name, domain); +} + +// Test that the provider status is Ready by default. +TEST_F(ClientAPITest, GetProviderStatusDefaultsToReady) { + ClientAPI client(repo_, "test-domain"); + EXPECT_EQ(client.GetProviderStatus(), ProviderStatus::kReady); +} + +// Test setting and getting the EvaluationContext. +TEST_F(ClientAPITest, SetAndGetEvaluationContext) { + ClientAPI client(repo_, "test-domain"); + EvaluationContext ctx; + + // Verify we can set the context without error. + EXPECT_NO_THROW(client.SetEvaluationContext(ctx)); +} + +// Test that GetBooleanValue returns the default value when using the default +// provider. +TEST_F(ClientAPITest, GetBooleanValueReturnsDefaultWithNoopProvider) { + ClientAPI client(repo_, "test-domain"); + std::string flag_key = "my-boolean-flag"; + + EXPECT_TRUE(client.GetBooleanValue(flag_key, true)); + + EXPECT_FALSE(client.GetBooleanValue(flag_key, false)); +} + +// Test GetBooleanValue with an EvaluationContext passed in. +TEST_F(ClientAPITest, GetBooleanValueWithContextReturnsDefault) { + ClientAPI client(repo_, "test-domain"); + EvaluationContext ctx; + std::string flag_key = "my-boolean-flag"; + + EXPECT_TRUE(client.GetBooleanValue(flag_key, true, ctx)); + EXPECT_FALSE(client.GetBooleanValue(flag_key, false, ctx)); +} + +// Test context merging logic indirectly. +TEST_F(ClientAPITest, GetBooleanValueSafeWithMergedContexts) { + EvaluationContext global_ctx; + GlobalContextManager::GetInstance().SetGlobalEvaluationContext(global_ctx); + + ClientAPI client(repo_, "test-domain"); + EvaluationContext client_ctx; + client.SetEvaluationContext(client_ctx); + + EvaluationContext invocation_ctx; + + // This call forces a merge of Global + Client + Invocation contexts. + // We expect the NoopProvider to handle the result gracefully (return + // default). + EXPECT_TRUE(client.GetBooleanValue("flag", true, invocation_ctx)); +} + +// Test behavior when the domain is empty. +TEST_F(ClientAPITest, WorksWithEmptyDomain) { + ClientAPI client(repo_, ""); + EXPECT_EQ(client.GetMetadata().name, ""); + EXPECT_TRUE(client.GetBooleanValue("flag", true)); +} + +// TODO: If ClientAPI is refactored to allow injecting a MockFeatureProvider +// (e.g. by passing the OpenFeatureAPI's repository or via constructor), +// add tests here to verify that the MockProvider receives the correct +// flag key and merged EvaluationContext. \ No newline at end of file From 71c09c76e9412fe6a10b3b2744f7b9463e92203e Mon Sep 17 00:00:00 2001 From: NeaguGeorgiana23 <115723925+NeaguGeorgiana23@users.noreply.github.com> Date: Tue, 27 Jan 2026 14:26:10 +0100 Subject: [PATCH 3/3] Update openfeature/global_context_manager.cpp Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> Signed-off-by: NeaguGeorgiana23 <115723925+NeaguGeorgiana23@users.noreply.github.com> --- openfeature/global_context_manager.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/openfeature/global_context_manager.cpp b/openfeature/global_context_manager.cpp index c356e22..7829223 100644 --- a/openfeature/global_context_manager.cpp +++ b/openfeature/global_context_manager.cpp @@ -1,6 +1,7 @@ #include "openfeature/global_context_manager.h" #include +#include namespace openfeature {