Skip to content

Commit

Permalink
refactor!: Separate Client and Provider metadata, add client crea…
Browse files Browse the repository at this point in the history
…tion tests (#116)

Signed-off-by: Max VelDink <[email protected]>
  • Loading branch information
maxveldink authored Apr 4, 2024
1 parent f8e016f commit f028c39
Show file tree
Hide file tree
Showing 14 changed files with 82 additions and 62 deletions.
2 changes: 0 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,3 @@ source "https://rubygems.org"

# Specify your gem's dependencies in openfeature-sdk.gemspec
gemspec

gem "concurrent-ruby", require: "concurrent"
2 changes: 0 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ GEM
remote: https://rubygems.org/
specs:
ast (2.4.2)
concurrent-ruby (1.2.3)
debug (1.9.2)
irb (~> 1.10)
reline (>= 0.3.8)
Expand Down Expand Up @@ -102,7 +101,6 @@ PLATFORMS
x86_64-linux

DEPENDENCIES
concurrent-ruby
debug
markly
openfeature-sdk!
Expand Down
8 changes: 4 additions & 4 deletions lib/open_feature/sdk/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
require_relative "configuration"
require_relative "evaluation_context"
require_relative "evaluation_details"
require_relative "client_metadata"
require_relative "client"
require_relative "metadata"
require_relative "provider"

module OpenFeature
Expand Down Expand Up @@ -44,11 +44,11 @@ def configure(&block)
end

def build_client(name: nil, version: nil, domain: nil)
client_options = Metadata.new(name: name, version: version, domain: domain).freeze

active_provider = provider(domain:).nil? ? Provider::NoOpProvider.new : provider(domain:)

Client.new(provider: active_provider, client_options:, context:)
Client.new(provider: active_provider, domain:, context:)
rescue
Client.new(provider: Provider::NoOpProvider.new)
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/open_feature/sdk/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ class Client

attr_accessor :hooks

def initialize(provider:, client_options: nil, context: nil)
def initialize(provider:, domain: nil, context: nil)
@provider = provider
@metadata = client_options
@metadata = ClientMetadata.new(domain:)
@context = context
@hooks = []
end
Expand Down
5 changes: 5 additions & 0 deletions lib/open_feature/sdk/client_metadata.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module OpenFeature
module SDK
ClientMetadata = Struct.new(:domain, keyword_init: true)
end
end
4 changes: 0 additions & 4 deletions lib/open_feature/sdk/configuration.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# frozen_string_literal: true

require "concurrent"

require_relative "api"

module OpenFeature
Expand All @@ -15,8 +13,6 @@ class Configuration

attr_accessor :context, :hooks

def_delegator :provider, :metadata

def initialize
@hooks = []
@providers = {}
Expand Down
38 changes: 0 additions & 38 deletions lib/open_feature/sdk/metadata.rb

This file was deleted.

1 change: 1 addition & 0 deletions lib/open_feature/sdk/provider.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require_relative "provider/error_code"
require_relative "provider/reason"
require_relative "provider/resolution_details"
require_relative "provider/provider_metadata"
require_relative "provider/no_op_provider"
require_relative "provider/in_memory_provider"

Expand Down
2 changes: 1 addition & 1 deletion lib/open_feature/sdk/provider/in_memory_provider.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class InMemoryProvider
attr_reader :metadata

def initialize(flags = {})
@metadata = Metadata.new(name: NAME).freeze
@metadata = ProviderMetadata.new(name: NAME).freeze
@flags = flags
end

Expand Down
4 changes: 1 addition & 3 deletions lib/open_feature/sdk/provider/no_op_provider.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# frozen_string_literal: true

require_relative "../metadata"

# rubocop:disable Lint/UnusedMethodArgument
module OpenFeature
module SDK
Expand Down Expand Up @@ -31,7 +29,7 @@ class NoOpProvider
attr_reader :metadata

def initialize
@metadata = Metadata.new(name: NAME).freeze
@metadata = ProviderMetadata.new(name: NAME).freeze
end

def fetch_boolean_value(flag_key:, default_value:, evaluation_context: nil)
Expand Down
7 changes: 7 additions & 0 deletions lib/open_feature/sdk/provider/provider_metadata.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module OpenFeature
module SDK
module Provider
ProviderMetadata = Struct.new(:name, keyword_init: true)
end
end
end
4 changes: 2 additions & 2 deletions spec/open_feature/sdk/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@
end

it do
expect(api.provider.metadata).is_a?(OpenFeature::SDK::Metadata)
expect(api.provider.metadata).is_a?(OpenFeature::SDK::Provider::ProviderMetadata)
end

it do
expect(api.provider.metadata).to eq(OpenFeature::SDK::Metadata.new(name: OpenFeature::SDK::Provider::NoOpProvider::NAME))
expect(api.provider.metadata).to eq(OpenFeature::SDK::Provider::ProviderMetadata.new(name: OpenFeature::SDK::Provider::NoOpProvider::NAME))
end
end

Expand Down
5 changes: 1 addition & 4 deletions spec/open_feature/sdk/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
# https://openfeature.dev/docs/specification/sections/flag-evaluation#12-client-usage

RSpec.describe OpenFeature::SDK::Client do
subject(:client) { described_class.new(provider: provider, client_options: client_metadata) }
subject(:client) { described_class.new(provider: provider, domain:) }
let(:provider) { OpenFeature::SDK::Provider::NoOpProvider.new }
let(:client_metadata) { OpenFeature::SDK::Metadata.new(name: name, domain: domain) }
let(:domain) { "testing" }
let(:name) { "my-openfeature-client" }

Expand All @@ -27,8 +26,6 @@
context "Requirement 1.2.2" do
it "MUST define a metadata member or accessor, containing an immutable name field or accessor of type string, which corresponds to the name value supplied during client creation." do
expect(client).to respond_to(:metadata)
expect(client.metadata).to respond_to(:name)
expect(client.metadata.name).to eq(name)
expect(client.metadata.domain).to eq(domain)
end
end
Expand Down
58 changes: 58 additions & 0 deletions spec/specification/flag_evaluation_api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,5 +93,63 @@
expect(OpenFeature::SDK.provider(domain: "not_here").metadata.name).to eq("In-memory Provider")
end
end

context "Requirement 1.1.6" do
before do
default_provider = OpenFeature::SDK::Provider::InMemoryProvider.new
OpenFeature::SDK.set_provider(default_provider)

domain_1_provider = OpenFeature::SDK::Provider::NoOpProvider.new
OpenFeature::SDK.set_provider(domain_1_provider, domain: "domain_1")
end

specify "The API MUST provide a function for creating a client" do
client = OpenFeature::SDK.build_client

expect(client.instance_variable_get(:@provider).metadata.name).to eq("In-memory Provider")
end

specify "which accepts domain as an optional parameter." do
client = OpenFeature::SDK.build_client(domain: "domain_1")

expect(client.instance_variable_get(:@provider).metadata.name).to eq("No-op Provider")
end
end

context "Requirement 1.1.7" do
specify "The client creation function MUST NOT throw, or otherwise abnormally terminate." do
expect_any_instance_of(OpenFeature::SDK::Configuration).to receive(:provider).and_raise(StandardError)

expect do
client = OpenFeature::SDK.build_client

expect(client.instance_variable_get(:@provider).metadata.name).to eq("No-op Provider")
end.not_to raise_error
end
end
end

context "1.2 - Client Usage" do
context "Requirement 1.2.1" do
pending "The client MUST provide a method to add hooks which accepts one or more API-conformant hooks, and appends them to the collection of any previously added hooks. When new hooks are added, previously added hooks are not removed."
end

context "Requirement 1.2.2" do
before do
default_provider = OpenFeature::SDK::Provider::InMemoryProvider.new
OpenFeature::SDK.set_provider(default_provider)

domain_1_provider = OpenFeature::SDK::Provider::NoOpProvider.new
OpenFeature::SDK.set_provider(domain_1_provider, domain: "domain_1")
end

specify "The client interface MUST define a metadata member or accessor, containing an immutable domain field or accessor of type string, which corresponds to the domain value supplied during client creation." do
client = OpenFeature::SDK.build_client
expect(client.metadata.domain).to be_nil

client = OpenFeature::SDK.build_client(domain: "domain_1")
expect(client.metadata.domain).to eq("domain_1")
end
end
end
end

0 comments on commit f028c39

Please sign in to comment.