|
4 | 4 | "fmt" |
5 | 5 | "sync/atomic" |
6 | 6 | "testing" |
| 7 | + "time" |
7 | 8 |
|
8 | 9 | "github.com/stretchr/testify/assert" |
9 | 10 |
|
@@ -183,6 +184,52 @@ func testCombineOptStopTogether(t *testing.T, actorsCount int) { |
183 | 184 | } |
184 | 185 | } |
185 | 186 |
|
| 187 | +// Test_Combine_OthersNotStopped asserts that if any of underlaying |
| 188 | +// actors end it wont affect other actors. This is default behavior when |
| 189 | +// `OptStopTogether` is not provided. |
| 190 | +func Test_Combine_OthersNotStopped(t *testing.T) { |
| 191 | + t.Parallel() |
| 192 | + |
| 193 | + combineParallel(t, 1, testCombineOthersNotStopped) |
| 194 | + combineParallel(t, 2, testCombineOthersNotStopped) |
| 195 | + combineParallel(t, 10, testCombineOthersNotStopped) |
| 196 | +} |
| 197 | + |
| 198 | +func testCombineOthersNotStopped(t *testing.T, actorsCount int) { |
| 199 | + t.Helper() |
| 200 | + |
| 201 | + for i := range actorsCount { |
| 202 | + onStartC := make(chan any, actorsCount) |
| 203 | + onStopC := make(chan any, actorsCount) |
| 204 | + onStart := OptOnStart(func(Context) { onStartC <- `🌞` }) |
| 205 | + onStop := OptOnStop(func() { onStopC <- `🌚` }) |
| 206 | + actors := createActors(actorsCount, onStart, onStop) |
| 207 | + |
| 208 | + a := Combine(actors...).WithOptions().Build() |
| 209 | + |
| 210 | + a.Start() |
| 211 | + drainC(onStartC, actorsCount) |
| 212 | + |
| 213 | + // stop actors indiviually, and expect that after some time |
| 214 | + // there wont be any other actors stopping. |
| 215 | + stopCount := i + 1 |
| 216 | + for j := range stopCount { |
| 217 | + actors[j].Stop() |
| 218 | + } |
| 219 | + |
| 220 | + drainC(onStopC, stopCount) |
| 221 | + |
| 222 | + select { |
| 223 | + case <-onStartC: |
| 224 | + t.Fatal("should not have any more stopped actors") |
| 225 | + case <-time.After(time.Millisecond * 20): |
| 226 | + } |
| 227 | + |
| 228 | + a.Stop() |
| 229 | + drainC(onStopC, actorsCount-stopCount) |
| 230 | + } |
| 231 | +} |
| 232 | + |
186 | 233 | func Test_Combine_OptOnStop_AfterActorStops(t *testing.T) { |
187 | 234 | t.Parallel() |
188 | 235 |
|
|
0 commit comments