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
25 changes: 25 additions & 0 deletions lib/ldclient-openfeature/provider.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,31 @@ def initialize(sdk_key, config = LaunchDarkly::Config.default, wait_for_seconds
@metadata = ::OpenFeature::SDK::Provider::ProviderMetadata.new(name: "launchdarkly-openfeature-server").freeze
end

#
# Called by the OpenFeature SDK when this provider is set. The LaunchDarkly client has already been given the
# opportunity to initialize, so this only reports whether that succeeded.
#
# @param _evaluation_context [::OpenFeature::SDK::EvaluationContext, nil]
#
# @return [void]
#
def init(_evaluation_context = nil)
return if @client.initialized?

state = @client.data_source_status_provider.status.state
raise "the LaunchDarkly client was unable to initialize; the data source state is #{state}"
end

#
# Called by the OpenFeature SDK when this provider is replaced or the SDK is shut down. The LaunchDarkly client
# cannot be restarted, so a new provider instance is required afterward.
#
# @return [void]
#
def shutdown
@client.close
end

def fetch_boolean_value(flag_key:, default_value:, evaluation_context: nil)
resolve_value(:boolean, flag_key, default_value, evaluation_context)
end
Expand Down
20 changes: 20 additions & 0 deletions spec/provider_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,26 @@
expect(provider.client).to be_a LaunchDarkly::LDClient
end

it "init succeeds when the client is initialized" do
expect { provider.init(evaluation_context) }.not_to raise_error
end

it "init raises when the client failed to initialize" do
status = LaunchDarkly::Interfaces::DataSource::Status.new(LaunchDarkly::Interfaces::DataSource::Status::OFF, Time.now, nil)
status_provider = double(status: status)
allow(provider.client).to receive_messages(initialized?: false, data_source_status_provider: status_provider)

expect { provider.init(evaluation_context) }.to raise_error(/unable to initialize/)
end

it "shutdown closes the client" do
allow(provider.client).to receive(:close)

provider.shutdown

expect(provider.client).to have_received(:close)
end

it "not providing context returns error" do
resolution_details = provider.fetch_boolean_value(flag_key: "flag-key", default_value: true)

Expand Down
Loading