Skip to content

Commit 45def53

Browse files
authored
Merge pull request #8797 from joshcooper/singleton_test_11236
(PUP-11236) Add ThreadLocalSingleton tests
2 parents bc8f433 + e3eac12 commit 45def53

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
require 'spec_helper'
2+
require 'puppet/concurrent/thread_local_singleton'
3+
4+
class PuppetSpec::Singleton
5+
extend Puppet::Concurrent::ThreadLocalSingleton
6+
end
7+
8+
# Use the `equal?` matcher to ensure we get the same object
9+
describe Puppet::Concurrent::ThreadLocalSingleton do
10+
it 'returns the same object for a single thread' do
11+
expect(PuppetSpec::Singleton.singleton).to equal(PuppetSpec::Singleton.singleton)
12+
end
13+
14+
it 'is not inherited for a newly created thread' do
15+
main_thread_local = PuppetSpec::Singleton.singleton
16+
Thread.new do
17+
expect(main_thread_local).to_not equal(PuppetSpec::Singleton.singleton)
18+
end.join
19+
end
20+
21+
it 'does not leak outside a thread' do
22+
thread_local = nil
23+
Thread.new do
24+
thread_local = PuppetSpec::Singleton.singleton
25+
end.join
26+
expect(thread_local).to_not equal(PuppetSpec::Singleton.singleton)
27+
end
28+
29+
it 'is different for each thread' do
30+
locals = []
31+
Thread.new do
32+
locals << PuppetSpec::Singleton.singleton
33+
end.join
34+
Thread.new do
35+
locals << PuppetSpec::Singleton.singleton
36+
end.join
37+
expect(locals.first).to_not equal(locals.last)
38+
end
39+
end

0 commit comments

Comments
 (0)