-
Notifications
You must be signed in to change notification settings - Fork 15
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
Charlie Savage
committed
Jun 21, 2017
1 parent
0ad0e54
commit 0396b25
Showing
12 changed files
with
289 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
class FailJob < ActiveJob::Base | ||
include ActiveJob::Locking::Unique | ||
|
||
self.lock_acquire_time = 2 | ||
|
||
# We want the job ids to be all the same for testing | ||
def lock_key | ||
self.class.name | ||
end | ||
|
||
# Pass in index so we can distinguish different jobs | ||
def perform(index, sleep_time) | ||
raise(ArgumentError, 'Job failed') | ||
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 |
---|---|---|
@@ -0,0 +1,63 @@ | ||
require File.expand_path('../test_helper', __FILE__) | ||
|
||
module SerializedTests | ||
def test_one_completed | ||
assert_equal(0, ActiveJob::Base.queue_adapter.enqueued_jobs.count) | ||
assert_equal(0, ActiveJob::Base.queue_adapter.performed_jobs.count) | ||
|
||
start_time = Time.now | ||
sleep_time = SerialJob.lock_acquire_time / 0.9 | ||
threads = 3.times.map do |i| | ||
Thread.new do | ||
SerialJob.perform_later(i, sleep_time) | ||
end | ||
end | ||
|
||
# All the threads will complete after the sleep time has expired - since two jobs get requeued | ||
threads.each {|thread| thread.join} | ||
assert_equal(2, ActiveJob::Base.queue_adapter.enqueued_jobs.count) | ||
assert_equal(3, ActiveJob::Base.queue_adapter.performed_jobs.count) | ||
|
||
assert(Time.now - start_time > (1 * sleep_time)) | ||
end | ||
|
||
def test_some_completed | ||
assert_equal(0, ActiveJob::Base.queue_adapter.enqueued_jobs.count) | ||
assert_equal(0, ActiveJob::Base.queue_adapter.performed_jobs.count) | ||
|
||
start_time = Time.now | ||
sleep_time = SerialJob.lock_acquire_time / 1.9 | ||
threads = 3.times.map do |i| | ||
Thread.new do | ||
SerialJob.perform_later(i, sleep_time) | ||
end | ||
end | ||
|
||
# All the threads will complete after the sleep time has expired - since two jobs get requeued | ||
threads.each {|thread| thread.join} | ||
assert_equal(1, ActiveJob::Base.queue_adapter.enqueued_jobs.count) | ||
assert_equal(3, ActiveJob::Base.queue_adapter.performed_jobs.count) | ||
|
||
assert(Time.now - start_time > (1 * sleep_time)) | ||
end | ||
|
||
def test_all_completed | ||
assert_equal(0, ActiveJob::Base.queue_adapter.enqueued_jobs.count) | ||
assert_equal(0, ActiveJob::Base.queue_adapter.performed_jobs.count) | ||
|
||
start_time = Time.now | ||
sleep_time = SerialJob.lock_acquire_time / 4 | ||
threads = 3.times.map do |i| | ||
Thread.new do | ||
SerialJob.perform_later(i, sleep_time) | ||
end | ||
end | ||
|
||
# All the threads will complete after the sleep time has expired - since two jobs get requeued | ||
threads.each {|thread| thread.join} | ||
assert_equal(0, ActiveJob::Base.queue_adapter.enqueued_jobs.count) | ||
assert_equal(3, ActiveJob::Base.queue_adapter.performed_jobs.count) | ||
|
||
assert(Time.now - start_time > (1 * sleep_time)) | ||
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
require_relative('./serialized_tests') | ||
|
||
class SerializedMemory < MiniTest::Test | ||
include SerializedTests | ||
|
||
def setup | ||
ActiveJob::Base.queue_adapter = :test | ||
ActiveJob::Base.queue_adapter.perform_enqueued_jobs = true | ||
ActiveJob::Locking.options.adapter = ActiveJob::Locking::Adapters::Memory | ||
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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
require_relative('./serialized_tests') | ||
|
||
class UniqueRedisSemaphoreTest < MiniTest::Test | ||
include SerializedTests | ||
|
||
def setup | ||
redis_reset | ||
ActiveJob::Base.queue_adapter = :test | ||
ActiveJob::Base.queue_adapter.perform_enqueued_jobs = true | ||
ActiveJob::Locking.options.adapter = ActiveJob::Locking::Adapters::RedisSemaphore | ||
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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
require_relative('./serialized_tests') | ||
|
||
class SerializedRedlockTest < MiniTest::Test | ||
include SerializedTests | ||
|
||
def setup | ||
redis_reset | ||
|
||
ActiveJob::Base.queue_adapter = :test | ||
ActiveJob::Base.queue_adapter.perform_enqueued_jobs = true | ||
ActiveJob::Locking.options.adapter = ActiveJob::Locking::Adapters::Redlock | ||
ActiveJob::Locking.options.hosts = Redlock::Client::DEFAULT_REDIS_URLS | ||
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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
require_relative('./serialized_tests') | ||
|
||
class SerializedSuoRedisTest < MiniTest::Test | ||
include SerializedTests | ||
|
||
def setup | ||
redis_reset | ||
ActiveJob::Base.queue_adapter = :test | ||
ActiveJob::Base.queue_adapter.perform_enqueued_jobs = true | ||
ActiveJob::Locking.options.adapter = ActiveJob::Locking::Adapters::SuoRedis | ||
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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
%w( | ||
test_serialized_memory | ||
test_serialized_redis_semaphore | ||
test_serialized_redlock | ||
test_serialized_suo_redis | ||
test_unique_memory | ||
test_unique_redis_semaphore | ||
test_unique_redlock | ||
test_unique_suo_redis | ||
).each do |test| | ||
require File.expand_path("../#{test}", __FILE__) | ||
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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
require_relative('./unique_tests') | ||
|
||
class UniqueMemoryTest < MiniTest::Test | ||
include UniqueTests | ||
|
||
def setup | ||
ActiveJob::Locking::Adapters::Memory.reset | ||
ActiveJob::Base.queue_adapter = :test | ||
ActiveJob::Base.queue_adapter.perform_enqueued_jobs = true | ||
ActiveJob::Locking.options.adapter = ActiveJob::Locking::Adapters::Memory | ||
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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
require_relative('./unique_tests') | ||
|
||
class UniqueRedisSemaphoreTest < MiniTest::Test | ||
include UniqueTests | ||
|
||
def setup | ||
redis_reset | ||
ActiveJob::Base.queue_adapter = :test | ||
ActiveJob::Base.queue_adapter.perform_enqueued_jobs = true | ||
ActiveJob::Locking.options.adapter = ActiveJob::Locking::Adapters::RedisSemaphore | ||
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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
require_relative('./unique_tests') | ||
|
||
class UniqueRedlockTest < MiniTest::Test | ||
include UniqueTests | ||
|
||
def setup | ||
redis_reset | ||
|
||
ActiveJob::Base.queue_adapter = :test | ||
ActiveJob::Base.queue_adapter.perform_enqueued_jobs = true | ||
ActiveJob::Locking.options.adapter = ActiveJob::Locking::Adapters::Redlock | ||
ActiveJob::Locking.options.hosts = Redlock::Client::DEFAULT_REDIS_URLS | ||
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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
require_relative('./unique_tests') | ||
|
||
class UniqueSuoRedisTest < MiniTest::Test | ||
include UniqueTests | ||
|
||
def setup | ||
redis_reset | ||
ActiveJob::Base.queue_adapter = :test | ||
ActiveJob::Base.queue_adapter.perform_enqueued_jobs = true | ||
ActiveJob::Locking.options.adapter = ActiveJob::Locking::Adapters::SuoRedis | ||
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 |
---|---|---|
@@ -0,0 +1,100 @@ | ||
require File.expand_path('../test_helper', __FILE__) | ||
|
||
module UniqueTests | ||
def test_none_performed | ||
ActiveJob::Base.queue_adapter.perform_enqueued_jobs = false | ||
|
||
assert_equal(0, ActiveJob::Base.queue_adapter.enqueued_jobs.count) | ||
assert_equal(0, ActiveJob::Base.queue_adapter.performed_jobs.count) | ||
|
||
sleep_time = UniqueJob.lock_acquire_time | ||
threads = 3.times.map do |i| | ||
Thread.new do | ||
UniqueJob.perform_later(i, sleep_time) | ||
end | ||
end | ||
|
||
threads.each {|thread| thread.join} | ||
assert_equal(1, ActiveJob::Base.queue_adapter.enqueued_jobs.count) | ||
assert_equal(0, ActiveJob::Base.queue_adapter.performed_jobs.count) | ||
ensure | ||
ActiveJob::Base.queue_adapter.perform_enqueued_jobs = true | ||
ActiveJob::Base.queue_adapter.enqueued_jobs.clear | ||
end | ||
|
||
def test_one_performed | ||
assert_equal(0, ActiveJob::Base.queue_adapter.enqueued_jobs.count) | ||
assert_equal(0, ActiveJob::Base.queue_adapter.performed_jobs.count) | ||
|
||
sleep_time = UniqueJob.lock_acquire_time * 2 | ||
threads = 3.times.map do |i| | ||
Thread.new do | ||
UniqueJob.perform_later(i, sleep_time) | ||
end | ||
end | ||
|
||
threads.each {|thread| thread.join} | ||
assert_equal(0, ActiveJob::Base.queue_adapter.enqueued_jobs.count) | ||
assert_equal(1, ActiveJob::Base.queue_adapter.performed_jobs.count) | ||
end | ||
|
||
def test_all_performed | ||
assert_equal(0, ActiveJob::Base.queue_adapter.enqueued_jobs.count) | ||
assert_equal(0, ActiveJob::Base.queue_adapter.performed_jobs.count) | ||
|
||
start_time = Time.now | ||
sleep_time = UniqueJob.lock_acquire_time / 4.0 | ||
threads = 3.times.map do |i| | ||
Thread.new do | ||
UniqueJob.perform_later(i, sleep_time) | ||
end | ||
end | ||
|
||
threads.each {|thread| thread.join} | ||
|
||
assert_equal(0, ActiveJob::Base.queue_adapter.enqueued_jobs.count) | ||
assert_equal(threads.count, ActiveJob::Base.queue_adapter.performed_jobs.count) | ||
assert(Time.now - start_time > (threads.count * sleep_time)) | ||
end | ||
|
||
def test_some_performed | ||
assert_equal(0, ActiveJob::Base.queue_adapter.enqueued_jobs.count) | ||
assert_equal(0, ActiveJob::Base.queue_adapter.performed_jobs.count) | ||
|
||
start_time = Time.now | ||
sleep_time = UniqueJob.lock_acquire_time / 2.0 | ||
threads = 3.times.map do |i| | ||
Thread.new do | ||
UniqueJob.perform_later(i, sleep_time) | ||
end | ||
end | ||
|
||
threads.each {|thread| thread.join} | ||
|
||
assert_equal(0, ActiveJob::Base.queue_adapter.enqueued_jobs.count) | ||
assert_equal(threads.count - 1, ActiveJob::Base.queue_adapter.performed_jobs.count) | ||
assert(Time.now - start_time > ((threads.count - 1) * sleep_time)) | ||
end | ||
|
||
def test_fail | ||
assert_equal(0, ActiveJob::Base.queue_adapter.enqueued_jobs.count) | ||
assert_equal(0, ActiveJob::Base.queue_adapter.performed_jobs.count) | ||
|
||
start_time = Time.now | ||
sleep_time = UniqueJob.lock_acquire_time | ||
threads = 3.times.map do |i| | ||
Thread.new do | ||
begin | ||
FailJob.perform_later(i, sleep_time) | ||
rescue => e | ||
# do nothing | ||
end | ||
end | ||
end | ||
|
||
threads.each {|thread| thread.join} | ||
|
||
assert_equal(0, ActiveJob::Base.queue_adapter.enqueued_jobs.count) | ||
assert_equal(threads.count, ActiveJob::Base.queue_adapter.performed_jobs.count) | ||
end | ||
end |