Skip to content

Commit 8525e45

Browse files
committed
use an rspec stub instead of a hook setter
1 parent 6b892e7 commit 8525e45

File tree

2 files changed

+8
-16
lines changed

2 files changed

+8
-16
lines changed

lib/ldclient-rb/redis_store.rb

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -178,18 +178,12 @@ def stop
178178
end
179179
end
180180

181-
# exposed for testing
182-
def clear_local_cache()
183-
@cache.clear
184-
end
181+
private
185182

186183
# exposed for testing
187-
def set_transaction_hook(proc)
188-
@transaction_hook = proc
184+
def before_update_transaction(base_key, key)
189185
end
190186

191-
private
192-
193187
def items_key(kind)
194188
@prefix + ":" + kind[:namespace]
195189
end
@@ -231,9 +225,7 @@ def update_with_versioning(kind, new_item)
231225
with_connection do |redis|
232226
redis.watch(base_key) do
233227
old_item = get_redis(kind, redis, key)
234-
if @transaction_hook
235-
@transaction_hook.call(base_key, key)
236-
end
228+
before_update_transaction(base_key, key)
237229
if old_item.nil? || old_item[:version] < new_item[:version]
238230
begin
239231
result = redis.multi do |multi|

spec/redis_feature_store_spec.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,16 @@ def create_redis_store_uncached()
3232
include_examples "feature_store", method(:create_redis_store_uncached)
3333
end
3434

35-
def make_concurrent_modifier(other_client, flag, start_version, end_version)
35+
def add_concurrent_modifier(store, other_client, flag, start_version, end_version)
3636
version_counter = start_version
37-
Proc.new do |base_key, key|
37+
expect(store).to receive(:before_update_transaction) { |base_key, key|
3838
if version_counter <= end_version
3939
new_flag = flag.clone
4040
new_flag[:version] = version_counter
4141
other_client.hset(base_key, key, new_flag.to_json)
4242
version_counter = version_counter + 1
4343
end
44-
end
44+
}.at_least(:once)
4545
end
4646

4747
it "handles upsert race condition against external client with lower version" do
@@ -52,7 +52,7 @@ def make_concurrent_modifier(other_client, flag, start_version, end_version)
5252
flag = { key: "foo", version: 1 }
5353
store.init(LaunchDarkly::FEATURES => { flag[:key] => flag })
5454

55-
store.set_transaction_hook(make_concurrent_modifier(other_client, flag, 2, 4))
55+
add_concurrent_modifier(store, other_client, flag, 2, 4)
5656

5757
my_ver = { key: "foo", version: 10 }
5858
store.upsert(LaunchDarkly::FEATURES, my_ver)
@@ -71,7 +71,7 @@ def make_concurrent_modifier(other_client, flag, start_version, end_version)
7171
flag = { key: "foo", version: 1 }
7272
store.init(LaunchDarkly::FEATURES => { flag[:key] => flag })
7373

74-
store.set_transaction_hook(make_concurrent_modifier(other_client, flag, 3, 3))
74+
add_concurrent_modifier(store, other_client, flag, 3, 3)
7575

7676
my_ver = { key: "foo", version: 2 }
7777
store.upsert(LaunchDarkly::FEATURES, my_ver)

0 commit comments

Comments
 (0)