|
| 1 | +/* |
| 2 | +Copyright 2025 the Unikorn Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package saga_test |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "errors" |
| 22 | + "testing" |
| 23 | + |
| 24 | + "github.com/stretchr/testify/require" |
| 25 | + |
| 26 | + "github.com/unikorn-cloud/core/pkg/server/saga" |
| 27 | +) |
| 28 | + |
| 29 | +var ( |
| 30 | + errFailAction = errors.New("fail action") |
| 31 | + errFailCompensate = errors.New("fail compensation") |
| 32 | +) |
| 33 | + |
| 34 | +type Handler struct { |
| 35 | + action1Result error |
| 36 | + action2Result error |
| 37 | + action3Result error |
| 38 | + |
| 39 | + compensate1Result error |
| 40 | + compensate2Result error |
| 41 | + |
| 42 | + action1Called bool |
| 43 | + action2Called bool |
| 44 | + action3Called bool |
| 45 | + |
| 46 | + compensate1Called bool |
| 47 | + compensate2Called bool |
| 48 | +} |
| 49 | + |
| 50 | +func (h *Handler) action1(ctx context.Context) error { |
| 51 | + h.action1Called = true |
| 52 | + return h.action1Result |
| 53 | +} |
| 54 | + |
| 55 | +func (h *Handler) action2(ctx context.Context) error { |
| 56 | + h.action2Called = true |
| 57 | + return h.action2Result |
| 58 | +} |
| 59 | + |
| 60 | +func (h *Handler) action3(ctx context.Context) error { |
| 61 | + h.action3Called = true |
| 62 | + return h.action3Result |
| 63 | +} |
| 64 | + |
| 65 | +func (h *Handler) compensate1(ctx context.Context) error { |
| 66 | + h.compensate1Called = true |
| 67 | + return h.compensate1Result |
| 68 | +} |
| 69 | + |
| 70 | +func (h *Handler) compensate2(ctx context.Context) error { |
| 71 | + h.compensate2Called = true |
| 72 | + return h.compensate2Result |
| 73 | +} |
| 74 | + |
| 75 | +func (h *Handler) Actions() []saga.Action { |
| 76 | + return []saga.Action{ |
| 77 | + saga.NewAction("action1", h.action1, h.compensate1), |
| 78 | + saga.NewAction("action2", h.action2, h.compensate2), |
| 79 | + saga.NewAction("action3", h.action3, nil), |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +// TestSaga ensures all actions are called. |
| 84 | +func TestSaga(t *testing.T) { |
| 85 | + t.Parallel() |
| 86 | + |
| 87 | + h := &Handler{} |
| 88 | + |
| 89 | + require.NoError(t, saga.Run(t.Context(), h)) |
| 90 | + require.True(t, h.action1Called) |
| 91 | + require.True(t, h.action2Called) |
| 92 | + require.True(t, h.action3Called) |
| 93 | + require.False(t, h.compensate1Called) |
| 94 | + require.False(t, h.compensate2Called) |
| 95 | +} |
| 96 | + |
| 97 | +// TestSagaFailAction1 ensures compensating actions are correctly run. |
| 98 | +func TestSagaFailAction1(t *testing.T) { |
| 99 | + t.Parallel() |
| 100 | + |
| 101 | + h := &Handler{ |
| 102 | + action1Result: errFailAction, |
| 103 | + } |
| 104 | + |
| 105 | + require.ErrorIs(t, saga.Run(t.Context(), h), errFailAction) |
| 106 | + require.True(t, h.action1Called) |
| 107 | + require.False(t, h.action2Called) |
| 108 | + require.False(t, h.action3Called) |
| 109 | + require.False(t, h.compensate1Called) |
| 110 | + require.False(t, h.compensate2Called) |
| 111 | +} |
| 112 | + |
| 113 | +// TestSagaFailAction3 ensures compensating actions are correctly run. |
| 114 | +func TestSagaFailAction3(t *testing.T) { |
| 115 | + t.Parallel() |
| 116 | + |
| 117 | + h := &Handler{ |
| 118 | + action3Result: errFailAction, |
| 119 | + } |
| 120 | + |
| 121 | + require.ErrorIs(t, saga.Run(t.Context(), h), errFailAction) |
| 122 | + require.True(t, h.action1Called) |
| 123 | + require.True(t, h.action2Called) |
| 124 | + require.True(t, h.action3Called) |
| 125 | + require.True(t, h.compensate1Called) |
| 126 | + require.True(t, h.compensate2Called) |
| 127 | +} |
| 128 | + |
| 129 | +// TestSagaFailCompensation2 tests that compensation errors short circuit |
| 130 | +// and that the error returned is that of the failing action. |
| 131 | +func TestSagaFailCompensation2(t *testing.T) { |
| 132 | + t.Parallel() |
| 133 | + |
| 134 | + h := &Handler{ |
| 135 | + action3Result: errFailAction, |
| 136 | + compensate2Result: errFailCompensate, |
| 137 | + } |
| 138 | + |
| 139 | + require.ErrorIs(t, saga.Run(t.Context(), h), errFailAction) |
| 140 | + require.True(t, h.action1Called) |
| 141 | + require.True(t, h.action2Called) |
| 142 | + require.True(t, h.action3Called) |
| 143 | + require.False(t, h.compensate1Called) |
| 144 | + require.True(t, h.compensate2Called) |
| 145 | +} |
0 commit comments