diff --git a/lib/ldclient-openfeature/provider.rb b/lib/ldclient-openfeature/provider.rb index f339d43..640676d 100644 --- a/lib/ldclient-openfeature/provider.rb +++ b/lib/ldclient-openfeature/provider.rb @@ -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 diff --git a/spec/provider_spec.rb b/spec/provider_spec.rb index e704e0b..97cdccd 100644 --- a/spec/provider_spec.rb +++ b/spec/provider_spec.rb @@ -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)