Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 231 lines (197 sloc) 5.847 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
require 'test_helper'

context "Multiple plugins with multiple hooks" do
  include PerformJob

  module Plugin1
    def before_perform_record_history1(history)
      history << :before1
    end
    def after_perform_record_history1(history)
      history << :after1
    end
  end

  module Plugin2
    def before_perform_record_history2(history)
      history << :before2
    end
    def after_perform_record_history2(history)
      history << :after2
    end
  end

  class ::ManyBeforesJob
    extend Plugin1
    extend Plugin2
    def self.perform(history)
      history << :perform
    end
  end

  test "hooks of each type are executed in alphabetical order" do
    result = perform_job(ManyBeforesJob, history=[])
    assert_equal true, result, "perform returned true"
    assert_equal [:before1, :before2, :perform, :after1, :after2], history
  end
end

context "Resque::Plugin ordering before_perform" do
  include PerformJob

  module BeforePerformPlugin
    def before_perform1(history)
      history << :before_perform1
    end
  end

  class ::JobPluginsTestBeforePerformJob
    extend BeforePerformPlugin
    def self.perform(history)
      history << :perform
    end
    def self.before_perform(history)
      history << :before_perform
    end
  end

  test "before_perform hooks are executed in order" do
    result = perform_job(JobPluginsTestBeforePerformJob, history=[])
    assert_equal true, result, "perform returned true"
    assert_equal [:before_perform, :before_perform1, :perform], history
  end
end

context "Resque::Plugin ordering after_perform" do
  include PerformJob

  module AfterPerformPlugin
    def after_perform_record_history(history)
      history << :after_perform1
    end
  end

  class ::JobPluginsTestAfterPerformJob
    extend AfterPerformPlugin
    def self.perform(history)
      history << :perform
    end
    def self.after_perform(history)
      history << :after_perform
    end
  end

  test "after_perform hooks are executed in order" do
    result = perform_job(JobPluginsTestAfterPerformJob, history=[])
    assert_equal true, result, "perform returned true"
    assert_equal [:perform, :after_perform, :after_perform1], history
  end
end

context "Resque::Plugin ordering around_perform" do
  include PerformJob

  module AroundPerformPlugin1
    def around_perform1(history)
      history << :around_perform_plugin1
      yield
    end
  end

  class ::AroundPerformJustPerformsJob
    extend AroundPerformPlugin1
    def self.perform(history)
      history << :perform
    end
  end

  test "around_perform hooks are executed before the job" do
    result = perform_job(AroundPerformJustPerformsJob, history=[])
    assert_equal true, result, "perform returned true"
    assert_equal [:around_perform_plugin1, :perform], history
  end

  class ::JobPluginsTestAroundPerformJob
    extend AroundPerformPlugin1
    def self.perform(history)
      history << :perform
    end
    def self.around_perform(history)
      history << :around_perform
      yield
    end
  end

  test "around_perform hooks are executed in order" do
    result = perform_job(JobPluginsTestAroundPerformJob, history=[])
    assert_equal true, result, "perform returned true"
    assert_equal [:around_perform, :around_perform_plugin1, :perform], history
  end

  module AroundPerformPlugin2
    def around_perform2(history)
      history << :around_perform_plugin2
      yield
    end
  end

  class ::AroundPerformJob2
    extend AroundPerformPlugin1
    extend AroundPerformPlugin2
    def self.perform(history)
      history << :perform
    end
    def self.around_perform(history)
      history << :around_perform
      yield
    end
  end

  test "many around_perform are executed in order" do
    result = perform_job(AroundPerformJob2, history=[])
    assert_equal true, result, "perform returned true"
    assert_equal [:around_perform, :around_perform_plugin1, :around_perform_plugin2, :perform], history
  end

  module AroundPerformDoesNotYield
    def around_perform0(history)
      history << :around_perform0
    end
  end

  class ::AroundPerformJob3
    extend AroundPerformPlugin1
    extend AroundPerformPlugin2
    extend AroundPerformDoesNotYield
    def self.perform(history)
      history << :perform
    end
    def self.around_perform(history)
      history << :around_perform
      yield
    end
  end

  test "the job is aborted if an around_perform hook does not yield" do
    result = perform_job(AroundPerformJob3, history=[])
    assert_equal false, result, "perform returned false"
    assert_equal [:around_perform, :around_perform0], history
  end

  module AroundPerformGetsJobResult
    @@result = nil
    def last_job_result
      @@result
    end

    def around_perform_gets_job_result(*args)
      @@result = yield
    end
  end

  class ::AroundPerformJobWithReturnValue < GoodJob
    extend AroundPerformGetsJobResult
  end

  test "the job is aborted if an around_perform hook does not yield" do
    result = perform_job(AroundPerformJobWithReturnValue, 'Bob')
    assert_equal true, result, "perform returned true"
    assert_equal 'Good job, Bob', AroundPerformJobWithReturnValue.last_job_result
  end
end

context "Resque::Plugin ordering on_failure" do
  include PerformJob

  module OnFailurePlugin
    def on_failure1(exception, history)
      history << "#{exception.message} plugin"
    end
  end

  class ::FailureJob
    extend OnFailurePlugin
    def self.perform(history)
      history << :perform
      raise StandardError, "oh no"
    end
    def self.on_failure(exception, history)
      history << exception.message
    end
  end

  test "on_failure hooks are executed in order" do
    history = []
    assert_raises StandardError do
      perform_job(FailureJob, history)
    end
    assert_equal [:perform, "oh no", "oh no plugin"], history
  end
end
Something went wrong with that request. Please try again.