Skip to content

Commit

Permalink
Even more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
AntoineAugusti committed Dec 19, 2024
1 parent c7e293a commit 35f64e7
Showing 1 changed file with 59 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@ defmodule TransportWeb.Plugs.WorkerHealthcheckTest do
use TransportWeb.ConnCase, async: false
alias TransportWeb.Plugs.WorkerHealthcheck

@cache_name Transport.Cache.Cachex.cache_name()
@cache_key WorkerHealthcheck.app_start_datetime_cache_key_name()

setup do
# Use a real in-memory cache for these tests to test the caching mecanism
old_value = Application.fetch_env!(:transport, :cache_impl)
Application.put_env(:transport, :cache_impl, Transport.Cache.Cachex)

on_exit(fn ->
Application.put_env(:transport, :cache_impl, old_value)
Cachex.reset(Transport.Cache.Cachex.cache_name())
Cachex.reset(@cache_name)
end)

Ecto.Adapters.SQL.Sandbox.checkout(DB.Repo)
Expand All @@ -25,7 +28,7 @@ defmodule TransportWeb.Plugs.WorkerHealthcheckTest do

test "app was not started recently, Oban jobs have not been attempted recently" do
datetime = DateTime.add(DateTime.utc_now(), -30, :minute)
Cachex.put(Transport.Cache.Cachex.cache_name(), WorkerHealthcheck.app_start_datetime_cache_key_name(), datetime)
Cachex.put(@cache_name, @cache_key, datetime)

refute WorkerHealthcheck.app_started_recently?()
refute WorkerHealthcheck.oban_attempted_jobs_recently?()
Expand All @@ -34,7 +37,7 @@ defmodule TransportWeb.Plugs.WorkerHealthcheckTest do

test "app was not started recently, Oban jobs have been attempted recently" do
datetime = DateTime.add(DateTime.utc_now(), -30, :minute)
Cachex.put(Transport.Cache.Cachex.cache_name(), WorkerHealthcheck.app_start_datetime_cache_key_name(), datetime)
Cachex.put(@cache_name, @cache_key, datetime)

# A completed job was attempted 55 minutes ago
Transport.Jobs.ResourceUnavailableJob.new(%{resource_id: 1})
Expand All @@ -48,6 +51,58 @@ defmodule TransportWeb.Plugs.WorkerHealthcheckTest do
end
end

describe "app_started_recently?" do
test "value is set when executed for the first time" do
assert {:ok, false} == Cachex.exists?(@cache_name, @cache_key)
# Calling for the first time creates the key
assert WorkerHealthcheck.app_started_recently?()
assert {:ok, true} == Cachex.exists?(@cache_name, @cache_key)

# Calling again does not refresh the initial value
start_datetime = WorkerHealthcheck.app_start_datetime()
WorkerHealthcheck.app_started_recently?()
assert start_datetime == WorkerHealthcheck.app_start_datetime()

# Key does not expire
assert {:ok, nil} == Cachex.ttl(@cache_name, @cache_key)
end

test "acceptable delay is 20 minutes" do
# Just right
datetime = DateTime.add(DateTime.utc_now(), -19, :minute)
Cachex.put(@cache_name, @cache_key, datetime)

assert WorkerHealthcheck.app_started_recently?()

# Too long ago
datetime = DateTime.add(DateTime.utc_now(), -21, :minute)
Cachex.put(@cache_name, @cache_key, datetime)
refute WorkerHealthcheck.app_started_recently?()
end
end

describe "oban_attempted_jobs_recently?" do
test "job attempted recently" do
# Attempted less than 60 minutes ago
Transport.Jobs.ResourceUnavailableJob.new(%{resource_id: 1})
|> Oban.insert!()
|> Ecto.Changeset.change(attempted_at: DateTime.add(DateTime.utc_now(), -59, :minute), state: "completed")
|> DB.Repo.update!()

assert WorkerHealthcheck.oban_attempted_jobs_recently?()
end

test "job attempted too long ago" do
# Attempted more than 60 minutes ago
Transport.Jobs.ResourceUnavailableJob.new(%{resource_id: 1})
|> Oban.insert!()
|> Ecto.Changeset.change(attempted_at: DateTime.add(DateTime.utc_now(), -61, :minute), state: "completed")
|> DB.Repo.update!()

refute WorkerHealthcheck.oban_attempted_jobs_recently?()
end
end

describe "call" do
test "healthy system", %{conn: conn} do
assert WorkerHealthcheck.app_started_recently?()
Expand All @@ -59,7 +114,7 @@ defmodule TransportWeb.Plugs.WorkerHealthcheckTest do

test "unhealthy system", %{conn: conn} do
datetime = DateTime.add(DateTime.utc_now(), -30, :minute)
Cachex.put(Transport.Cache.Cachex.cache_name(), WorkerHealthcheck.app_start_datetime_cache_key_name(), datetime)
Cachex.put(@cache_name, @cache_key, datetime)

refute WorkerHealthcheck.app_started_recently?()
refute WorkerHealthcheck.oban_attempted_jobs_recently?()
Expand Down

0 comments on commit 35f64e7

Please sign in to comment.