Skip to content

Commit

Permalink
Allow aync steps with compensations without side effects #44
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewDryga committed Jan 26, 2020
1 parent c3f1762 commit dcdde33
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/sage/executor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ defmodule Sage.Executor do
end
end

defp execute_compensation({{name, {:run, _transaction, :noop, _tx_opts} = operation}, state}, _opts) do
defp execute_compensation({{name, {_type, _transaction, :noop, _tx_opts} = operation}, state}, _opts) do
{name, operation, :ok, nil, state}
end

Expand Down
14 changes: 14 additions & 0 deletions test/sage/executor_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,20 @@ defmodule Sage.ExecutorTest do
hook_assertion.()
end

test "effects are not compensated for async operations with :noop compensation" do
{hook, hook_assertion} = final_hook_with_assertion(:error, a: :b)

result =
new()
|> run_async(:step1, transaction_with_error(:t1), :noop)
|> finally(hook)
|> execute(a: :b)

assert_effects([:t1])
assert result == {:error, :t1}
hook_assertion.()
end

test "asynchronous transactions process metadata is copied from parent process" do
test_pid = self()
metadata = [test_pid: test_pid, test_ref: make_ref()]
Expand Down
9 changes: 8 additions & 1 deletion test/sage_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ defmodule SageTest do
end
end

describe "run_async/4" do
describe "run_async/5" do
test "adds compensation via anonymous function to a sage" do
tx = transaction(:t1)
cmp = compensation()
Expand All @@ -218,6 +218,13 @@ defmodule SageTest do
assert MapSet.member?(names, :step1)
end

test "adds noop compensation to a sage" do
tx = transaction(:t1)
%Sage{stages: stages, stage_names: names} = run_async(new(), :step1, tx, :noop, timeout: 5_000)
assert {:step1, {:run_async, tx, :noop, [timeout: 5_000]}} in stages
assert MapSet.member?(names, :step1)
end

test "adds compensation via mfa tuple to a sage" do
tx = transaction(:t1)
cmp = {__MODULE__, :dummy_compensation_for_mfa, []}
Expand Down

0 comments on commit dcdde33

Please sign in to comment.