ExJsonschema.Cache.Test (ExJsonschema v0.1.17)

View Source

Test cache implementation using process dictionary and Application environment.

This cache is designed for testing and supports both async and non-async tests:

Usage

Async Tests (isolated per-process):

setup do
  test_cache = start_supervised!({Agent, fn -> %{} end})
  ExJsonschema.Cache.Test.set_cache_for_process(test_cache)
  :ok
end

Non-async Tests (global, works with spawns):

setup do
  test_cache = start_supervised!({Agent, fn -> %{} end})
  ExJsonschema.Cache.Test.set_global_cache(test_cache)

  on_exit(fn ->
    ExJsonschema.Cache.Test.clear_global_cache()
  end)

  :ok
end

Summary

Functions

Clears the global cache reference.

Sets the cache pid/name for the current process.

Sets the global cache pid/name for non-async tests.

Sets up the test cache in global mode for non-async tests.

Sets up the test cache in process-local mode for async tests.

Functions

clear_global_cache()

Clears the global cache reference.

Should be called in test cleanup to avoid test pollution.

set_cache_for_process(pid_or_name)

Sets the cache pid/name for the current process.

This cache will only be available to the current process and any processes spawned from it that explicitly inherit the process dictionary.

set_global_cache(pid_or_name)

Sets the global cache pid/name for non-async tests.

This cache will be available to all processes, including spawned tasks. Should only be used in non-async tests to avoid conflicts.

setup_global_mode(cache_pid)

Sets up the test cache in global mode for non-async tests.

Returns a cleanup function that should be called in test teardown.

Usage

setup do
  test_cache = start_supervised!({Agent, fn -> %{} end})
  cleanup = ExJsonschema.Cache.Test.setup_global_mode(test_cache)

  on_exit(cleanup)
  :ok
end

setup_process_mode(cache_pid)

Sets up the test cache in process-local mode for async tests.

Returns a cleanup function that should be called in test teardown.

Usage

setup do
  test_cache = start_supervised!({Agent, fn -> %{} end})
  cleanup = ExJsonschema.Cache.Test.setup_process_mode(test_cache)

  on_exit(cleanup)
  :ok
end