Skip to content

Commit d93abba

Browse files
committed
Created specs to test the various gem builds.
1 parent d28fc72 commit d93abba

File tree

6 files changed

+297
-0
lines changed

6 files changed

+297
-0
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
require 'benchmark'
2+
require_relative 'example_group_extensions'
3+
require_relative 'platform_helpers'
4+
5+
require 'concurrent/atomics'
6+
7+
def atomic_boolean_test(clazz, opts = {})
8+
threads = opts.fetch(:threads, 5)
9+
tests = opts.fetch(:tests, 100)
10+
11+
atomic = Concurrent.const_get(clazz.to_s).new
12+
latch = Concurrent::CountDownLatch.new(threads)
13+
14+
stats = Benchmark.measure do
15+
threads.times do |i|
16+
Thread.new do
17+
tests.times{ atomic.value = true }
18+
latch.count_down
19+
end
20+
end
21+
latch.wait
22+
end
23+
stats
24+
end
25+
26+
describe Concurrent::AtomicBoolean do
27+
28+
let!(:threads) { 10 }
29+
let!(:tests) { 1000 }
30+
31+
describe Concurrent::MutexAtomicBoolean do
32+
33+
specify 'is defined' do
34+
expect(defined?(Concurrent::MutexAtomicBoolean)).to be_truthy
35+
end
36+
37+
specify 'runs the benchmarks' do
38+
stats = atomic_boolean_test('MutexAtomicBoolean', threads: threads, tests: tests)
39+
expect(stats).to be_benchmark_results
40+
end
41+
end
42+
43+
if jruby?
44+
45+
describe Concurrent::JavaAtomicBoolean do
46+
47+
specify 'Concurrent::JavaAtomicBoolean is defined' do
48+
expect(defined?(Concurrent::JavaAtomicBoolean)).to be_truthy
49+
end
50+
51+
specify 'runs the benchmarks' do
52+
stats = atomic_boolean_test('JavaAtomicBoolean', threads: threads, tests: tests)
53+
expect(stats).to be_benchmark_results
54+
end
55+
end
56+
57+
else
58+
59+
specify 'Concurrent::JavaAtomicBoolean is not defined' do
60+
expect(defined?(Concurrent::JavaAtomicBoolean)).to be_falsey
61+
end
62+
end
63+
64+
if 'EXT' == ENV['TEST_PLATFORM']
65+
66+
describe Concurrent::CAtomicBoolean do
67+
68+
specify 'Concurrent::CAtomicBoolean is defined' do
69+
expect(defined?(Concurrent::CAtomicBoolean)).to be_truthy
70+
end
71+
72+
specify 'runs the benchmarks' do
73+
stats = atomic_boolean_test('CAtomicBoolean', threads: threads, tests: tests)
74+
expect(stats).to be_benchmark_results
75+
end
76+
end
77+
78+
else
79+
80+
specify 'Concurrent::CAtomicBoolean is not defined' do
81+
expect(defined?(Concurrent::CAtomicBoolean)).to be_falsey
82+
end
83+
end
84+
end
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
require 'benchmark'
2+
require_relative 'example_group_extensions'
3+
require_relative 'platform_helpers'
4+
5+
require 'concurrent/atomics'
6+
7+
def atomic_fixnum_test(clazz, opts = {})
8+
threads = opts.fetch(:threads, 5)
9+
tests = opts.fetch(:tests, 100)
10+
11+
atomic = Concurrent.const_get(clazz.to_s).new
12+
latch = Concurrent::CountDownLatch.new(threads)
13+
14+
stats = Benchmark.measure do
15+
threads.times do |i|
16+
Thread.new do
17+
tests.times{ atomic.up }
18+
latch.count_down
19+
end
20+
end
21+
latch.wait
22+
end
23+
stats
24+
end
25+
26+
describe Concurrent::AtomicFixnum do
27+
28+
let!(:threads) { 10 }
29+
let!(:tests) { 1000 }
30+
31+
describe Concurrent::MutexAtomicFixnum do
32+
33+
specify 'is defined' do
34+
expect(defined?(Concurrent::MutexAtomicFixnum)).to be_truthy
35+
end
36+
37+
specify 'runs the benchmarks' do
38+
stats = atomic_fixnum_test('MutexAtomicFixnum', threads: threads, tests: tests)
39+
expect(stats).to be_benchmark_results
40+
end
41+
end
42+
43+
if jruby?
44+
45+
describe Concurrent::JavaAtomicFixnum do
46+
47+
specify 'Concurrent::JavaAtomicFixnum is defined' do
48+
expect(defined?(Concurrent::JavaAtomicFixnum)).to be_truthy
49+
end
50+
51+
specify 'runs the benchmarks' do
52+
stats = atomic_fixnum_test('JavaAtomicFixnum', threads: threads, tests: tests)
53+
expect(stats).to be_benchmark_results
54+
end
55+
end
56+
57+
else
58+
59+
specify 'Concurrent::JavaAtomicFixnum is not defined' do
60+
expect(defined?(Concurrent::JavaAtomicFixnum)).to be_falsey
61+
end
62+
end
63+
64+
if 'EXT' == ENV['TEST_PLATFORM']
65+
66+
describe Concurrent::CAtomicFixnum do
67+
68+
specify 'Concurrent::CAtomicFixnum is defined' do
69+
expect(defined?(Concurrent::CAtomicFixnum)).to be_truthy
70+
end
71+
72+
specify 'runs the benchmarks' do
73+
stats = atomic_fixnum_test('CAtomicFixnum', threads: threads, tests: tests)
74+
expect(stats).to be_benchmark_results
75+
end
76+
end
77+
78+
else
79+
80+
specify 'Concurrent::CAtomicFixnum is not defined' do
81+
expect(defined?(Concurrent::CAtomicFixnum)).to be_falsey
82+
end
83+
end
84+
end
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
require 'benchmark'
2+
3+
RSpec::Matchers.define :be_benchmark_results do
4+
match do |actual|
5+
actual.is_a? Benchmark::Tms
6+
end
7+
end

