forked from salsify/with_advisory_lock
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
84 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |