Swiss.Task (swiss v3.12.0) View Source
Functions for writing async code that's testable asynchronously by passing allowances to created processes. You should import this module and use its macros as a DSL.
Examples
defmodule YourProject.Task do
@moduledoc "Defines the Task API for your project"
import Swiss.Task
async YourProject.Task.Allowances
async_stream YourProject.Task.Allowances
end
defmodule YourProject.Task.Allowances do
@moduledoc "Defines the Allowances behaviour"
@behaviour Swiss.Task.Allowances
@impl true
def allow(parent) do
Ecto.Adapters.SQL.Sandbox.allow(YourProject.Repo, parent, self())
end
end
defmodule YourProject.SomeModule do
def some_async_function do
YourProject.Task.async(fn -> end)
end
def some_other_async_function(enumerable) do
YourProject.Task.async_stream(enumerable, fn -> end)
end
end
Link to this section Summary
Functions
Call this macro from your projects Task
module to declare an async
function that, in a test environment, runs the called module's allow
function with the parent process's PID.
Call this macro from your project's Task
module to define an async_stream
function that, in a test environment, runs the called module's allow
function with the parent process's PID
Link to this section Functions
Call this macro from your projects Task
module to declare an async
function that, in a test environment, runs the called module's allow
function with the parent process's PID.
In other envs this delegates to the Elixir.Task.async/1
function.
Call this macro from your project's Task
module to define an async_stream
function that, in a test environment, runs the called module's allow
function with the parent process's PID
In other envs this delegates to the Elixir.Task.async_stream/3
function.