|
| 1 | +// Copyright (c) 2025 Uber Technologies, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package lifecycle |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "fmt" |
| 20 | + "testing" |
| 21 | + |
| 22 | + "github.com/stretchr/testify/assert" |
| 23 | + "github.com/stretchr/testify/require" |
| 24 | +) |
| 25 | + |
| 26 | +// spy records the order of Start/Stop calls and can be configured to fail. |
| 27 | +type spy struct { |
| 28 | + name string |
| 29 | + startErr error |
| 30 | + stopErr error |
| 31 | + log *[]string |
| 32 | +} |
| 33 | + |
| 34 | +func (s *spy) Start(_ context.Context) error { |
| 35 | + *s.log = append(*s.log, "start:"+s.name) |
| 36 | + return s.startErr |
| 37 | +} |
| 38 | + |
| 39 | +func (s *spy) Stop(_ context.Context) error { |
| 40 | + *s.log = append(*s.log, "stop:"+s.name) |
| 41 | + return s.stopErr |
| 42 | +} |
| 43 | + |
| 44 | +func TestGroup_StartStop_HappyPath(t *testing.T) { |
| 45 | + var log []string |
| 46 | + a := &spy{name: "a", log: &log} |
| 47 | + b := &spy{name: "b", log: &log} |
| 48 | + c := &spy{name: "c", log: &log} |
| 49 | + |
| 50 | + g := NewGroup(a, b, c) |
| 51 | + |
| 52 | + require.NoError(t, g.Start(context.Background())) |
| 53 | + assert.Equal(t, []string{"start:a", "start:b", "start:c"}, log) |
| 54 | + |
| 55 | + log = nil |
| 56 | + require.NoError(t, g.Stop(context.Background())) |
| 57 | + assert.Equal(t, []string{"stop:c", "stop:b", "stop:a"}, log) |
| 58 | +} |
| 59 | + |
| 60 | +func TestGroup_StartRollback_OnFailure(t *testing.T) { |
| 61 | + var log []string |
| 62 | + a := &spy{name: "a", log: &log} |
| 63 | + b := &spy{name: "b", startErr: fmt.Errorf("b broke"), log: &log} |
| 64 | + c := &spy{name: "c", log: &log} |
| 65 | + |
| 66 | + g := NewGroup(a, b, c) |
| 67 | + |
| 68 | + err := g.Start(context.Background()) |
| 69 | + require.Error(t, err) |
| 70 | + assert.Contains(t, err.Error(), "b broke") |
| 71 | + |
| 72 | + // a was started and then rolled back; b failed; c was never started |
| 73 | + assert.Equal(t, []string{"start:a", "start:b", "stop:a"}, log) |
| 74 | +} |
| 75 | + |
| 76 | +func TestGroup_StartRollback_FirstMemberFails(t *testing.T) { |
| 77 | + var log []string |
| 78 | + a := &spy{name: "a", startErr: fmt.Errorf("a broke"), log: &log} |
| 79 | + b := &spy{name: "b", log: &log} |
| 80 | + |
| 81 | + g := NewGroup(a, b) |
| 82 | + |
| 83 | + err := g.Start(context.Background()) |
| 84 | + require.Error(t, err) |
| 85 | + assert.Contains(t, err.Error(), "a broke") |
| 86 | + |
| 87 | + // Nothing to roll back — a failed on start, b never started |
| 88 | + assert.Equal(t, []string{"start:a"}, log) |
| 89 | +} |
| 90 | + |
| 91 | +func TestGroup_StartRollback_JoinsStopErrors(t *testing.T) { |
| 92 | + var log []string |
| 93 | + a := &spy{name: "a", stopErr: fmt.Errorf("a stop failed"), log: &log} |
| 94 | + b := &spy{name: "b", log: &log} |
| 95 | + c := &spy{name: "c", startErr: fmt.Errorf("c broke"), log: &log} |
| 96 | + |
| 97 | + g := NewGroup(a, b, c) |
| 98 | + |
| 99 | + err := g.Start(context.Background()) |
| 100 | + require.Error(t, err) |
| 101 | + assert.Contains(t, err.Error(), "c broke") |
| 102 | + assert.Contains(t, err.Error(), "a stop failed") |
| 103 | + |
| 104 | + // a and b started, c failed, then b and a rolled back in reverse |
| 105 | + assert.Equal(t, []string{"start:a", "start:b", "start:c", "stop:b", "stop:a"}, log) |
| 106 | +} |
| 107 | + |
| 108 | +func TestGroup_Stop_CollectsAllErrors(t *testing.T) { |
| 109 | + var log []string |
| 110 | + a := &spy{name: "a", stopErr: fmt.Errorf("a stop failed"), log: &log} |
| 111 | + b := &spy{name: "b", stopErr: fmt.Errorf("b stop failed"), log: &log} |
| 112 | + c := &spy{name: "c", log: &log} |
| 113 | + |
| 114 | + g := NewGroup(a, b, c) |
| 115 | + require.NoError(t, g.Start(context.Background())) |
| 116 | + |
| 117 | + log = nil |
| 118 | + err := g.Stop(context.Background()) |
| 119 | + require.Error(t, err) |
| 120 | + assert.Contains(t, err.Error(), "a stop failed") |
| 121 | + assert.Contains(t, err.Error(), "b stop failed") |
| 122 | + |
| 123 | + // All three stopped in reverse despite errors |
| 124 | + assert.Equal(t, []string{"stop:c", "stop:b", "stop:a"}, log) |
| 125 | +} |
| 126 | + |
| 127 | +func TestGroup_NilMembers_Skipped(t *testing.T) { |
| 128 | + var log []string |
| 129 | + a := &spy{name: "a", log: &log} |
| 130 | + |
| 131 | + g := NewGroup(nil, a, nil) |
| 132 | + |
| 133 | + require.NoError(t, g.Start(context.Background())) |
| 134 | + assert.Equal(t, []string{"start:a"}, log) |
| 135 | + |
| 136 | + log = nil |
| 137 | + require.NoError(t, g.Stop(context.Background())) |
| 138 | + assert.Equal(t, []string{"stop:a"}, log) |
| 139 | +} |
| 140 | + |
| 141 | +func TestGroup_Empty(t *testing.T) { |
| 142 | + g := NewGroup() |
| 143 | + require.NoError(t, g.Start(context.Background())) |
| 144 | + require.NoError(t, g.Stop(context.Background())) |
| 145 | +} |
| 146 | + |
| 147 | +func TestGroup_Nested(t *testing.T) { |
| 148 | + var log []string |
| 149 | + a := &spy{name: "a", log: &log} |
| 150 | + b := &spy{name: "b", log: &log} |
| 151 | + c := &spy{name: "c", log: &log} |
| 152 | + d := &spy{name: "d", log: &log} |
| 153 | + |
| 154 | + inner := NewGroup(b, c) |
| 155 | + outer := NewGroup(a, inner, d) |
| 156 | + |
| 157 | + require.NoError(t, outer.Start(context.Background())) |
| 158 | + assert.Equal(t, []string{"start:a", "start:b", "start:c", "start:d"}, log) |
| 159 | + |
| 160 | + log = nil |
| 161 | + require.NoError(t, outer.Stop(context.Background())) |
| 162 | + assert.Equal(t, []string{"stop:d", "stop:c", "stop:b", "stop:a"}, log) |
| 163 | +} |
| 164 | + |
| 165 | +func TestGroup_Nested_RollbackOnInnerFailure(t *testing.T) { |
| 166 | + var log []string |
| 167 | + a := &spy{name: "a", log: &log} |
| 168 | + b := &spy{name: "b", log: &log} |
| 169 | + c := &spy{name: "c", startErr: fmt.Errorf("c broke"), log: &log} |
| 170 | + d := &spy{name: "d", log: &log} |
| 171 | + |
| 172 | + inner := NewGroup(b, c) |
| 173 | + outer := NewGroup(a, inner, d) |
| 174 | + |
| 175 | + err := outer.Start(context.Background()) |
| 176 | + require.Error(t, err) |
| 177 | + assert.Contains(t, err.Error(), "c broke") |
| 178 | + |
| 179 | + // a started, inner started b then c failed, inner rolled back b, |
| 180 | + // then outer rolled back a. d never started. |
| 181 | + assert.Equal(t, []string{"start:a", "start:b", "start:c", "stop:b", "stop:a"}, log) |
| 182 | +} |
0 commit comments