Skip to content

Commit

Permalink
Merge pull request puppetlabs#110 from malikparvez/thread_safety
Browse files Browse the repository at this point in the history
Adding spec for thread safety
  • Loading branch information
bastelfreak authored Sep 25, 2023
2 parents a6b95f4 + 833c162 commit 7902337
Showing 1 changed file with 44 additions and 22 deletions.
66 changes: 44 additions & 22 deletions spec/unit/forge/lru_cache_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,18 +67,26 @@
expect(cache.send(:lru)).to eq(['foo', 'baz'])
end

# The below test is non-deterministic but I'm not sure how to unit
# test thread-safety.
# it 'is thread-safe' do
# cache = PuppetForge::LruCache.new
# cache.put('foo', 'bar')
# cache.put('baz', 'qux')
# threads = []
# threads << Thread.new { 100.times { cache.get('foo') } }
# threads << Thread.new { 100.times { cache.get('baz') } }
# threads.each(&:join)
# expect(cache.send(:lru)).to eq(['baz', 'foo'])
# end
it 'is thread-safe for get calls' do
cache = PuppetForge::LruCache.new

# Populate the cache with initial values
cache.put('foo', 'bar')
cache.put('baz', 'qux')

# Create two threads for concurrent cache get operations
thread_one = Thread.new do
100.times { expect(cache.get('foo')).to eq('bar') }
end

thread_two = Thread.new do
100.times { expect(cache.get('baz')).to eq('qux') }
end

# Wait for both threads to complete
thread_one.join
thread_two.join
end
end

context '#put' do
Expand All @@ -102,16 +110,30 @@
expect(cache.send(:lru)).to eq(['quux', 'baz'])
end

# The below test is non-deterministic but I'm not sure how to unit
# test thread-safety.
# it 'is thread-safe' do
# cache = PuppetForge::LruCache.new
# threads = []
# threads << Thread.new { 100.times { cache.put('foo', 'bar') } }
# threads << Thread.new { 100.times { cache.put('baz', 'qux') } }
# threads.each(&:join)
# expect(cache.send(:lru)).to eq(['baz', 'foo'])
# end
it 'is thread-safe' do
cache = PuppetForge::LruCache.new

# Create two threads for concurrent cache operations
thread_one = Thread.new do
100.times { cache.put('foo', 'bar') }
end

thread_two = Thread.new do
100.times { cache.put('baz', 'qux') }
end

# Wait for both threads to complete
thread_one.join
thread_two.join

# At this point, we don't need to compare the LRU list,
# because the order may change due to concurrent puts.

# Instead, we simply expect the code to run without errors.
expect { thread_one.value }.not_to raise_error
expect { thread_two.value }.not_to raise_error
end

end

context '#clear' do
Expand Down

0 comments on commit 7902337

Please sign in to comment.