Cache.CaseTemplate (elixir_cache v0.4.6)

View Source

A reusable ExUnit case template for applications using elixir_cache.

Creates a CacheCase module that automatically starts sandboxed caches in setup for every test that uses it.

Creating a CacheCase module

Pass an explicit list of cache modules:

defmodule MyApp.CacheCase do
  use Cache.CaseTemplate, default_caches: [MyApp.UserCache, MyApp.SessionCache]
end

Or discover caches at runtime by inspecting a running supervisor:

defmodule MyApp.CacheCase do
  use Cache.CacheTemplate, supervisors: [MyApp.Supervisor]
end

Using the CacheCase in a test file

defmodule MyApp.SomeTest do
  use ExUnit.Case, async: true
  use MyApp.CacheCase

  # or with additional caches just for this file:
  use MyApp.CacheCase, caches: [MyApp.ExtraCache]
end

Options for use Cache.CaseTemplate

  • :default_caches — list of cache modules to start for every test
  • :supervisors — list of supervisor atoms; their Cache children are discovered at runtime

Options for use MyApp.CacheCase

  • :caches — additional cache modules for this test file only
  • :sleep — milliseconds to sleep after starting caches (default: 10)

Summary

Functions

Inspects a running supervisor's children to find cache modules started under a Cache supervisor child.

Validates that the list of cache modules contains no duplicates.

Functions

inferred_caches(supervisors)

@spec inferred_caches([atom()] | atom()) :: [module()]

Inspects a running supervisor's children to find cache modules started under a Cache supervisor child.

Raises if the given supervisor is not running or has no Cache child.

validate_uniq!(caches)

@spec validate_uniq!([module()]) :: [module()]

Validates that the list of cache modules contains no duplicates.

Raises with a descriptive message listing the duplicates if any are found.