Skip to content
This repository was archived by the owner on Mar 15, 2022. It is now read-only.

Commit b0cd464

Browse files
committed
Merge pull request #15 from dzjuck/rspec_tests
Rewrite tests from minitest to rspec
2 parents b2eb53d + e0b661b commit b0cd464

25 files changed

+1711
-1625
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Gemfile.lock
2-
test/*.jar
2+
spec/*.jar
33
.rspec-local
44
*.gem
55
lib/1.8

.rspec

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
--require spec_helper
2+
--format progress

.travis.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,4 @@ matrix:
4040
- rvm: jruby-head
4141
- rvm: 1.9.3
4242

43-
script: "rake TESTOPTS='--seed=1'"
43+
script: "rake test"

Gemfile

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ source 'https://rubygems.org'
33
gemspec
44

55
group :development, :test do
6-
gem 'minitest', '~> 5.5.1'
7-
gem 'minitest-reporters', '~> 1.0.11'
6+
gem 'rspec', '~> 3.2.0'
87
gem 'simplecov', '~> 0.9.2', :require => false
98
gem 'coveralls', '~> 0.7.11', :require => false
109
end

Rakefile

+10-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
require "bundler/gem_tasks"
2-
require "rake/testtask"
1+
require 'bundler/gem_tasks'
2+
require 'rspec'
3+
require 'rspec/core/rake_task'
34

45
## safely load all the rake tasks in the `tasks` directory
56
def safe_load(file)
@@ -10,16 +11,17 @@ def safe_load(file)
1011
puts ex.message
1112
end
1213
end
14+
1315
Dir.glob('tasks/**/*.rake').each do |rakefile|
1416
safe_load rakefile
1517
end
1618

1719
task :default => :test
1820

1921
if defined?(JRUBY_VERSION)
20-
require "ant"
22+
require 'ant'
2123

22-
directory "pkg/classes"
24+
directory 'pkg/classes'
2325
directory 'pkg/tests'
2426

2527
desc "Clean up build artifacts"
@@ -43,10 +45,10 @@ if defined?(JRUBY_VERSION)
4345

4446
desc "Build test jar"
4547
task 'test-jar' => 'pkg/tests' do |t|
46-
ant.javac :srcdir => 'test/src', :destdir => t.prerequisites.first,
48+
ant.javac :srcdir => 'spec/src', :destdir => t.prerequisites.first,
4749
:source => "1.5", :target => "1.5", :debug => true
4850

49-
ant.jar :basedir => 'pkg/tests', :destfile => 'test/package.jar', :includes => '**/*.class'
51+
ant.jar :basedir => 'pkg/tests', :destfile => 'spec/package.jar', :includes => '**/*.class'
5052
end
5153

5254
task :package => [ :clean, :compile, :jar, 'test-jar' ]
@@ -55,7 +57,6 @@ else
5557
task :package
5658
end
5759

58-
Rake::TestTask.new :test => :package do |t|
59-
t.libs << "lib"
60-
t.test_files = FileList["test/**/*.rb"]
60+
RSpec::Core::RakeTask.new :test => :package do |t|
61+
t.rspec_opts = '--color --backtrace --tag ~unfinished --seed 1 --format documentation ./spec'
6162
end

spec/.gitignore

Whitespace-only changes.

spec/spec_helper.rb

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
require 'simplecov'
2+
require 'coveralls'
3+
require 'logger'
4+
5+
SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
6+
SimpleCov::Formatter::HTMLFormatter,
7+
Coveralls::SimpleCov::Formatter
8+
]
9+
10+
SimpleCov.start do
11+
project_name 'thread_safe'
12+
add_filter '/coverage/'
13+
add_filter '/pkg/'
14+
add_filter '/spec/'
15+
add_filter '/tasks/'
16+
add_filter '/yard-template/'
17+
end
18+
19+
$VERBOSE = nil # suppress our deprecation warnings
20+
require 'thread_safe'
21+
22+
logger = Logger.new($stderr)
23+
logger.level = Logger::WARN
24+
25+
# import all the support files
26+
Dir[File.join(File.dirname(__FILE__), 'support/**/*.rb')].each { |f| require File.expand_path(f) }
27+
28+
RSpec.configure do |config|
29+
#config.raise_errors_for_deprecations!
30+
config.order = 'random'
31+
end

spec/support/.gitignore

Whitespace-only changes.

spec/support/threads.rb

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
THREADS = (RUBY_ENGINE == 'ruby' ? 100 : 10)

spec/support/threadsafe_test.rb

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
module ThreadSafe
2+
module Test
3+
class Latch
4+
def initialize(count = 1)
5+
@count = count
6+
@mutex = Mutex.new
7+
@cond = ConditionVariable.new
8+
end
9+
10+
def release
11+
@mutex.synchronize do
12+
@count -= 1 if @count > 0
13+
@cond.broadcast if @count.zero?
14+
end
15+
end
16+
17+
def await
18+
@mutex.synchronize do
19+
@cond.wait @mutex if @count > 0
20+
end
21+
end
22+
end
23+
24+
class Barrier < Latch
25+
def await
26+
@mutex.synchronize do
27+
if @count.zero? # fall through
28+
elsif @count > 0
29+
@count -= 1
30+
@count.zero? ? @cond.broadcast : @cond.wait(@mutex)
31+
end
32+
end
33+
end
34+
end
35+
36+
class HashCollisionKey
37+
attr_reader :hash, :key
38+
def initialize(key, hash = key.hash % 3)
39+
@key = key
40+
@hash = hash
41+
end
42+
43+
def eql?(other)
44+
other.kind_of?(self.class) && @key.eql?(other.key)
45+
end
46+
47+
def even?
48+
@key.even?
49+
end
50+
51+
def <=>(other)
52+
@key <=> other.key
53+
end
54+
end
55+
56+
# having 4 separate HCK classes helps for a more thorough CHMV8 testing
57+
class HashCollisionKey2 < HashCollisionKey; end
58+
class HashCollisionKeyNoCompare < HashCollisionKey
59+
def <=>(other)
60+
0
61+
end
62+
end
63+
class HashCollisionKey4 < HashCollisionKeyNoCompare; end
64+
65+
HASH_COLLISION_CLASSES = [HashCollisionKey, HashCollisionKey2, HashCollisionKeyNoCompare, HashCollisionKey4]
66+
67+
def self.HashCollisionKey(key, hash = key.hash % 3)
68+
HASH_COLLISION_CLASSES[rand(4)].new(key, hash)
69+
end
70+
71+
class HashCollisionKeyNonComparable < HashCollisionKey
72+
undef <=>
73+
end
74+
end
75+
end

spec/thread_safe/.gitignore

Whitespace-only changes.

spec/thread_safe/array_spec.rb

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
module ThreadSafe
2+
describe Array do
3+
let!(:ary) { described_class.new }
4+
5+
it 'concurrency' do
6+
(1..THREADS).map do |i|
7+
Thread.new do
8+
1000.times do
9+
ary << i
10+
ary.each { |x| x * 2 }
11+
ary.shift
12+
ary.last
13+
end
14+
end
15+
end.map(&:join)
16+
end
17+
end
18+
end

0 commit comments

Comments
 (0)