aboutsummaryrefslogtreecommitdiffstats
path: root/railties/test/queueing
diff options
context:
space:
mode:
authorOscar Del Ben <info@oscardelben.com>2012-04-28 11:45:15 +0200
committerOscar Del Ben <info@oscardelben.com>2012-04-28 11:45:15 +0200
commit4e69a1579ea74fe2823f6fb2f055db97480492e5 (patch)
treed8a5834a0ed5a372969f26db753a30785777a5c2 /railties/test/queueing
parent943410197cca4235212ca0fc409820378a40890c (diff)
parent0297e8e4ddc034bcb8ed477efff91d8fe104cb39 (diff)
downloadrails-4e69a1579ea74fe2823f6fb2f055db97480492e5.tar.gz
rails-4e69a1579ea74fe2823f6fb2f055db97480492e5.tar.bz2
rails-4e69a1579ea74fe2823f6fb2f055db97480492e5.zip
Merge branch 'master' of github.com:lifo/docrails
Diffstat (limited to 'railties/test/queueing')
-rw-r--r--railties/test/queueing/test_queue_test.rb44
-rw-r--r--railties/test/queueing/threaded_consumer_test.rb65
2 files changed, 109 insertions, 0 deletions
diff --git a/railties/test/queueing/test_queue_test.rb b/railties/test/queueing/test_queue_test.rb
new file mode 100644
index 0000000000..de30e8cffd
--- /dev/null
+++ b/railties/test/queueing/test_queue_test.rb
@@ -0,0 +1,44 @@
+require 'abstract_unit'
+require 'rails/queueing'
+
+class TestQueueTest < ActiveSupport::TestCase
+ class Job
+ attr_reader :id
+ def initialize(id, &block)
+ @id = id
+ @block = block
+ end
+
+ def run
+ @block.call if @block
+ end
+ end
+
+ def setup
+ @queue = Rails::Queueing::TestQueue.new
+ end
+
+ def test_contents
+ assert_equal [], @queue.contents
+ job = Job.new(1)
+ @queue.push job
+ assert_equal [job], @queue.contents
+ end
+
+ def test_drain
+ t = nil
+ ran = false
+
+ job = Job.new(1) do
+ ran = true
+ t = Thread.current
+ end
+
+ @queue.push job
+ @queue.drain
+
+ assert_equal [], @queue.contents
+ assert ran, "The job runs synchronously when the queue is drained"
+ assert_not_equal t, Thread.current
+ end
+end
diff --git a/railties/test/queueing/threaded_consumer_test.rb b/railties/test/queueing/threaded_consumer_test.rb
new file mode 100644
index 0000000000..d00a67d511
--- /dev/null
+++ b/railties/test/queueing/threaded_consumer_test.rb
@@ -0,0 +1,65 @@
+require 'abstract_unit'
+require 'rails/queueing'
+
+class TestThreadConsumer < ActiveSupport::TestCase
+ class Job
+ attr_reader :id
+ def initialize(id, &block)
+ @id = id
+ @block = block
+ end
+
+ def run
+ @block.call if @block
+ end
+ end
+
+ def setup
+ @queue = Queue.new
+ @consumer = Rails::Queueing::ThreadedConsumer.start(@queue)
+ end
+
+ def teardown
+ @queue.push nil
+ end
+
+ test "the jobs are executed" do
+ ran = false
+
+ job = Job.new(1) do
+ ran = true
+ end
+
+ @queue.push job
+ sleep 0.1
+ assert_equal true, ran
+ end
+
+ test "the jobs are not executed synchronously" do
+ ran = false
+
+ job = Job.new(1) do
+ sleep 0.1
+ ran = true
+ end
+
+ @queue.push job
+ assert_equal false, ran
+ end
+
+ test "shutting down the queue synchronously drains the jobs" do
+ ran = false
+
+ job = Job.new(1) do
+ sleep 0.1
+ ran = true
+ end
+
+ @queue.push job
+ assert_equal false, ran
+
+ @consumer.shutdown
+
+ assert_equal true, ran
+ end
+end