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

Commit be3dca3

Browse files
committed
Merge pull request #43 from ktdreyer/tests
tests: switch to minitest
2 parents d2c735b + cf1dc73 commit be3dca3

File tree

7 files changed

+64
-67
lines changed

7 files changed

+64
-67
lines changed

test/test_array.rb

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
1-
require 'test/unit'
21
require 'thread_safe'
2+
require File.join(File.dirname(__FILE__), "test_helper")
33

4-
class TestArray < Test::Unit::TestCase
4+
class TestArray < Minitest::Test
55
def test_concurrency
66
ary = ThreadSafe::Array.new
7-
assert_nothing_raised do
8-
(1..100).map do |i|
9-
Thread.new do
10-
1000.times do
11-
ary << i
12-
ary.each {|x| x * 2}
13-
ary.shift
14-
ary.last
15-
end
7+
(1..100).map do |i|
8+
Thread.new do
9+
1000.times do
10+
ary << i
11+
ary.each {|x| x * 2}
12+
ary.shift
13+
ary.last
1614
end
17-
end.map(&:join)
18-
end
15+
end
16+
end.map(&:join)
1917
end
2018
end

test/test_cache.rb

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,26 @@
1-
require 'test/unit'
21
require 'thread_safe'
32
require 'thread'
43
require File.join(File.dirname(__FILE__), "test_helper")
54

65
Thread.abort_on_exception = true
76

8-
class TestCache < Test::Unit::TestCase
7+
class TestCache < Minitest::Test
98
def setup
109
@cache = ThreadSafe::Cache.new
1110
end
1211

1312
def test_concurrency
1413
cache = @cache
15-
assert_nothing_raised do
16-
(1..100).map do |i|
17-
Thread.new do
18-
1000.times do |j|
19-
key = i*1000+j
20-
cache[key] = i
21-
cache[key]
22-
cache.delete(key)
23-
end
14+
(1..100).map do |i|
15+
Thread.new do
16+
1000.times do |j|
17+
key = i*1000+j
18+
cache[key] = i
19+
cache[key]
20+
cache.delete(key)
2421
end
25-
end.map(&:join)
26-
end
22+
end
23+
end.map(&:join)
2724
end
2825

2926
def test_retrieval
@@ -470,7 +467,7 @@ def test_fetch
470467
assert_equal(1, (@cache.fetch(:a) {flunk}))
471468
end
472469

473-
assert_raise(ThreadSafe::Cache::KEY_ERROR) do
470+
assert_raises(ThreadSafe::Cache::KEY_ERROR) do
474471
@cache.fetch(:b)
475472
end
476473
end
@@ -554,11 +551,9 @@ def test_each_pair_allows_modification
554551
@cache[:b] = 1
555552
@cache[:c] = 1
556553

557-
assert_nothing_raised do
558-
assert_size_change 1 do
559-
@cache.each_pair do |k, v|
560-
@cache[:z] = 1
561-
end
554+
assert_size_change 1 do
555+
@cache.each_pair do |k, v|
556+
@cache[:z] = 1
562557
end
563558
end
564559
end
@@ -704,22 +699,21 @@ def test_dup_clone
704699
end
705700

706701
def test_is_unfreezable
707-
assert_raise(NoMethodError) { @cache.freeze }
702+
assert_raises(NoMethodError) { @cache.freeze }
708703
end
709704

710705
def test_marshal_dump_load
711-
assert_nothing_raised do
712-
new_cache = Marshal.load(Marshal.dump(@cache))
713-
assert_equal 0, new_cache.size
714-
end
706+
new_cache = Marshal.load(Marshal.dump(@cache))
707+
assert_instance_of ThreadSafe::Cache, new_cache
708+
assert_equal 0, new_cache.size
715709
@cache[:a] = 1
716710
new_cache = Marshal.load(Marshal.dump(@cache))
717711
assert_equal 1, @cache[:a]
718712
assert_equal 1, new_cache.size
719713
end
720714

721715
def test_marshal_dump_doesnt_work_with_default_proc
722-
assert_raise(TypeError) do
716+
assert_raises(TypeError) do
723717
Marshal.dump(ThreadSafe::Cache.new {})
724718
end
725719
end
@@ -740,15 +734,16 @@ def assert_valid_option(option_name, value)
740734
end
741735

742736
def assert_valid_options(options)
743-
assert_nothing_raised { ThreadSafe::Cache.new(options) }
737+
c = ThreadSafe::Cache.new(options)
738+
assert_instance_of ThreadSafe::Cache, c
744739
end
745740

746741
def assert_invalid_option(option_name, value)
747742
assert_invalid_options(option_name => value)
748743
end
749744

750745
def assert_invalid_options(options)
751-
assert_raise(ArgumentError) { ThreadSafe::Cache.new(options) }
746+
assert_raises(ArgumentError) { ThreadSafe::Cache.new(options) }
752747
end
753748

