From be23243ba28f11605734a54ddea94c94a1e9fd7d Mon Sep 17 00:00:00 2001 From: Matthew McEachen Date: Sun, 27 Jan 2013 19:19:42 -0800 Subject: [PATCH] use skip properly --- test/mysql_nesting_test.rb | 4 +- test/parallelism_test.rb | 150 +++++++++++++++++++------------------ tests.sh | 17 ++--- 3 files changed, 84 insertions(+), 87 deletions(-) diff --git a/test/mysql_nesting_test.rb b/test/mysql_nesting_test.rb index f1c6116..c9ee1dc 100644 --- a/test/mysql_nesting_test.rb +++ b/test/mysql_nesting_test.rb @@ -2,10 +2,12 @@ describe "lock nesting" do it "warns about MySQL releasing advisory locks" do + skip if env_db != 'mysql' + Tag.expects(:wal_log) Tag.with_advisory_lock("first") do Tag.with_advisory_lock("second") do end end end -end if env_db == 'mysql' +end diff --git a/test/parallelism_test.rb b/test/parallelism_test.rb index 5cc681f..fb1c784 100644 --- a/test/parallelism_test.rb +++ b/test/parallelism_test.rb @@ -1,95 +1,97 @@ require 'minitest_helper' -if env_db != 'sqlite3' - describe "parallelism" do - def find_or_create_at_even_second(run_at, with_advisory_lock) - sleep(run_at - Time.now.to_f) - ActiveRecord::Base.connection.reconnect! - name = run_at.to_s - task = lambda do - Tag.transaction do - Tag.find_by_name(name) || Tag.create!(:name => name) - end - end - if with_advisory_lock - Tag.with_advisory_lock(name, nil, &task) - else - task.call +describe "parallelism" do + def find_or_create_at_even_second(run_at, with_advisory_lock) + ActiveRecord::Base.connection.reconnect! + sleep_time = run_at - Time.now.to_f + $stderr.puts "sleeping for #{sleep_time} for #{run_at}" + sleep(sleep_time) + name = run_at.to_s + task = lambda do + Tag.transaction do + Tag.find_by_name(name) || Tag.create(:name => name) end + $stderr.puts "finished with #{run_at}" end + if with_advisory_lock + Tag.with_advisory_lock(name, nil, &task) + else + task.call + end + end - def run_workers(with_advisory_lock) - start_time = Time.now.to_i + 2 - threads = @workers.times.collect do - Thread.new do - @iterations.times do |ea| - find_or_create_at_even_second(start_time + (ea * 2), with_advisory_lock) - end + def run_workers(with_advisory_lock) + skip if env_db == "sqlite" + start_time = Time.now.to_i + 2 + threads = @workers.times.collect do + Thread.new do + @iterations.times do |ea| + find_or_create_at_even_second(start_time + ea, with_advisory_lock) end end - threads.each { |ea| ea.join } - puts "Created #{Tag.all.size} (lock = #{with_advisory_lock})" end + threads.each { |ea| ea.join } + puts "Created #{Tag.all.size} (lock = #{with_advisory_lock})" + end - before :each do - @iterations = 5 - @workers = 5 - end + before :each do + @iterations = 5 + @workers = 10 + end - it "parallel threads create multiple duplicate rows" do - run_workers(with_advisory_lock = false) - if Tag.connection.adapter_name == "SQLite" && RUBY_VERSION == "1.9.3" - oper = :== # sqlite doesn't run in parallel. - else - oper = :> # Everything else should create duplicate rows. - end - Tag.all.size.must_be oper, @iterations # <- any duplicated rows will make me happy. - TagAudit.all.size.must_be oper, @iterations # <- any duplicated rows will make me happy. - Label.all.size.must_be oper, @iterations # <- any duplicated rows will make me happy. + it "parallel threads create multiple duplicate rows" do + run_workers(with_advisory_lock = false) + if Tag.connection.adapter_name == "SQLite" && RUBY_VERSION == "1.9.3" + oper = :== # sqlite doesn't run in parallel. + else + oper = :> # Everything else should create duplicate rows. end + Tag.all.size.must_be oper, @iterations # <- any duplicated rows will make me happy. + TagAudit.all.size.must_be oper, @iterations # <- any duplicated rows will make me happy. + Label.all.size.must_be oper, @iterations # <- any duplicated rows will make me happy. + end - it "parallel threads with_advisory_lock don't create multiple duplicate rows" do - run_workers(with_advisory_lock = true) - Tag.all.size.must_equal @iterations # <- any duplicated rows will NOT make me happy. - TagAudit.all.size.must_equal @iterations # <- any duplicated rows will NOT make me happy. - Label.all.size.must_equal @iterations # <- any duplicated rows will NOT make me happy. - end + it "parallel threads with_advisory_lock don't create multiple duplicate rows" do + run_workers(with_advisory_lock = true) + Tag.all.size.must_equal @iterations # <- any duplicated rows will NOT make me happy. + TagAudit.all.size.must_equal @iterations # <- any duplicated rows will NOT make me happy. + Label.all.size.must_equal @iterations # <- any duplicated rows will NOT make me happy. + end - it "returns false if the lock wasn't acquirable" do - t1_acquired_lock = false - t1_return_value = nil - t1 = Thread.new do - ActiveRecord::Base.connection.reconnect! - t1_return_value = Label.with_advisory_lock("testing 1,2,3") do - t1_acquired_lock = true - sleep(0.3) - "boom" - end + it "returns false if the lock wasn't acquirable" do + t1_acquired_lock = false + t1_return_value = nil + t1 = Thread.new do + ActiveRecord::Base.connection.reconnect! + t1_return_value = Label.with_advisory_lock("testing 1,2,3") do + t1_acquired_lock = true + sleep(0.3) + "boom" end + end - # Make sure the lock is acquired: - sleep(0.1) + # Make sure the lock is acquired: + sleep(0.1) - # Now try to acquire the lock impatiently: - t2_acquired_lock = false - t2_return_value = nil - t2 = Thread.new do - ActiveRecord::Base.connection.reconnect! - t2_return_value = Label.with_advisory_lock("testing 1,2,3", 0.1) do - t2_acquired_lock = true - "not expected" - end + # Now try to acquire the lock impatiently: + t2_acquired_lock = false + t2_return_value = nil + t2 = Thread.new do + ActiveRecord::Base.connection.reconnect! + t2_return_value = Label.with_advisory_lock("testing 1,2,3", 0.1) do + t2_acquired_lock = true + "not expected" end + end - # Wait for them to finish: - t1.join - t2.join + # Wait for them to finish: + t1.join + t2.join - t1_acquired_lock.must_be_true - t1_return_value.must_equal "boom" + t1_acquired_lock.must_be_true + t1_return_value.must_equal "boom" - t2_acquired_lock.must_be_false - t2_return_value.must_be_false - end + t2_acquired_lock.must_be_false + t2_return_value.must_be_false end -end \ No newline at end of file +end diff --git a/tests.sh b/tests.sh index 74be12b..af55701 100755 --- a/tests.sh +++ b/tests.sh @@ -1,20 +1,13 @@ -#!/bin/sh -ex +#!/bin/sh -e export BUNDLE_GEMFILE RMI DB for RMI in 1.8.7-p370 1.9.3-p327 do rbenv local $RMI - (gem list | grep bundler) || gem install bundler - (gem list | grep rake) || gem install rake - rbenv rehash || true - - for BUNDLE_GEMFILE in ci/Gemfile.activerecord-3.0.x ci/Gemfile.activerecord-3.1.x ci/Gemfile.activerecord-3.2.x + bundle --quiet + for DB in sqlite mysql postgresql do - bundle --quiet - for DB in sqlite mysql postgresql - do - echo $DB $BUNDLE_GEMFILE `ruby -v` - bundle exec rake - done + echo $DB $BUNDLE_GEMFILE `ruby -v` + bundle exec rake done done \ No newline at end of file