AshCache.Testing (AshCache v0.1.0)

Copy Markdown View Source

Lets a test see the cache events its own code caused, even when many tests run at the same time (async: true).

AshCache announces cache hits, misses (of reads, generic actions and calculations) and invalidations with :telemetry (see AshCache). Telemetry handlers are global, so a plain handler would also catch events from other tests running in parallel. Call capture_events/0 instead:

setup do
  AshCache.Testing.capture_events()
end

test "lookup is cached" do
  MyApp.Shop.get_product_by_sku!("A1")
  MyApp.Shop.get_product_by_sku!("A1")
  assert_received {:ash_cache, :hit, %{action: :get_by_sku}}
end

Each event arrives in the test's mailbox as {:ash_cache, kind, metadata}. kind is :hit, :miss or :invalidate, and metadata is the telemetry metadata described in AshCache.

Only events caused by the test are delivered: those from the test process itself and from Tasks it started. Events from any other process, such as a GenServer started elsewhere, are dropped. The handler is removed automatically when the test ends.

Summary

Functions

Starts delivering cache events to the calling test process.

Functions

capture_events()

@spec capture_events() :: :ok

Starts delivering cache events to the calling test process.

Call it from the test process, in setup or in the test itself.