754749
def assert_size_change(change, cache = @cache)
@@ -782,7 +777,7 @@ def assert_handles_exception(method, key, *args)
782777
before_had_value = before_had_key ? @cache[key] : nil
783778

784779
assert_no_size_change do
785-
assert_raise(TestException) do
780+
assert_raises(TestException) do
786781
@cache.send(method, key, *args) { raise TestException, '' }
787782
end
788783
assert_equal before_had_key, @cache.key?(key)

test/test_cache_loops.rb

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
require 'thread'
2-
require 'test/unit'
32
require 'thread_safe'
43
require File.join(File.dirname(__FILE__), "test_helper")
54

65
Thread.abort_on_exception = true
76

8-
class TestCacheTorture < Test::Unit::TestCase # this is not run unless RUBY_VERSION =~ /1\.8/ || ENV['TRAVIS'] (see the end of the file)
7+
class TestCacheTorture < Minitest::Test # this is not run unless RUBY_VERSION =~ /1\.8/ || ENV['TRAVIS'] (see the end of the file)
98
THREAD_COUNT = 40
109
KEY_COUNT = (((2**13) - 2) * 0.75).to_i # get close to the doubling cliff
1110
LOW_KEY_COUNT = (((2**8 ) - 2) * 0.75).to_i # get close to the doubling cliff
@@ -310,15 +309,13 @@ def add_remove_to_zero_via_merge_pair(opts = {})
310309
def do_thread_loop(name, code, options = {}, &block)
311310
options = DEFAULTS.merge(options)
312311
meth = define_loop name, code, options[:prelude]
313-
assert_nothing_raised do
314-
keys = to_keys_array(options[:key_count])
315-
run_thread_loop(meth, keys, options, &block)
316-
317-
if options[:key_count] > 1
318-
options[:key_count] = (options[:key_count] / 40).to_i
319-
keys = to_hash_collision_keys_array(options[:key_count])
320-
run_thread_loop(meth, keys, options.merge(:loop_count => (options[:loop_count] * 5)), &block)
321-
end
312+
keys = to_keys_array(options[:key_count])
313+
run_thread_loop(meth, keys, options, &block)
314+
315+
if options[:key_count] > 1
316+
options[:key_count] = (options[:key_count] / 40).to_i
317+
keys = to_hash_collision_keys_array(options[:key_count])
318+
run_thread_loop(meth, keys, options.merge(:loop_count => (options[:loop_count] * 5)), &block)
322319
end
323320
end
324321

test/test_hash.rb

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
1-
require 'test/unit'
21
require 'thread_safe'
2+
require File.join(File.dirname(__FILE__), "test_helper")
33

4-
class TestHash < Test::Unit::TestCase
4+
class TestHash < Minitest::Test
55
def test_concurrency
66
hsh = ThreadSafe::Hash.new
7-
assert_nothing_raised do
8-
(1..100).map do |i|
9-
Thread.new do
10-
1000.times do |j|
11-
hsh[i*1000+j] = i
12-
hsh[i*1000+j]
13-
hsh.delete(i*1000+j)
14-
end
7+
(1..100).map do |i|
8+
Thread.new do
9+
1000.times do |j|
10+
hsh[i*1000+j] = i
11+
hsh[i*1000+j]
12+
hsh.delete(i*1000+j)
1513
end
16-
end.map(&:join)
17-
end
14+
end
15+
end.map(&:join)
1816
end
1917
end

test/test_helper.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
11
require 'thread'
2+
require 'minitest/autorun'
3+
4+
if Minitest.const_defined?('Test')
5+
# We're on Minitest 5+. Nothing to do here.
6+
else
7+
# Minitest 4 doesn't have Minitest::Test yet.
8+
Minitest::Test = MiniTest::Unit::TestCase
9+
end
210

311
if defined?(JRUBY_VERSION) && ENV['TEST_NO_UNSAFE']
412
# to be used like this: rake test TEST_NO_UNSAFE=true
@@ -10,7 +18,7 @@
1018
manager.deny java.lang.RuntimePermission.new("accessClassInPackage.sun.misc")
1119
java.lang.System.setSecurityManager manager
1220

13-
class TestNoUnsafe < Test::Unit::TestCase
21+
class TestNoUnsafe < Minitest::Test
1422
def test_security_manager_is_used
1523
begin
1624
java_import 'sun.misc.Unsafe'

test/test_synchronized_delegator.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
require 'test/unit'
21
require 'thread_safe/synchronized_delegator.rb'
2+
require File.join(File.dirname(__FILE__), "test_helper")
33

4-
class TestSynchronizedDelegator < Test::Unit::TestCase
4+
class TestSynchronizedDelegator < Minitest::Test
55

66
def test_wraps_array
77
sync_array = SynchronizedDelegator.new(array = [])

thread_safe.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,5 @@ Gem::Specification.new do |gem|
2121

2222
gem.add_development_dependency 'atomic', ['>= 1.1.7', '< 2']
2323
gem.add_development_dependency 'rake'
24+
gem.add_development_dependency 'minitest'
2425
end

0 commit comments

Comments
 (0)