Skip to content

Commit 78e7496

Browse files
authored
Merge pull request #136 from launchdarkly/eb/ch73982/nil-sdk-key
fail fast for nil SDK key when appropriate
2 parents 93112f5 + 3e55dc4 commit 78e7496

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

lib/ldclient-rb/events.rb

+1
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ class StopMessage < SynchronousMessage
9191
# @private
9292
class EventProcessor
9393
def initialize(sdk_key, config, client = nil, diagnostic_accumulator = nil, test_properties = nil)
94+
raise ArgumentError, "sdk_key must not be nil" if sdk_key.nil? # see LDClient constructor comment on sdk_key
9495
@logger = config.logger
9596
@inbox = SizedQueue.new(config.capacity < 100 ? 100 : config.capacity)
9697
@flush_task = Concurrent::TimerTask.new(execution_interval: config.flush_interval) do

lib/ldclient-rb/ldclient.rb

+11
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,16 @@ class LDClient
3333
# @return [LDClient] The LaunchDarkly client instance
3434
#
3535
def initialize(sdk_key, config = Config.default, wait_for_sec = 5)
36+
# Note that sdk_key is normally a required parameter, and a nil value would cause the SDK to
37+
# fail in most configurations. However, there are some configurations where it would be OK
38+
# (offline = true, *or* we are using LDD mode or the file data source and events are disabled
39+
# so we're not connecting to any LD services) so rather than try to check for all of those
40+
# up front, we will let the constructors for the data source implementations implement this
41+
# fail-fast as appropriate, and just check here for the part regarding events.
42+
if !config.offline? && config.send_events
43+
raise ArgumentError, "sdk_key must not be nil" if sdk_key.nil?
44+
end
45+
3646
@sdk_key = sdk_key
3747

3848
@event_factory_default = EventFactory.new(false)
@@ -352,6 +362,7 @@ def create_default_data_source(sdk_key, config, diagnostic_accumulator)
352362
if config.offline?
353363
return NullUpdateProcessor.new
354364
end
365+
raise ArgumentError, "sdk_key must not be nil" if sdk_key.nil? # see LDClient constructor comment on sdk_key
355366
requestor = Requestor.new(sdk_key, config)
356367
if config.stream?
357368
StreamProcessor.new(sdk_key, config, requestor, diagnostic_accumulator)

spec/ldclient_spec.rb

+40
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,46 @@ def event_processor
4949
client.instance_variable_get(:@event_processor)
5050
end
5151

52+
describe "constructor requirement of non-nil sdk key" do
53+
it "is not enforced when offline" do
54+
subject.new(nil, offline_config)
55+
end
56+
57+
it "is not enforced if use_ldd is true and send_events is false" do
58+
subject.new(nil, LaunchDarkly::Config.new({ use_ldd: true, send_events: false }))
59+
end
60+
61+
it "is not enforced if using file data and send_events is false" do
62+
source = LaunchDarkly::FileDataSource.factory({})
63+
subject.new(nil, LaunchDarkly::Config.new({ data_source: source, send_events: false }))
64+
end
65+
66+
it "is enforced in streaming mode even if send_events is false" do
67+
expect {
68+
subject.new(nil, LaunchDarkly::Config.new({ send_events: false }))
69+
}.to raise_error(ArgumentError)
70+
end
71+
72+
it "is enforced in polling mode even if send_events is false" do
73+
expect {
74+
subject.new(nil, LaunchDarkly::Config.new({ stream: false, send_events: false }))
75+
}.to raise_error(ArgumentError)
76+
end
77+
78+
it "is enforced if use_ldd is true and send_events is true" do
79+
expect {
80+
subject.new(nil, LaunchDarkly::Config.new({ use_ldd: true }))
81+
}.to raise_error(ArgumentError)
82+
end
83+
84+
it "is enforced if using file data and send_events is true" do
85+
source = LaunchDarkly::FileDataSource.factory({})
86+
expect {
87+
subject.new(nil, LaunchDarkly::Config.new({ data_source: source }))
88+
}.to raise_error(ArgumentError)
89+
end
90+
end
91+
5292
describe '#variation' do
5393
feature_with_value = { key: "key", on: false, offVariation: 0, variations: ["value"], version: 100,
5494
trackEvents: true, debugEventsUntilDate: 1000 }

0 commit comments

Comments
 (0)