Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/geometerio/tangent
https://github.com/geometerio/tangent
Last synced: about 2 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/geometerio/tangent
- Owner: geometerio
- License: mit
- Created: 2021-07-08T21:03:10.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2021-07-14T00:04:55.000Z (over 3 years ago)
- Last Synced: 2024-10-30T09:27:37.770Z (2 months ago)
- Language: Elixir
- Size: 33.2 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Tangent
[Documentation](https://hexdocs.pm/tangent)
Let your `Agent` calls go on a tangent.
Tangent provides functions and macros for bridging global `Agent` processes with `ExUnit` tests
configured to be `async: true`.## Usage
`Tangent` can be used as a drop-in replacement for `Agent`:
```elixir
defmodule MyTangent do
use Tangentdef start_link(_), do: Tangent.start_link(fn -> 0 end, name: __MODULE__)
def current(), do: Tangent.get(__MODULE__, & &1)
def increment(), do: Tangent.get_and_update(__MODULE__, fn current -> {current + 1, current + 1} end)
def decrement(), do: Tangent.get_and_update(__MODULE__, fn current -> {current - 1, current - 1} end)
end
```When `Mix.env/0` is not equal to `:test`, this will compile to use `Agent` directly. In `:test` mode, this will
instead compile to use an interceptor process. By default this process will behave like an `Agent`, with a single
global dataset that will be accessed when callers access `Tangent`.Test processes can register themselves as owners of dataset overloads via the `Tangent.Test.overload/1` macro.
If a process that traces its ancestry to the owner attempts to access the data, it will only get/update the
overloaded dataset specific to the owner.```elixir
defmodule MyTangentTest do
use ExUnit.Case, async: true
use Tangent.Testsetup do
Tangent.Test.overload(MyTangent)
enddescribe "increment" do
test "increments the saved value" do
assert MyTangent.current() == 0
assert MyTangent.increment() == 1
assert MyTangent.increment() == 2
assert MyTangent.current() == 2spawn fn ->
assert MyTangent.current() == 0
assert MyTangent.increment() == 1
assert MyTangent.current() == 1
endTask.async fn ->
assert MyTangent.current() == 2
end
|> Task.await()assert MyTangent.current() == 2
end
end
end
```Note that the above works because `Task.async/1` spawns a process with an ancestry list, while `spawn/1` spawns
a process with no ancestry.