Skip to content

Commit

Permalink
use ActiveSupport::Notification.subscribed
Browse files Browse the repository at this point in the history
  • Loading branch information
elfassy committed Feb 12, 2024
1 parent b499351 commit e275f83
Showing 1 changed file with 8 additions and 13 deletions.
21 changes: 8 additions & 13 deletions test/models/maintenance_tasks/run_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class RunTest < ActiveSupport::TestCase
run = Run.create!(task_name: "Maintenance::CallbackTestTask", status: "running")
run.status = :succeeded
run.task.expects(:after_complete_callback)
assert_notification_for("succeeded", run: run) do
assert_notification_for("maintenance_tasks.succeeded", run: run) do
run.persist_transition
end
end
Expand All @@ -93,7 +93,7 @@ class RunTest < ActiveSupport::TestCase
)
run.status = :cancelled
run.task.expects(:after_cancel_callback)
assert_notification_for("cancelled", run: run) do
assert_notification_for("maintenance_tasks.cancelled", run: run) do
run.persist_transition
end
end
Expand All @@ -102,7 +102,7 @@ class RunTest < ActiveSupport::TestCase
run = Run.create!(task_name: "Maintenance::CallbackTestTask", status: "pausing")
run.status = :paused
run.task.expects(:after_pause_callback)
assert_notification_for("paused", run: run) do
assert_notification_for("maintenance_tasks.paused", run: run) do
run.persist_transition
end
end
Expand All @@ -115,7 +115,7 @@ class RunTest < ActiveSupport::TestCase
run.task.expects(:after_cancel_callback)

run.status = :interrupted
assert_notification_for("cancelled", run: run) do
assert_notification_for("maintenance_tasks.cancelled", run: run) do
run.persist_transition
end
assert_predicate run.reload, :cancelled?
Expand All @@ -129,7 +129,7 @@ class RunTest < ActiveSupport::TestCase
run.task.expects(:after_complete_callback)

run.status = :succeeded
assert_notification_for("succeeded", run: run) do
assert_notification_for("maintenance_tasks.succeeded", run: run) do
run.persist_transition
end
assert_predicate run.reload, :succeeded?
Expand Down Expand Up @@ -181,7 +181,7 @@ class RunTest < ActiveSupport::TestCase
error = ArgumentError.new("Something went wrong")
error.set_backtrace(["lib/foo.rb:42:in `bar'"])
run.task.expects(:after_error_callback)
assert_notification_for("errored", run: run) do
assert_notification_for("maintenance_tasks.errored", run: run) do
run.persist_error(error)
end
end
Expand Down Expand Up @@ -702,14 +702,9 @@ class RunTest < ActiveSupport::TestCase

private

def assert_notification_for(name, expected_payload)
def assert_notification_for(name, expected_payload, &block)
payload = nil

notification = ActiveSupport::Notifications.subscribe("maintenance_tasks.#{name}") do |*args|
payload = args.last
end
yield
ActiveSupport::Notifications.unsubscribe(notification)
ActiveSupport::Notifications.subscribed(->(*args) { payload = args.last }, name, &block)
assert_equal(expected_payload, payload)
end

Expand Down

0 comments on commit e275f83

Please sign in to comment.