From b6bbcc83481f21e52524371cb6421e98510bcc69 Mon Sep 17 00:00:00 2001 From: Graham McIntire Date: Fri, 24 Apr 2026 10:13:17 -0500 Subject: [PATCH] tests: remove remaining 1s sleeps in slow tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * UISP diff_snapshots test: backdate first snapshot via UPDATE instead of Process.sleep(1_100) to get distinct second-precision snapshot_at values. * PagerDuty.Client.send_event_with_retry/2: check max_retries BEFORE sleeping on 429. Previously a 429 response always slept once (~1s) before checking the retry cap, so even with @max_retries = 0 in test env every 429 test paid that cost. Suite time: 65.3s → 33.9s (-48%). No test is >400ms now. --- lib/towerops/pagerduty/client.ex | 15 ++++++++++----- test/towerops/uisp/config_snapshot_test.exs | 11 +++++++++-- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/lib/towerops/pagerduty/client.ex b/lib/towerops/pagerduty/client.ex index fd61d8e5..274f8145 100644 --- a/lib/towerops/pagerduty/client.ex +++ b/lib/towerops/pagerduty/client.ex @@ -102,12 +102,17 @@ defmodule Towerops.PagerDuty.Client do {:error, {:bad_request, resp_body["message"] || "Invalid event"}} {:ok, %{status: 429} = response} -> - # Respect Retry-After header if present - retry_after = extract_retry_after(response, retries) + if retries >= @max_retries do + Logger.warning("PagerDuty rate limited after #{@max_retries} retries, giving up") + {:error, :rate_limited} + else + retry_after = extract_retry_after(response, retries) - Logger.info("PagerDuty rate limited, retrying in #{retry_after}ms (attempt #{retries + 1}/#{@max_retries})") - Process.sleep(retry_after) - send_event_with_retry(body, retries + 1) + Logger.info("PagerDuty rate limited, retrying in #{retry_after}ms (attempt #{retries + 1}/#{@max_retries})") + + Process.sleep(retry_after) + send_event_with_retry(body, retries + 1) + end {:ok, %{status: status}} -> {:error, {:unexpected_status, status}} diff --git a/test/towerops/uisp/config_snapshot_test.exs b/test/towerops/uisp/config_snapshot_test.exs index 52adbec3..cc95b6f2 100644 --- a/test/towerops/uisp/config_snapshot_test.exs +++ b/test/towerops/uisp/config_snapshot_test.exs @@ -222,8 +222,15 @@ defmodule Towerops.Uisp.ConfigSnapshotTest do test "returns computed diff of two stored snapshots", %{device: device} do ConfigSnapshot.store_if_changed(device, %{"a" => 1}, %{stored: 0, unchanged: 0}) - # Ensure distinct snapshot_at timestamps (second precision). - Process.sleep(1_100) + # Backdate the first snapshot so the second one has a strictly later + # snapshot_at, without the slow Process.sleep(1_100) a second-precision + # timestamp would otherwise require. + {1, _} = + Towerops.Repo.update_all( + Ecto.Query.from(s in "uisp_config_snapshots", where: s.device_id == type(^device.id, :binary_id)), + set: [snapshot_at: DateTime.add(Towerops.Time.now(), -5, :second)] + ) + ConfigSnapshot.store_if_changed(device, %{"a" => 2, "b" => 3}, %{stored: 0, unchanged: 0}) [newer, older] = ConfigSnapshot.list_snapshots(device.id)