|
| 1 | +require File.dirname(__FILE__) + '/test_helper' |
| 2 | + |
| 3 | +context "Multiple plugins with multiple hooks" do |
| 4 | + include PerformJob |
| 5 | + |
| 6 | + module Plugin1 |
| 7 | + def before_perform_record_history1(history) |
| 8 | + history << :before1 |
| 9 | + end |
| 10 | + def after_perform_record_history1(history) |
| 11 | + history << :after1 |
| 12 | + end |
| 13 | + end |
| 14 | + |
| 15 | + module Plugin2 |
| 16 | + def before_perform_record_history2(history) |
| 17 | + history << :before2 |
| 18 | + end |
| 19 | + def after_perform_record_history2(history) |
| 20 | + history << :after2 |
| 21 | + end |
| 22 | + end |
| 23 | + |
| 24 | + class ManyBeforesJob |
| 25 | + extend Plugin1 |
| 26 | + extend Plugin2 |
| 27 | + def self.perform(history) |
| 28 | + history << :perform |
| 29 | + end |
| 30 | + end |
| 31 | + |
| 32 | + test "hooks of each type are executed in alphabetical order" do |
| 33 | + result = perform_job(ManyBeforesJob, history=[]) |
| 34 | + assert_equal true, result, "perform returned true" |
| 35 | + assert_equal [:before1, :before2, :perform, :after1, :after2], history |
| 36 | + end |
| 37 | +end |
| 38 | + |
| 39 | +context "Resque::Plugin ordering before_perform" do |
| 40 | + include PerformJob |
| 41 | + |
| 42 | + module BeforePerformPlugin |
| 43 | + def before_perform1(history) |
| 44 | + history << :before_perform1 |
| 45 | + end |
| 46 | + end |
| 47 | + |
| 48 | + class BeforePerformJob |
| 49 | + extend BeforePerformPlugin |
| 50 | + def self.perform(history) |
| 51 | + history << :perform |
| 52 | + end |
| 53 | + def self.before_perform(history) |
| 54 | + history << :before_perform |
| 55 | + end |
| 56 | + end |
| 57 | + |
| 58 | + test "before_perform hooks are executed in order" do |
| 59 | + result = perform_job(BeforePerformJob, history=[]) |
| 60 | + assert_equal true, result, "perform returned true" |
| 61 | + assert_equal [:before_perform, :before_perform1, :perform], history |
| 62 | + end |
| 63 | +end |
| 64 | + |
| 65 | +context "Resque::Plugin ordering after_perform" do |
| 66 | + include PerformJob |
| 67 | + |
| 68 | + module AfterPerformPlugin |
| 69 | + def after_perform_record_history(history) |
| 70 | + history << :after_perform1 |
| 71 | + end |
| 72 | + end |
| 73 | + |
| 74 | + class AfterPerformJob |
| 75 | + extend AfterPerformPlugin |
| 76 | + def self.perform(history) |
| 77 | + history << :perform |
| 78 | + end |
| 79 | + def self.after_perform(history) |
| 80 | + history << :after_perform |
| 81 | + end |
| 82 | + end |
| 83 | + |
| 84 | + test "after_perform hooks are executed in order" do |
| 85 | + result = perform_job(AfterPerformJob, history=[]) |
| 86 | + assert_equal true, result, "perform returned true" |
| 87 | + assert_equal [:perform, :after_perform, :after_perform1], history |
| 88 | + end |
| 89 | +end |
| 90 | + |
| 91 | +context "Resque::Plugin ordering around_perform" do |
| 92 | + include PerformJob |
| 93 | + |
| 94 | + module AroundPerformPlugin1 |
| 95 | + def around_perform1(history) |
| 96 | + history << :around_perform_plugin1 |
| 97 | + yield |
| 98 | + end |
| 99 | + end |
| 100 | + |
| 101 | + class AroundPerformJustPerformsJob |
| 102 | + extend AroundPerformPlugin1 |
| 103 | + def self.perform(history) |
| 104 | + history << :perform |
| 105 | + end |
| 106 | + end |
| 107 | + |
| 108 | + test "around_perform hooks are executed before the job" do |
| 109 | + result = perform_job(AroundPerformJustPerformsJob, history=[]) |
| 110 | + assert_equal true, result, "perform returned true" |
| 111 | + assert_equal [:around_perform_plugin1, :perform], history |
| 112 | + end |
| 113 | + |
| 114 | + class AroundPerformJob |
| 115 | + extend AroundPerformPlugin1 |
| 116 | + def self.perform(history) |
| 117 | + history << :perform |
| 118 | + end |
| 119 | + def self.around_perform(history) |
| 120 | + history << :around_perform |
| 121 | + yield |
| 122 | + end |
| 123 | + end |
| 124 | + |
| 125 | + test "around_perform hooks are executed in order" do |
| 126 | + result = perform_job(AroundPerformJob, history=[]) |
| 127 | + assert_equal true, result, "perform returned true" |
| 128 | + assert_equal [:around_perform, :around_perform_plugin1, :perform], history |
| 129 | + end |
| 130 | + |
| 131 | + module AroundPerformPlugin2 |
| 132 | + def around_perform2(history) |
| 133 | + history << :around_perform_plugin2 |
| 134 | + yield |
| 135 | + end |
| 136 | + end |
| 137 | + |
| 138 | + class AroundPerformJob2 |
| 139 | + extend AroundPerformPlugin1 |
| 140 | + extend AroundPerformPlugin2 |
| 141 | + def self.perform(history) |
| 142 | + history << :perform |
| 143 | + end |
| 144 | + def self.around_perform(history) |
| 145 | + history << :around_perform |
| 146 | + yield |
| 147 | + end |
| 148 | + end |
| 149 | + |
| 150 | + test "many around_perform are executed in order" do |
| 151 | + result = perform_job(AroundPerformJob2, history=[]) |
| 152 | + assert_equal true, result, "perform returned true" |
| 153 | + assert_equal [:around_perform, :around_perform_plugin1, :around_perform_plugin2, :perform], history |
| 154 | + end |
| 155 | + |
| 156 | + module AroundPerformDoesNotYield |
| 157 | + def around_perform0(history) |
| 158 | + history << :around_perform0 |
| 159 | + end |
| 160 | + end |
| 161 | + |
| 162 | + class AroundPerformJob3 |
| 163 | + extend AroundPerformPlugin1 |
| 164 | + extend AroundPerformPlugin2 |
| 165 | + extend AroundPerformDoesNotYield |
| 166 | + def self.perform(history) |
| 167 | + history << :perform |
| 168 | + end |
| 169 | + def self.around_perform(history) |
| 170 | + history << :around_perform |
| 171 | + yield |
| 172 | + end |
| 173 | + end |
| 174 | + |
| 175 | + test "the job is aborted if an around_perform hook does not yield" do |
| 176 | + result = perform_job(AroundPerformJob3, history=[]) |
| 177 | + assert_equal false, result, "perform returned false" |
| 178 | + assert_equal [:around_perform, :around_perform0], history |
| 179 | + end |
| 180 | +end |
| 181 | + |
| 182 | +context "Resque::Plugin ordering on_failure" do |
| 183 | + include PerformJob |
| 184 | + |
| 185 | + module OnFailurePlugin |
| 186 | + def on_failure1(exception, history) |
| 187 | + history << "#{exception.message} plugin" |
| 188 | + end |
| 189 | + end |
| 190 | + |
| 191 | + class FailureJob |
| 192 | + extend OnFailurePlugin |
| 193 | + def self.perform(history) |
| 194 | + history << :perform |
| 195 | + raise StandardError, "oh no" |
| 196 | + end |
| 197 | + def self.on_failure(exception, history) |
| 198 | + history << exception.message |
| 199 | + end |
| 200 | + end |
| 201 | + |
| 202 | + test "on_failure hooks are executed in order" do |
| 203 | + history = [] |
| 204 | + assert_raises StandardError do |
| 205 | + perform_job(FailureJob, history) |
| 206 | + end |
| 207 | + assert_equal [:perform, "oh no", "oh no plugin"], history |
| 208 | + end |
| 209 | +end |
0 commit comments