Blob Blame History Raw
From 3ed52e0625076f19b2876bad051d18309b5264b4 Mon Sep 17 00:00:00 2001
From: Ken Dreyer <ktdreyer@ktdreyer.com>
Date: Fri, 4 Apr 2014 12:40:39 -0500
Subject: [PATCH] tests: switch to minitest

Ruby 1.9+ uses Minitest as the backend for Test::Unit. As of Minitest 5,
the shim no longer supports Test::Unit::TestCase.

Adjust the thread_safe test suite to support Minitest 5's syntax.

Minitest versions 4 and below do not support the newer Minitest::Test
class that arrived in version 5. For that case, use the
MiniTest::Unit::TestCase class as a fallback.

Also add a backwards-compatible function to Minitest::Test for
assert_nothing_raised, since Minitest doesn't support that assertion.
Eventually the use of assert_nothing_raised in thread_safe ought to be
removed in favor of something better (such as more specific asserts).
---
 test/test_array.rb                  |  4 ++--
 test/test_cache.rb                  | 13 ++++++-------
 test/test_cache_loops.rb            |  3 +--
 test/test_hash.rb                   |  4 ++--
 test/test_helper.rb                 | 20 +++++++++++++++++++-
 test/test_synchronized_delegator.rb |  4 ++--
 thread_safe.gemspec                 |  1 +
 7 files changed, 33 insertions(+), 16 deletions(-)

diff --git a/test/test_array.rb b/test/test_array.rb
index 97d7427..89f8910 100644
--- a/test/test_array.rb
+++ b/test/test_array.rb
@@ -1,7 +1,7 @@
-require 'test/unit'
 require 'thread_safe'
+require File.join(File.dirname(__FILE__), "test_helper")
 
-class TestArray < Test::Unit::TestCase
+class TestArray < Minitest::Test
   def test_concurrency
     ary = ThreadSafe::Array.new
     assert_nothing_raised do
diff --git a/test/test_cache.rb b/test/test_cache.rb
index f450369..d8157a5 100644
--- a/test/test_cache.rb
+++ b/test/test_cache.rb
@@ -1,11 +1,10 @@
-require 'test/unit'
 require 'thread_safe'
 require 'thread'
 require File.join(File.dirname(__FILE__), "test_helper")
 
 Thread.abort_on_exception = true
 
-class TestCache < Test::Unit::TestCase
+class TestCache < Minitest::Test
   def setup
     @cache = ThreadSafe::Cache.new
   end
@@ -470,7 +469,7 @@ def test_fetch
         assert_equal(1, (@cache.fetch(:a) {flunk}))
       end
 
-      assert_raise(ThreadSafe::Cache::KEY_ERROR) do
+      assert_raises(ThreadSafe::Cache::KEY_ERROR) do
         @cache.fetch(:b)
       end
     end
@@ -704,7 +703,7 @@ def test_dup_clone
   end
 
   def test_is_unfreezable
-    assert_raise(NoMethodError) { @cache.freeze }
+    assert_raises(NoMethodError) { @cache.freeze }
   end
 
   def test_marshal_dump_load
@@ -719,7 +718,7 @@ def test_marshal_dump_load
   end
 
   def test_marshal_dump_doesnt_work_with_default_proc
-    assert_raise(TypeError) do
+    assert_raises(TypeError) do
       Marshal.dump(ThreadSafe::Cache.new {})
     end
   end
@@ -748,7 +747,7 @@ def assert_invalid_option(option_name, value)
   end
 
   def assert_invalid_options(options)
-    assert_raise(ArgumentError) { ThreadSafe::Cache.new(options) }
+    assert_raises(ArgumentError) { ThreadSafe::Cache.new(options) }
   end
 
   def assert_size_change(change, cache = @cache)
@@ -782,7 +781,7 @@ def assert_handles_exception(method, key, *args)
     before_had_value = before_had_key ? @cache[key] : nil
 
     assert_no_size_change do
-      assert_raise(TestException) do
+      assert_raises(TestException) do
         @cache.send(method, key, *args) { raise TestException, '' }
       end
       assert_equal before_had_key,   @cache.key?(key)
diff --git a/test/test_cache_loops.rb b/test/test_cache_loops.rb
index 7ef5b38..4882e41 100644
--- a/test/test_cache_loops.rb
+++ b/test/test_cache_loops.rb
@@ -1,11 +1,10 @@
 require 'thread'
-require 'test/unit'
 require 'thread_safe'
 require File.join(File.dirname(__FILE__), "test_helper")
 
 Thread.abort_on_exception = true
 
-class TestCacheTorture < Test::Unit::TestCase # this is not run unless RUBY_VERSION =~ /1\.8/ || ENV['TRAVIS'] (see the end of the file)
+class TestCacheTorture < Minitest::Test # this is not run unless RUBY_VERSION =~ /1\.8/ || ENV['TRAVIS'] (see the end of the file)
   THREAD_COUNT  = 40
   KEY_COUNT     = (((2**13) - 2) * 0.75).to_i # get close to the doubling cliff
   LOW_KEY_COUNT = (((2**8 ) - 2) * 0.75).to_i # get close to the doubling cliff
