Skip to content

Commit

Permalink
all_flags_state is invalid if store isn't initialized (#182)
Browse files Browse the repository at this point in the history
  • Loading branch information
keelerm84 authored Jan 28, 2022
1 parent 5e7cd71 commit 64b2549
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/ldclient-rb/ldclient.rb
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,15 @@ def all_flags(user)
def all_flags_state(user, options={})
return FeatureFlagsState.new(false) if @config.offline?

if !initialized?
if @store.initialized?
@config.logger.warn { "Called all_flags_state before client initialization; using last known values from data store" }
else
@config.logger.warn { "Called all_flags_state before client initialization. Data store not available; returning empty state" }
return FeatureFlagsState.new(false)
end
end

unless user && !user[:key].nil?
@config.logger.error { "[LDClient] User and user key must be specified in all_flags_state" }
return FeatureFlagsState.new(false)
Expand Down
16 changes: 16 additions & 0 deletions spec/ldclient_evaluation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,22 @@ module LaunchDarkly
expect(state.values_map).to eq({})
end
end

it "returns empty state if store is not initialize" do
wait = double
expect(wait).to receive(:wait).at_least(:once)

source = double
expect(source).to receive(:start).at_least(:once).and_return(wait)
expect(source).to receive(:stop).at_least(:once).and_return(wait)
expect(source).to receive(:initialized?).at_least(:once).and_return(false)
store = LaunchDarkly::InMemoryFeatureStore.new
with_client(test_config(store: store, data_source: source)) do |offline_client|
state = offline_client.all_flags_state({ key: 'userkey' })
expect(state.valid?).to be false
expect(state.values_map).to eq({})
end
end
end
end
end

0 comments on commit 64b2549

Please sign in to comment.