From 08a2ba99e0bdecd0835e73ec8945dcaf7020f127 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Sun, 18 May 2014 12:32:22 +0200 Subject: [PATCH] Simplest job and inline queue --- lib/active_job/base.rb | 7 +++++++ lib/active_job/queue_adapters/inline_queue.rb | 11 +++++++++++ test/cases/queuing_test.rb | 16 ++++++++++++++-- test/jobs/hello_job.rb | 5 +++++ 4 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 lib/active_job/queue_adapters/inline_queue.rb create mode 100644 test/jobs/hello_job.rb diff --git a/lib/active_job/base.rb b/lib/active_job/base.rb index f2c76c1811..a404355026 100644 --- a/lib/active_job/base.rb +++ b/lib/active_job/base.rb @@ -1,4 +1,11 @@ +require 'active_job/queue_adapters/inline_queue' + module ActiveJob class Base + class << self + def enqueue(*args) + ActiveJob::QueueAdapters::InlineQueue.queue self, *args + end + end end end \ No newline at end of file diff --git a/lib/active_job/queue_adapters/inline_queue.rb b/lib/active_job/queue_adapters/inline_queue.rb new file mode 100644 index 0000000000..dd80c6a5a3 --- /dev/null +++ b/lib/active_job/queue_adapters/inline_queue.rb @@ -0,0 +1,11 @@ +module ActiveJob + module QueueAdapters + class InlineQueue + class << self + def queue(job, *args) + job.perform *args + end + end + end + end +end \ No newline at end of file diff --git a/test/cases/queuing_test.rb b/test/cases/queuing_test.rb index 5f1b35fee5..b6180a23dd 100644 --- a/test/cases/queuing_test.rb +++ b/test/cases/queuing_test.rb @@ -1,7 +1,19 @@ require 'helper' +require 'jobs/hello_job' + class QueuingTest < ActiveSupport::TestCase - test 'the truth' do - assert true + setup do + $BUFFER = [] + end + + test 'run queued job' do + HelloJob.enqueue + assert_equal "David says hello", $BUFFER.pop + end + + test 'run queued job with parameters' do + HelloJob.enqueue "Jamie" + assert_equal "Jamie says hello", $BUFFER.pop end end diff --git a/test/jobs/hello_job.rb b/test/jobs/hello_job.rb new file mode 100644 index 0000000000..d0e65f9674 --- /dev/null +++ b/test/jobs/hello_job.rb @@ -0,0 +1,5 @@ +class HelloJob < ActiveJob::Base + def self.perform(greeter = "David") + $BUFFER << "#{greeter} says hello" + end +end