diff --git a/test/test_hash.rb b/test/test_hash.rb
index 137ec82..d1725c1 100644
--- a/test/test_hash.rb
+++ b/test/test_hash.rb
@@ -1,7 +1,7 @@
-require 'test/unit'
 require 'thread_safe'
+require File.join(File.dirname(__FILE__), "test_helper")
 
-class TestHash < Test::Unit::TestCase
+class TestHash < Minitest::Test
   def test_concurrency
     hsh = ThreadSafe::Hash.new
     assert_nothing_raised do
diff --git a/test/test_helper.rb b/test/test_helper.rb
index 6c41651..47aecbb 100644
--- a/test/test_helper.rb
+++ b/test/test_helper.rb
@@ -1,4 +1,22 @@
 require 'thread'
+require 'minitest/autorun'
+
+if Minitest.const_defined?('Test')
+  # We're on Minitest 5+. Nothing to do here.
+else
+  # Minitest 4 doesn't have Minitest::Test yet.
+  Minitest::Test = MiniTest::Unit::TestCase
+end
+
+# Minitest does not support assert_nothing_raised.
+# Eventually we should remove our use of this assert_nothing_raised function
+# for more specific asserts. In the mean time, add a backwards-compatible
+# implementation of assert_nothing_raised.
+class MiniTest::Test
+  def assert_nothing_raised(&block)
+    block.call
+  end
+end
 
 if defined?(JRUBY_VERSION) && ENV['TEST_NO_UNSAFE']
   # to be used like this: rake test TEST_NO_UNSAFE=true
@@ -10,7 +28,7 @@
   manager.deny java.lang.RuntimePermission.new("accessClassInPackage.sun.misc")
   java.lang.System.setSecurityManager manager
 
-  class TestNoUnsafe < Test::Unit::TestCase
+  class TestNoUnsafe < Minitest::Test
     def test_security_manager_is_used
       begin
         java_import 'sun.misc.Unsafe'
diff --git a/test/test_synchronized_delegator.rb b/test/test_synchronized_delegator.rb
index 4d3ba16..40c9438 100644
--- a/test/test_synchronized_delegator.rb
+++ b/test/test_synchronized_delegator.rb
@@ -1,7 +1,7 @@
-require 'test/unit'
 require 'thread_safe/synchronized_delegator.rb'
+require File.join(File.dirname(__FILE__), "test_helper")
 
-class TestSynchronizedDelegator < Test::Unit::TestCase
+class TestSynchronizedDelegator < Minitest::Test
 
   def test_wraps_array
     sync_array = SynchronizedDelegator.new(array = [])

-- 
1.9.1

From cf1dc73e925acc5e717322af9982ce41bad7aad6 Mon Sep 17 00:00:00 2001
From: Ken Dreyer <ktdreyer@ktdreyer.com>
Date: Mon, 7 Apr 2014 11:04:33 -0500
Subject: [PATCH] tests: remove assert_nothing_raised

Minitest doesn't support assert_nothing_raised. Remove our uses of
assert_nothing_raised by either simply removing the call, or adding
other basic asserts.
---
 test/test_array.rb       | 20 +++++++++-----------
 test/test_cache.rb       | 38 +++++++++++++++++---------------------
 test/test_cache_loops.rb | 16 +++++++---------
 test/test_hash.rb        | 18 ++++++++----------
 test/test_helper.rb      | 10 ----------
 5 files changed, 41 insertions(+), 61 deletions(-)

diff --git a/test/test_array.rb b/test/test_array.rb
index 89f8910..7a6023a 100644
--- a/test/test_array.rb
+++ b/test/test_array.rb
@@ -4,17 +4,15 @@
 class TestArray < Minitest::Test
   def test_concurrency
     ary = ThreadSafe::Array.new
-    assert_nothing_raised do
-      (1..100).map do |i|
-        Thread.new do
-          1000.times do
-            ary << i
-            ary.each {|x| x * 2}
-            ary.shift
-            ary.last
-          end
+    (1..100).map do |i|
+      Thread.new do
+        1000.times do
+          ary << i
+          ary.each {|x| x * 2}
+          ary.shift
+          ary.last
         end
-      end.map(&:join)
-    end
+      end
+    end.map(&:join)
   end
 end
diff --git a/test/test_cache.rb b/test/test_cache.rb
index d8157a5..0a61ba3 100644
--- a/test/test_cache.rb
+++ b/test/test_cache.rb
@@ -11,18 +11,16 @@ def setup
 
   def test_concurrency
     cache = @cache