build-tests/platform_helpers.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
require 'rbconfig'
2+
3+
def mri?(engine = RUBY_ENGINE)
4+
engine == 'ruby'
5+
end
6+
7+
def jruby?(engine = RUBY_ENGINE)
8+
engine == 'jruby'
9+
end
10+
11+
def rbx?(engine = RUBY_ENGINE)
12+
engine == 'rbx'
13+
end

build-tests/runner.rb

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
#!/usr/bin/env ruby
2+
3+
if File.exist?('Gemfile')
4+
puts <<-WARNING
5+
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
6+
!! A Gemfile has been detected. This will likely cause some tests !!
7+
!! to erroneously fail (RSpec + Bundler shenanigans!). You may need !!
8+
!! to run tests from a different directory. !!
9+
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
10+
WARNING
11+
end
12+
13+
$:.push File.join(File.dirname(__FILE__), '..', 'lib')
14+
15+
require 'concurrent/version'
16+
require_relative 'platform_helpers'
17+
18+
EXT_PLATFORMS = {
19+
'i686-linux' => 'x86-linux',
20+
'x86_64-linux' => 'x86_64-linux',
21+
'x86-mingw32' => 'x86-mingw32',
22+
'x64-mingw32' => 'x64-mingw32',
23+
'i386-solaris2.11' => 'x86-solaris-2.11',
24+
'x86_64-darwin14.0' => 'x86_64-darwin-14',
25+
}
26+
27+
TEST_PATH = File.dirname(__FILE__)
28+
PKG_PATH = File.join(File.dirname(__FILE__), '..', 'pkg')
29+
30+
RSPEC = "rspec --default-path #{TEST_PATH} -fd --color --seed 0"
31+
32+
UNINSTALL_GEMS_COMMAND = <<-CMD
33+
gem uninstall -q -a -I concurrent-ruby-ext
34+
gem uninstall -q -a -I concurrent-ruby
35+
gem uninstall -q -a -I ref
36+
CMD
37+
38+
SUITE_BREAK = "######################################################################\n"
39+
GEM_BREAK = "----------------------------------------------------------------------\n"
40+
41+
def platform_specific_extensions?(platform = RUBY_PLATFORM)
42+
EXT_PLATFORMS.keys.include?(platform) &&
43+
File.exists?("#{PKG_PATH}/#{extension_gem_name(platform)}")
44+
end
45+
46+
def extension_gem_name(platform = RUBY_PLATFORM)
47+
platform = EXT_PLATFORMS.fetch(platform, '')
48+
platform = '-' + platform unless platform.empty?
49+
"concurrent-ruby-ext-#{Concurrent::EXT_VERSION}#{platform}.gem"
50+
end
51+
52+
def install_gems_command(ext, platform = '')
53+
cmd = "gem install #{PKG_PATH}/concurrent-ruby-#{Concurrent::VERSION}.gem"
54+
if ext
55+
cmd << "\ngem install #{PKG_PATH}/#{extension_gem_name(platform)}"
56+
end
57+
cmd
58+
end
59+
60+
def install_java_gem_command
61+
"gem install #{PKG_PATH}/concurrent-ruby-#{Concurrent::VERSION}-java.gem"
62+
end
63+
64+
def run_tests_cmd(file, ext, platform = '')
65+
test_platform = if ext
66+
'EXT'
67+
elsif jruby?(platform)
68+
'JRUBY'
69+
else
70+
'RUBY'
71+
end
72+
73+
cmd = if jruby?(platform)
74+
install_java_gem_command
75+
else
76+
install_gems_command(ext, platform)
77+
end
78+
79+
cmd << "\n"
80+
cmd << "TEST_PLATFORM='#{test_platform}' #{RSPEC} #{file}"
81+
cmd << "\n"
82+
cmd << UNINSTALL_GEMS_COMMAND
83+
cmd
84+
end
85+
86+
TESTS = Dir["#{TEST_PATH}/*_spec.rb"]
87+
ok = system(UNINSTALL_GEMS_COMMAND)
88+
89+
TESTS.each do |file|
90+
puts SUITE_BREAK
91+
puts "Running #{file}"
92+
puts GEM_BREAK
93+
ok = system(run_tests_cmd(file, false))
94+
if jruby?
95+
puts GEM_BREAK
96+
ok = system(run_tests_cmd(file, false, 'jruby'))
97+
elsif mri?
98+
puts GEM_BREAK
99+
ok = system(run_tests_cmd(file, true))
100+
if platform_specific_extensions?(RUBY_PLATFORM)
101+
puts GEM_BREAK
102+
ok = system(run_tests_cmd(file, true, RUBY_PLATFORM))
103+
end
104+
end
105+
end

spec/spec_helper.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,15 @@
88

99
SimpleCov.start do
1010
project_name 'concurrent-ruby'
11+
add_filter '/build-tests/'
1112
add_filter '/coverage/'
1213
add_filter '/doc/'
14+
add_filter '/examples/'
1315
add_filter '/pkg/'
1416
add_filter '/spec/'
1517
add_filter '/tasks/'
18+
add_filter '/yard-template/'
19+
add_filter '/yardoc/'
1620
end
1721

1822
require 'concurrent'

0 commit comments

Comments
 (0)