33
44require "concurrent/atomics"
55
6+ require "mock_components"
67require "spec_helper"
78
89module LaunchDarkly
@@ -50,6 +51,7 @@ def with_manager(config)
5051 store = double
5152 expect ( store ) . to receive ( :get_metadata ) . at_least ( :once ) . and_return ( always_up_to_date )
5253 expect ( store ) . to receive ( :get_membership ) . with ( user_hash ) . once . and_return ( expected_membership )
54+ # the ".once" on this mock expectation is what verifies that the cache is working; there should only be one query
5355 allow ( store ) . to receive ( :stop )
5456
5557 with_manager ( BigSegmentsConfig . new ( store : store ) ) do |m |
@@ -59,6 +61,36 @@ def with_manager(config)
5961 end
6062 end
6163
64+ it "can cache a nil result" do
65+ store = double
66+ expect ( store ) . to receive ( :get_metadata ) . at_least ( :once ) . and_return ( always_up_to_date )
67+ expect ( store ) . to receive ( :get_membership ) . with ( user_hash ) . once . and_return ( nil )
68+ # the ".once" on this mock expectation is what verifies that the cache is working; there should only be one query
69+ allow ( store ) . to receive ( :stop )
70+
71+ with_manager ( BigSegmentsConfig . new ( store : store ) ) do |m |
72+ expected_result = BigSegmentMembershipResult . new ( { } , BigSegmentsStatus ::HEALTHY )
73+ expect ( m . get_user_membership ( user_key ) ) . to eq ( expected_result )
74+ expect ( m . get_user_membership ( user_key ) ) . to eq ( expected_result )
75+ end
76+ end
77+
78+ it "cache can expire" do
79+ expected_membership = { 'key1' => true , 'key2' => true }
80+ store = double
81+ expect ( store ) . to receive ( :get_metadata ) . at_least ( :once ) . and_return ( always_up_to_date )
82+ expect ( store ) . to receive ( :get_membership ) . with ( user_hash ) . twice . and_return ( expected_membership )
83+ # the ".twice" on this mock expectation is what verifies that the cached result expired
84+ allow ( store ) . to receive ( :stop )
85+
86+ with_manager ( BigSegmentsConfig . new ( store : store , user_cache_time : 0.01 ) ) do |m |
87+ expected_result = BigSegmentMembershipResult . new ( expected_membership , BigSegmentsStatus ::HEALTHY )
88+ expect ( m . get_user_membership ( user_key ) ) . to eq ( expected_result )
89+ sleep ( 0.1 )
90+ expect ( m . get_user_membership ( user_key ) ) . to eq ( expected_result )
91+ end
92+ end
93+
6294 it "with stale status" do
6395 expected_membership = { 'key1' => true , 'key2' => true }
6496 store = double
0 commit comments