-    assert_nothing_raised do
-      (1..100).map do |i|
-        Thread.new do
-          1000.times do |j|
-            key = i*1000+j
-            cache[key] = i
-            cache[key]
-            cache.delete(key)
-          end
+    (1..100).map do |i|
+      Thread.new do
+        1000.times do |j|
+          key = i*1000+j
+          cache[key] = i
+          cache[key]
+          cache.delete(key)
         end
-      end.map(&:join)
-    end
+      end
+    end.map(&:join)
   end
 
   def test_retrieval
@@ -553,11 +551,9 @@ def test_each_pair_allows_modification
     @cache[:b] = 1
     @cache[:c] = 1
 
-    assert_nothing_raised do
-      assert_size_change 1 do
-        @cache.each_pair do |k, v|
-          @cache[:z] = 1
-        end
+    assert_size_change 1 do
+      @cache.each_pair do |k, v|
+        @cache[:z] = 1
       end
     end
   end
@@ -707,10 +703,9 @@ def test_is_unfreezable
   end
 
   def test_marshal_dump_load
-    assert_nothing_raised do
-      new_cache = Marshal.load(Marshal.dump(@cache))
-      assert_equal 0, new_cache.size
-    end
+    new_cache = Marshal.load(Marshal.dump(@cache))
+    assert_instance_of ThreadSafe::Cache, new_cache
+    assert_equal 0, new_cache.size
     @cache[:a] = 1
     new_cache = Marshal.load(Marshal.dump(@cache))
     assert_equal 1, @cache[:a]
@@ -739,7 +734,8 @@ def assert_valid_option(option_name, value)
   end
 
   def assert_valid_options(options)
-    assert_nothing_raised { ThreadSafe::Cache.new(options) }
+    c = ThreadSafe::Cache.new(options)
+    assert_instance_of ThreadSafe::Cache, c
   end
 
   def assert_invalid_option(option_name, value)
diff --git a/test/test_cache_loops.rb b/test/test_cache_loops.rb
index 4882e41..e3a92ed 100644
--- a/test/test_cache_loops.rb
+++ b/test/test_cache_loops.rb
@@ -309,15 +309,13 @@ def add_remove_to_zero_via_merge_pair(opts = {})
   def do_thread_loop(name, code, options = {}, &block)
     options = DEFAULTS.merge(options)
     meth    = define_loop name, code, options[:prelude]
-    assert_nothing_raised do
-      keys = to_keys_array(options[:key_count])
-      run_thread_loop(meth, keys, options, &block)
-
-      if options[:key_count] > 1
-        options[:key_count] = (options[:key_count] / 40).to_i
-        keys = to_hash_collision_keys_array(options[:key_count])
-        run_thread_loop(meth, keys, options.merge(:loop_count => (options[:loop_count] * 5)), &block)
-      end
+    keys = to_keys_array(options[:key_count])
+    run_thread_loop(meth, keys, options, &block)
+
+    if options[:key_count] > 1
+      options[:key_count] = (options[:key_count] / 40).to_i
+      keys = to_hash_collision_keys_array(options[:key_count])
+      run_thread_loop(meth, keys, options.merge(:loop_count => (options[:loop_count] * 5)), &block)
     end
   end
 
diff --git a/test/test_hash.rb b/test/test_hash.rb
index d1725c1..12f4975 100644
--- a/test/test_hash.rb
+++ b/test/test_hash.rb
@@ -4,16 +4,14 @@
 class TestHash < Minitest::Test
   def test_concurrency
     hsh = ThreadSafe::Hash.new
-    assert_nothing_raised do
-      (1..100).map do |i|
-        Thread.new do
-          1000.times do |j|
-            hsh[i*1000+j] = i
-            hsh[i*1000+j]
-            hsh.delete(i*1000+j)
-          end
+    (1..100).map do |i|
+      Thread.new do
+        1000.times do |j|
+          hsh[i*1000+j] = i
+          hsh[i*1000+j]
+          hsh.delete(i*1000+j)
         end
-      end.map(&:join)
-    end
+      end
+    end.map(&:join)
   end
 end
diff --git a/test/test_helper.rb b/test/test_helper.rb
index 47aecbb..b5c84f8 100644
--- a/test/test_helper.rb
+++ b/test/test_helper.rb
@@ -8,16 +8,6 @@
   Minitest::Test = MiniTest::Unit::TestCase
 end
 
-# Minitest does not support assert_nothing_raised.
-# Eventually we should remove our use of this assert_nothing_raised function
-# for more specific asserts. In the mean time, add a backwards-compatible
-# implementation of assert_nothing_raised.
-class MiniTest::Test
-  def assert_nothing_raised(&block)
-    block.call
-  end
-end
-
 if defined?(JRUBY_VERSION) && ENV['TEST_NO_UNSAFE']
   # to be used like this: rake test TEST_NO_UNSAFE=true
   load 'test/package.jar'
-- 
1.9.1