Skip to content
This repository
Browse code

Make TestQueueTest work with marshalling queue

This requires all jobs to be instances of named classes, without block
implementations of methods.
  • Loading branch information...
commit b44104ae1357f0177056e833d7cd1e0abaa5c759 1 parent a3ade2e
Paul Battley authored July 03, 2012

Showing 1 changed file with 60 additions and 25 deletions. Show diff stats Hide diff stats

  1. 85  railties/test/queueing/test_queue_test.rb
85  railties/test/queueing/test_queue_test.rb
@@ -2,22 +2,18 @@
2 2
 require 'rails/queueing'
3 3
 
4 4
 class TestQueueTest < ActiveSupport::TestCase
5  
-  class Job
6  
-    def initialize(&block)
7  
-      @block = block
8  
-    end
  5
+  def setup
  6
+    @queue = Rails::Queueing::TestQueue.new
  7
+  end
9 8
 
  9
+  class ExceptionRaisingJob
10 10
     def run
11  
-      @block.call if @block
  11
+      raise
12 12
     end
13 13
   end
14 14
 
15  
-  def setup
16  
-    @queue = Rails::Queueing::TestQueue.new
17  
-  end
18  
-
19 15
   def test_drain_raises
20  
-    @queue.push Job.new { raise }
  16
+    @queue.push ExceptionRaisingJob.new
21 17
     assert_raises(RuntimeError) { @queue.drain }
22 18
   end
23 19
 
@@ -27,41 +23,80 @@ def test_jobs
27 23
     assert_equal [1,2], @queue.jobs
28 24
   end
29 25
 
  26
+  class EquivalentJob
  27
+    def initialize
  28
+      @initial_id = self.object_id
  29
+    end
  30
+
  31
+    def run
  32
+    end
  33
+
  34
+    def ==(other)
  35
+      other.same_initial_id?(@initial_id)
  36
+    end
  37
+
  38
+    def same_initial_id?(other_id)
  39
+      other_id == @initial_id
  40
+    end
  41
+  end
  42
+
30 43
   def test_contents
31 44
     assert @queue.empty?
32  
-    job = Job.new
  45
+    job = EquivalentJob.new
33 46
     @queue.push job
34 47
     refute @queue.empty?
35 48
     assert_equal job, @queue.pop
36 49
   end
37 50
 
38  
-  def test_order
39  
-    processed = []
  51
+  class ProcessingJob
  52
+    def self.clear_processed
  53
+      @processed = []
  54
+    end
  55
+
  56
+    def self.processed
  57
+      @processed
  58
+    end
  59
+
  60
+    def initialize(object)
  61
+      @object = object
  62
+    end
  63
+
  64
+    def run
  65
+      self.class.processed << @object
  66
+    end
  67
+  end
40 68
 
41  
-    job1 = Job.new { processed << 1 }
42  
-    job2 = Job.new { processed << 2 }
  69
+  def test_order
  70
+    ProcessingJob.clear_processed
  71
+    job1 = ProcessingJob.new(1)
  72
+    job2 = ProcessingJob.new(2)
43 73
 
44 74
     @queue.push job1
45 75
     @queue.push job2
46 76
     @queue.drain
47 77
 
48  
-    assert_equal [1,2], processed
  78
+    assert_equal [1,2], ProcessingJob.processed
49 79
   end
50 80
 
51  
-  def test_drain
52  
-    t = nil
53  
-    ran = false
  81
+  class ThreadTrackingJob
  82
+    attr_reader :thread_id
54 83
 
55  
-    job = Job.new do
56  
-      ran = true
57  
-      t = Thread.current
  84
+    def run
  85
+      @thread_id = Thread.current.object_id
58 86
     end
59 87
 
60  
-    @queue.push job
  88
+    def ran?
  89
+      @thread_id
  90
+    end
  91
+  end
  92
+
  93
+  def test_drain
  94
+    @queue.push ThreadTrackingJob.new
  95
+    job = @queue.jobs.last
61 96
     @queue.drain
62 97
 
63 98
     assert @queue.empty?
64  
-    assert ran, "The job runs synchronously when the queue is drained"
65  
-    assert_not_equal t, Thread.current
  99
+    assert job.ran?, "The job runs synchronously when the queue is drained"
  100
+    assert_not_equal job.thread_id, Thread.current.object_id
66 101
   end
67 102
 end

0 notes on commit b44104a

Please sign in to comment.
Something went wrong with that request. Please try again.