From cf6ee38154c8d9c6c87dcae9a8072fe07329bc5c Mon Sep 17 00:00:00 2001 From: Eli Bishop Date: Mon, 26 Feb 2018 14:03:00 -0800 Subject: [PATCH 1/2] fix stream indirect patch method and add unit tests --- lib/ldclient-rb/stream.rb | 6 ++--- spec/stream_spec.rb | 49 +++++++++++++-------------------------- 2 files changed, 19 insertions(+), 36 deletions(-) diff --git a/lib/ldclient-rb/stream.rb b/lib/ldclient-rb/stream.rb index 671a80e5..1986abf7 100644 --- a/lib/ldclient-rb/stream.rb +++ b/lib/ldclient-rb/stream.rb @@ -110,13 +110,13 @@ def process_message(message, method) @initialized.make_true @config.logger.info("[LDClient] Stream initialized (via indirect message)") elsif method == INDIRECT_PATCH - key = feature_key_for_path(message.data) + key = key_for_path(FEATURES, message.data) if key @feature_store.upsert(FEATURES, @requestor.request_flag(key)) else - key = segment_key_for_path(message.data) + key = key_for_path(SEGMENTS, message.data) if key - @feature_store.upsert(SEGMENTS, key, @requestor.request_segment(key)) + @feature_store.upsert(SEGMENTS, @requestor.request_segment(key)) end end else diff --git a/spec/stream_spec.rb b/spec/stream_spec.rb index e4495b52..df27e173 100644 --- a/spec/stream_spec.rb +++ b/spec/stream_spec.rb @@ -1,42 +1,10 @@ require "spec_helper" require 'ostruct' -describe LaunchDarkly::InMemoryFeatureStore do - subject { LaunchDarkly::InMemoryFeatureStore } - - include LaunchDarkly - - let(:store) { subject.new } - let(:key) { :asdf } - let(:feature) { { key: "asdf", value: "qwer", version: 0 } } - - describe '#all' do - it "will get all keys" do - store.upsert(LaunchDarkly::FEATURES, feature) - data = store.all(LaunchDarkly::FEATURES) - expect(data).to eq(key => feature) - end - it "will not get deleted keys" do - store.upsert(LaunchDarkly::FEATURES, feature) - store.delete(LaunchDarkly::FEATURES, key, 1) - data = store.all(LaunchDarkly::FEATURES) - expect(data).to eq({}) - end - end - - describe '#initialized?' do - it "will return whether the store has been initialized" do - expect(store.initialized?).to eq false - store.init(key => feature) - expect(store.initialized?).to eq true - end - end -end - describe LaunchDarkly::StreamProcessor do subject { LaunchDarkly::StreamProcessor } let(:config) { LaunchDarkly::Config.new } - let(:requestor) { LaunchDarkly::Requestor.new("sdk_key", config)} + let(:requestor) { double() } let(:processor) { subject.new("sdk_key", config, requestor) } describe '#process_message' do @@ -45,6 +13,9 @@ let(:patch_seg_message) { OpenStruct.new({data: '{"path": "/segments/key", "data": {"key": "asdf", "version": 1}}'}) } let(:delete_flag_message) { OpenStruct.new({data: '{"path": "/flags/key", "version": 2}'}) } let(:delete_seg_message) { OpenStruct.new({data: '{"path": "/segments/key", "version": 2}'}) } + let(:indirect_patch_flag_message) { OpenStruct.new({data: "/flags/key"}) } + let(:indirect_patch_segment_message) { OpenStruct.new({data: "/segments/key"}) } + it "will accept PUT methods" do processor.send(:process_message, put_message, LaunchDarkly::PUT) expect(config.feature_store.get(LaunchDarkly::FEATURES, "asdf")).to eq(key: "asdf") @@ -68,6 +39,18 @@ processor.send(:process_message, delete_seg_message, LaunchDarkly::DELETE) expect(config.feature_store.get(LaunchDarkly::SEGMENTS, "key")).to eq(nil) end + it "will accept INDIRECT PATCH method for flags" do + flag = { key: 'key', version: 1 } + allow(requestor).to receive(:request_flag).with(flag[:key]).and_return(flag) + processor.send(:process_message, indirect_patch_flag_message, LaunchDarkly::INDIRECT_PATCH); + expect(config.feature_store.get(LaunchDarkly::FEATURES, flag[:key])).to eq(flag) + end + it "will accept INDIRECT PATCH method for segments" do + segment = { key: 'key', version: 1 } + allow(requestor).to receive(:request_segment).with(segment[:key]).and_return(segment) + processor.send(:process_message, indirect_patch_segment_message, LaunchDarkly::INDIRECT_PATCH); + expect(config.feature_store.get(LaunchDarkly::SEGMENTS, segment[:key])).to eq(segment) + end it "will log a warning if the method is not recognized" do expect(processor.instance_variable_get(:@config).logger).to receive :warn processor.send(:process_message, put_message, "get") From 10c6b1635612fad5096ca64a80d871ba387d0989 Mon Sep 17 00:00:00 2001 From: Eli Bishop Date: Mon, 26 Feb 2018 14:03:28 -0800 Subject: [PATCH 2/2] rm unused methods --- lib/ldclient-rb/requestor.rb | 8 -------- spec/requestor_spec.rb | 8 ++++---- 2 files changed, 4 insertions(+), 12 deletions(-) diff --git a/lib/ldclient-rb/requestor.rb b/lib/ldclient-rb/requestor.rb index 40928806..27ce822e 100644 --- a/lib/ldclient-rb/requestor.rb +++ b/lib/ldclient-rb/requestor.rb @@ -18,18 +18,10 @@ def initialize(sdk_key, config) end end - def request_all_flags() - make_request("/sdk/latest-flags") - end - def request_flag(key) make_request("/sdk/latest-flags/" + key) end - def request_all_segments() - make_request("/sdk/latest-segments") - end - def request_segment(key) make_request("/sdk/latest-segments/" + key) end diff --git a/spec/requestor_spec.rb b/spec/requestor_spec.rb index 21e06ed4..b7838200 100644 --- a/spec/requestor_spec.rb +++ b/spec/requestor_spec.rb @@ -19,12 +19,12 @@ allow(faraday).to receive(:get) do |*args, &block| req = double(Faraday::Request, :headers => {}, :options => Faraday::RequestOptions.new) block.call(req) - expect(args).to eq ['http://ld.com/sdk/latest-flags'] + expect(args).to eq ['http://ld.com/sdk/latest-all'] expect(req.options.proxy[:uri]).to eq URI("http://proxy.com") double(body: '{"foo": "bar"}', status: 200, headers: {}) end - requestor.request_all_flags() + requestor.request_all_data() end end describe "without a proxy" do @@ -42,11 +42,11 @@ allow(faraday).to receive(:get) do |*args, &block| req = double(Faraday::Request, :headers => {}, :options => Faraday::RequestOptions.new) block.call(req) - expect(args).to eq ['http://ld.com/sdk/latest-flags'] + expect(args).to eq ['http://ld.com/sdk/latest-all'] expect(req.options.proxy).to eq nil double(body: '{"foo": "bar"}', status: 200, headers: {}) end - requestor.request_all_flags() + requestor.request_all_data() end end end