From 65a80482d3b818599fe1c07bae675630d451578c Mon Sep 17 00:00:00 2001 From: Sergey Andronov Date: Mon, 7 Oct 2024 19:33:43 +0200 Subject: [PATCH] Fix running the last attempt --- .../webhooks/outgoing/delivery_support.rb | 2 +- .../webhooks/outgoing/delivery_test.rb | 33 +++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 bullet_train-outgoing_webhooks/test/models/concerns/webhooks/outgoing/delivery_test.rb diff --git a/bullet_train-outgoing_webhooks/app/models/concerns/webhooks/outgoing/delivery_support.rb b/bullet_train-outgoing_webhooks/app/models/concerns/webhooks/outgoing/delivery_support.rb index 50827faa1..46d207f33 100644 --- a/bullet_train-outgoing_webhooks/app/models/concerns/webhooks/outgoing/delivery_support.rb +++ b/bullet_train-outgoing_webhooks/app/models/concerns/webhooks/outgoing/delivery_support.rb @@ -51,7 +51,7 @@ def delivered? def still_attempting? return false if delivered? - attempt_count < max_attempts + attempt_count <= max_attempts end def failed? diff --git a/bullet_train-outgoing_webhooks/test/models/concerns/webhooks/outgoing/delivery_test.rb b/bullet_train-outgoing_webhooks/test/models/concerns/webhooks/outgoing/delivery_test.rb new file mode 100644 index 000000000..ccca3f362 --- /dev/null +++ b/bullet_train-outgoing_webhooks/test/models/concerns/webhooks/outgoing/delivery_test.rb @@ -0,0 +1,33 @@ +require "test_helper" + +class Webhooks::Outgoing::DeliveryTest < ActiveSupport::TestCase + include ActiveJob::TestHelper + + setup do + @team = Team.create!(name: "test-team") + @endpoint = Webhooks::Outgoing::Endpoint.create!(url: "https://example.com/webhook", name: "test", team: @team) + @subject = @team + @event = Webhooks::Outgoing::Event.create!(team: @team, subject: @subject, api_version: "1") + end + + test "#deliver_async schedule jobs" do + delivery = Webhooks::Outgoing::Delivery.create!(endpoint: @endpoint, event: @event) + freeze_time + + # first job runs immediately + assert_enqueued_with(job: Webhooks::Outgoing::DeliveryJob, at: nil) do + delivery.deliver_async + end + + # shedule other jobs with the delays from ATTEMPT_SCHEDULE + Webhooks::Outgoing::Delivery::ATTEMPT_SCHEDULE.each do |attempt, delay| + delivery.reload + delivery.delivery_attempts.create!(attempt_number: attempt, response_code: 500, error_message: "error") + assert_enqueued_with(job: Webhooks::Outgoing::DeliveryJob, at: delay.from_now) do + delivery.deliver_async + end + end + + assert_equal Webhooks::Outgoing::Delivery::ATTEMPT_SCHEDULE.size, Webhooks::Outgoing::DeliveryAttempt.count, "amount of attempts should e equal to ATTEMPT_SCHEDULE" + end +end