Skip to content

Commit ee6ae1d

Browse files
committed
Add tests for cache
1 parent 784f1a4 commit ee6ae1d

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

helpers/sql-obfuscation/test/helpers/query_summary/cache_test.rb

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,39 @@ def test_fetch_returns_value_when_key_exists
1717

1818
assert_equal 'value1', result
1919
end
20+
21+
def test_eviction_when_cache_size_exceeded
22+
small_cache = OpenTelemetry::Helpers::QuerySummary::Cache.new(size: 2)
23+
24+
small_cache.fetch('key1') { 'value1' }
25+
small_cache.fetch('key2') { 'value2' }
26+
small_cache.fetch('key3') { 'value3' }
27+
28+
result = small_cache.fetch('key1') { 'new_value1' }
29+
assert_equal 'new_value1', result
30+
end
31+
32+
def test_cache_thread_safety
33+
threads = Array.new(10) do |i|
34+
Thread.new do
35+
@cache.fetch('shared_key') { "thread_#{i}_value" }
36+
end
37+
end
38+
39+
results = threads.map(&:value)
40+
41+
assert_equal 1, results.uniq.size
42+
end
43+
44+
def test_empty_string
45+
@cache.fetch('') { 'empty_string_value' }
46+
47+
assert_equal 'empty_string_value', @cache.fetch('')
48+
end
49+
50+
def test_nil
51+
@cache.fetch(nil) { 'nil_value' }
52+
53+
assert_equal 'nil_value', @cache.fetch(nil)
54+
end
2055
end

0 commit comments

Comments
 (0)