aboutsummaryrefslogtreecommitdiffstats
path: root/activejob/test/support
diff options
context:
space:
mode:
authorAbdelkader Boudih <terminale@gmail.com>2014-08-12 09:17:19 +0000
committerAbdelkader Boudih <terminale@gmail.com>2014-08-12 09:17:19 +0000
commita75f085941b2d6aed160f5f2f7e64e5fc7e03826 (patch)
tree23174b264983a15c3108ec912b355829c5f17a80 /activejob/test/support
parentb45b99894a60eda434abec94d133a1cfd8de2dda (diff)
parent14f74a8331f94150dfee653224de8fc837797709 (diff)
downloadrails-a75f085941b2d6aed160f5f2f7e64e5fc7e03826.tar.gz
rails-a75f085941b2d6aed160f5f2f7e64e5fc7e03826.tar.bz2
rails-a75f085941b2d6aed160f5f2f7e64e5fc7e03826.zip
Add 'activejob/' from commit '14f74a8331f94150dfee653224de8fc837797709'
git-subtree-dir: activejob git-subtree-mainline: b45b99894a60eda434abec94d133a1cfd8de2dda git-subtree-split: 14f74a8331f94150dfee653224de8fc837797709
Diffstat (limited to 'activejob/test/support')
-rw-r--r--activejob/test/support/backburner/inline.rb8
-rw-r--r--activejob/test/support/delayed_job/delayed/backend/test.rb113
-rw-r--r--activejob/test/support/delayed_job/delayed/serialization/test.rb0
-rw-r--r--activejob/test/support/integration/adapters/backburner.rb15
-rw-r--r--activejob/test/support/integration/adapters/delayed_job.rb14
-rw-r--r--activejob/test/support/integration/adapters/qu.rb14
-rw-r--r--activejob/test/support/integration/adapters/que.rb19
-rw-r--r--activejob/test/support/integration/adapters/queue_classic.rb21
-rw-r--r--activejob/test/support/integration/adapters/resque.rb18
-rw-r--r--activejob/test/support/integration/adapters/sidekiq.rb19
-rw-r--r--activejob/test/support/integration/adapters/sneakers.rb18
-rw-r--r--activejob/test/support/integration/adapters/sucker_punch.rb5
-rw-r--r--activejob/test/support/integration/helper.rb12
-rw-r--r--activejob/test/support/integration/jobs_manager.rb23
-rw-r--r--activejob/test/support/integration/test_case_helpers.rb30
-rw-r--r--activejob/test/support/queue_classic/inline.rb11
-rw-r--r--activejob/test/support/sneakers/inline.rb12
17 files changed, 352 insertions, 0 deletions
diff --git a/activejob/test/support/backburner/inline.rb b/activejob/test/support/backburner/inline.rb
new file mode 100644
index 0000000000..f761b53e27
--- /dev/null
+++ b/activejob/test/support/backburner/inline.rb
@@ -0,0 +1,8 @@
+require 'backburner'
+
+Backburner::Worker.class_eval do
+ class << self; alias_method :original_enqueue, :enqueue; end
+ def self.enqueue(job_class, args=[], opts={})
+ job_class.perform(*args)
+ end
+end \ No newline at end of file
diff --git a/activejob/test/support/delayed_job/delayed/backend/test.rb b/activejob/test/support/delayed_job/delayed/backend/test.rb
new file mode 100644
index 0000000000..b50ed36fc2
--- /dev/null
+++ b/activejob/test/support/delayed_job/delayed/backend/test.rb
@@ -0,0 +1,113 @@
+#copied from https://github.com/collectiveidea/delayed_job/blob/master/spec/delayed/backend/test.rb
+require 'ostruct'
+
+# An in-memory backend suitable only for testing. Tries to behave as if it were an ORM.
+module Delayed
+ module Backend
+ module Test
+ class Job
+ attr_accessor :id
+ attr_accessor :priority
+ attr_accessor :attempts
+ attr_accessor :handler
+ attr_accessor :last_error
+ attr_accessor :run_at
+ attr_accessor :locked_at
+ attr_accessor :locked_by
+ attr_accessor :failed_at
+ attr_accessor :queue
+
+ include Delayed::Backend::Base
+
+ cattr_accessor :id
+ self.id = 0
+
+ def initialize(hash = {})
+ self.attempts = 0
+ self.priority = 0
+ self.id = (self.class.id += 1)
+ hash.each{|k,v| send(:"#{k}=", v)}
+ end
+
+ @jobs = []
+ def self.all
+ @jobs
+ end
+
+ def self.count
+ all.size
+ end
+
+ def self.delete_all
+ all.clear
+ end
+
+ def self.create(attrs = {})
+ new(attrs).tap do |o|
+ o.save
+ end
+ end
+
+ def self.create!(*args); create(*args); end
+
+ def self.clear_locks!(worker_name)
+ all.select{|j| j.locked_by == worker_name}.each {|j| j.locked_by = nil; j.locked_at = nil}
+ end
+
+ # Find a few candidate jobs to run (in case some immediately get locked by others).
+ def self.find_available(worker_name, limit = 5, max_run_time = Worker.max_run_time)
+ jobs = all.select do |j|
+ j.run_at <= db_time_now &&
+ (j.locked_at.nil? || j.locked_at < db_time_now - max_run_time || j.locked_by == worker_name) &&
+ !j.failed?
+ end
+
+ jobs = jobs.select{|j| Worker.queues.include?(j.queue)} if Worker.queues.any?
+ jobs = jobs.select{|j| j.priority >= Worker.min_priority} if Worker.min_priority
+ jobs = jobs.select{|j| j.priority <= Worker.max_priority} if Worker.max_priority
+ jobs.sort_by{|j| [j.priority, j.run_at]}[0..limit-1]
+ end
+
+ # Lock this job for this worker.
+ # Returns true if we have the lock, false otherwise.
+ def lock_exclusively!(max_run_time, worker)
+ now = self.class.db_time_now
+ if locked_by != worker
+ # We don't own this job so we will update the locked_by name and the locked_at
+ self.locked_at = now
+ self.locked_by = worker
+ end
+
+ return true
+ end
+
+ def self.db_time_now
+ Time.current
+ end
+
+ def update_attributes(attrs = {})
+ attrs.each{|k,v| send(:"#{k}=", v)}
+ save
+ end
+
+ def destroy
+ self.class.all.delete(self)
+ end
+
+ def save
+ self.run_at ||= Time.current
+
+ self.class.all << self unless self.class.all.include?(self)
+ true
+ end
+
+ def save!; save; end
+
+ def reload
+ reset
+ self
+ end
+ end
+ end
+ end
+end
diff --git a/activejob/test/support/delayed_job/delayed/serialization/test.rb b/activejob/test/support/delayed_job/delayed/serialization/test.rb
new file mode 100644
index 0000000000..e69de29bb2
--- /dev/null
+++ b/activejob/test/support/delayed_job/delayed/serialization/test.rb
diff --git a/activejob/test/support/integration/adapters/backburner.rb b/activejob/test/support/integration/adapters/backburner.rb
new file mode 100644
index 0000000000..7271d50a45
--- /dev/null
+++ b/activejob/test/support/integration/adapters/backburner.rb
@@ -0,0 +1,15 @@
+module BackburnerJobsManager
+ def clear_jobs
+ Backburner::Worker.connection.tubes.all.map &:clear
+ end
+
+ def start_workers
+ @thread = Thread.new { Backburner.work "active-jobs-default" }
+ end
+
+ def stop_workers
+ @thread.kill
+ end
+
+end
+
diff --git a/activejob/test/support/integration/adapters/delayed_job.rb b/activejob/test/support/integration/adapters/delayed_job.rb
new file mode 100644
index 0000000000..3e55933438
--- /dev/null
+++ b/activejob/test/support/integration/adapters/delayed_job.rb
@@ -0,0 +1,14 @@
+module DelayedJobJobsManager
+ def clear_jobs
+ Delayed::Job.delete_all
+ end
+
+ def start_workers
+ @worker = Delayed::Worker.new(quiet: false, sleep_delay: 0.5)
+ @thread = Thread.new { @worker.start }
+ end
+
+ def stop_workers
+ @worker.stop
+ end
+end
diff --git a/activejob/test/support/integration/adapters/qu.rb b/activejob/test/support/integration/adapters/qu.rb
new file mode 100644
index 0000000000..12d063ea0d
--- /dev/null
+++ b/activejob/test/support/integration/adapters/qu.rb
@@ -0,0 +1,14 @@
+module QuJobsManager
+ def clear_jobs
+ Qu.clear "active_jobs_default"
+ end
+
+ def start_workers
+ @thread = Thread.new { Qu::Worker.new("active_jobs_default").start }
+ end
+
+ def stop_workers
+ @thread.kill
+ end
+end
+
diff --git a/activejob/test/support/integration/adapters/que.rb b/activejob/test/support/integration/adapters/que.rb
new file mode 100644
index 0000000000..f15c9af910
--- /dev/null
+++ b/activejob/test/support/integration/adapters/que.rb
@@ -0,0 +1,19 @@
+module QueJobsManager
+ def clear_jobs
+ Que.clear!
+ end
+
+ def start_workers
+ @thread = Thread.new do
+ loop do
+ Que::Job.work("active_jobs_default")
+ sleep 0.5
+ end
+ end
+ end
+
+ def stop_workers
+ @thread.kill
+ end
+end
+
diff --git a/activejob/test/support/integration/adapters/queue_classic.rb b/activejob/test/support/integration/adapters/queue_classic.rb
new file mode 100644
index 0000000000..3b24eca5b9
--- /dev/null
+++ b/activejob/test/support/integration/adapters/queue_classic.rb
@@ -0,0 +1,21 @@
+module QC; WAIT_TIME = 0.5; end
+
+module QueueClassicJobsManager
+ def clear_jobs
+ # disabling this as it locks
+ # QC::Queue.new("active_jobs_default").delete_all
+ end
+
+ def start_workers
+ @pid = fork do
+ QC::Conn.connection = QC::Conn.connect
+ worker = QC::Worker.new(q_name: 'active_jobs_default')
+ worker.start
+ end
+ end
+
+ def stop_workers
+ Process.kill 'HUP', @pid
+ end
+end
+
diff --git a/activejob/test/support/integration/adapters/resque.rb b/activejob/test/support/integration/adapters/resque.rb
new file mode 100644
index 0000000000..b9811bd3e1
--- /dev/null
+++ b/activejob/test/support/integration/adapters/resque.rb
@@ -0,0 +1,18 @@
+module ResqueJobsManager
+ def clear_jobs
+ Resque.queues.each { |queue_name| Resque.redis.del "queue:#{queue_name}" }
+ Resque.redis.keys("delayed:*").each { |key| Resque.redis.del "#{key}" }
+ Resque.redis.del "delayed_queue_schedule"
+ end
+
+ def start_workers
+ @thread = Thread.new do
+ Resque::Worker.new("*").work(0.5)
+ end
+ end
+
+ def stop_workers
+ @thread.kill
+ end
+end
+
diff --git a/activejob/test/support/integration/adapters/sidekiq.rb b/activejob/test/support/integration/adapters/sidekiq.rb
new file mode 100644
index 0000000000..d3b7d15614
--- /dev/null
+++ b/activejob/test/support/integration/adapters/sidekiq.rb
@@ -0,0 +1,19 @@
+require 'sidekiq/launcher'
+require 'sidekiq/api'
+
+module SidekiqJobsManager
+ def clear_jobs
+ Sidekiq::Queue.new("active_jobs_default").clear
+ end
+
+ def start_workers
+ options = {:queues=>["active_jobs_default"], :concurrency=>1, :environment=>"test", :timeout=>8, :daemon=>true, :strict=>true}
+ @launcher = Sidekiq::Launcher.new(options)
+ @launcher.run
+ end
+
+ def stop_workers
+ @launcher.stop
+ end
+end
+
diff --git a/activejob/test/support/integration/adapters/sneakers.rb b/activejob/test/support/integration/adapters/sneakers.rb
new file mode 100644
index 0000000000..5dcab68515
--- /dev/null
+++ b/activejob/test/support/integration/adapters/sneakers.rb
@@ -0,0 +1,18 @@
+require 'sneakers/runner'
+
+module SneakersJobsManager
+ def clear_jobs
+ end
+
+ def start_workers
+ cmd = %{cd #{Rails.root.to_s} && (RAILS_ENV=test AJADAPTER=sneakers WORKERS=ActiveJob::QueueAdapters::SneakersAdapter::JobWrapper bundle exec rake --trace sneakers:run)}
+ `#{cmd}`
+ while !Rails.root.join("tmp/sneakers.pid").exist? do
+ sleep 0.5
+ end
+ end
+
+ def stop_workers
+ Process.kill 'TERM', File.open(Rails.root.join("tmp/sneakers.pid").to_s).read.to_i
+ end
+end
diff --git a/activejob/test/support/integration/adapters/sucker_punch.rb b/activejob/test/support/integration/adapters/sucker_punch.rb
new file mode 100644
index 0000000000..317f9c80fd
--- /dev/null
+++ b/activejob/test/support/integration/adapters/sucker_punch.rb
@@ -0,0 +1,5 @@
+module SuckerPunchJobsManager
+ def clear_jobs
+ end
+end
+
diff --git a/activejob/test/support/integration/helper.rb b/activejob/test/support/integration/helper.rb
new file mode 100644
index 0000000000..cb94e7cfb5
--- /dev/null
+++ b/activejob/test/support/integration/helper.rb
@@ -0,0 +1,12 @@
+ENV["RAILS_ENV"] = "test"
+require File.expand_path("../../../dummy/config/environment.rb", __FILE__)
+require "rails/test_help"
+Rails.backtrace_cleaner.remove_silencers!
+
+require_relative 'test_case_helpers'
+ActiveSupport::TestCase.send(:include, TestCaseHelpers)
+
+JobsManager.current_manager.setup
+JobsManager.current_manager.start_workers
+Minitest.after_run { JobsManager.current_manager.stop_workers }
+
diff --git a/activejob/test/support/integration/jobs_manager.rb b/activejob/test/support/integration/jobs_manager.rb
new file mode 100644
index 0000000000..1da74193b1
--- /dev/null
+++ b/activejob/test/support/integration/jobs_manager.rb
@@ -0,0 +1,23 @@
+class JobsManager
+ @@managers = {}
+ attr :adapter_name
+
+ def self.current_manager
+ @@managers[ENV['AJADAPTER']] ||= new(ENV['AJADAPTER'])
+ end
+
+ def initialize(adapter_name)
+ @adapter_name = adapter_name
+ require_relative "adapters/#{adapter_name}"
+ extend "#{adapter_name.camelize}JobsManager".constantize
+ end
+
+ def setup
+ end
+
+ def start_workers
+ end
+
+ def stop_workers
+ end
+end
diff --git a/activejob/test/support/integration/test_case_helpers.rb b/activejob/test/support/integration/test_case_helpers.rb
new file mode 100644
index 0000000000..9a5eea0783
--- /dev/null
+++ b/activejob/test/support/integration/test_case_helpers.rb
@@ -0,0 +1,30 @@
+require 'active_support/concern'
+require 'support/integration/jobs_manager'
+
+module TestCaseHelpers
+ extend ActiveSupport::Concern
+
+ included do
+ self.use_transactional_fixtures = false
+
+ setup do
+ clear_jobs
+ end
+
+ teardown do
+ clear_jobs
+ FileUtils.rm_rf Dir[Dummy::Application.root.join("tmp/AJ-*")]
+ end
+ end
+
+ protected
+
+ def jobs_manager
+ JobsManager.current_manager
+ end
+
+ def clear_jobs
+ jobs_manager.clear_jobs
+ end
+
+end
diff --git a/activejob/test/support/queue_classic/inline.rb b/activejob/test/support/queue_classic/inline.rb
new file mode 100644
index 0000000000..5e9c295e01
--- /dev/null
+++ b/activejob/test/support/queue_classic/inline.rb
@@ -0,0 +1,11 @@
+require 'queue_classic'
+
+module QC
+ class Queue
+ def enqueue(method, *args)
+ receiver_str, _, message = method.rpartition('.')
+ receiver = eval(receiver_str)
+ receiver.send(message, *args)
+ end
+ end
+end
diff --git a/activejob/test/support/sneakers/inline.rb b/activejob/test/support/sneakers/inline.rb
new file mode 100644
index 0000000000..16d9b830fa
--- /dev/null
+++ b/activejob/test/support/sneakers/inline.rb
@@ -0,0 +1,12 @@
+require 'sneakers'
+
+module Sneakers
+ module Worker
+ module ClassMethods
+ def enqueue(msg)
+ worker = self.new(nil, nil, {})
+ worker.work(*msg)
+ end
+ end
+ end
+end