@@ -11,21 +11,33 @@ import (
1111func Test_Combine_TestSuite (t * testing.T ) {
1212 t .Parallel ()
1313
14- const actorsCount = 10
14+ TestSuite (t , func () Actor {
15+ actors := createActors (0 )
16+ return Combine (actors ... ).Build ()
17+ })
1518
1619 TestSuite (t , func () Actor {
17- actors := createActors (actorsCount )
20+ actors := createActors (1 )
21+ return Combine (actors ... ).Build ()
22+ })
1823
24+ TestSuite (t , func () Actor {
25+ actors := createActors (10 )
1926 return Combine (actors ... ).Build ()
2027 })
2128}
2229
23- // Test asserts that all Start and Stop is
24- // delegated to all combined actors.
30+ // Test asserts that all Start and Stop is delegated to all combined actors.
2531func Test_Combine (t * testing.T ) {
2632 t .Parallel ()
2733
28- const actorsCount = 5
34+ testCombine (t , 0 )
35+ testCombine (t , 1 )
36+ testCombine (t , 5 )
37+ }
38+
39+ func testCombine (t * testing.T , actorsCount int ) {
40+ t .Helper ()
2941
3042 onStartC := make (chan any , actorsCount )
3143 onStopC := make (chan any , actorsCount )
@@ -46,35 +58,6 @@ func Test_Combine(t *testing.T) {
4658 assert .Len (t , onStopC , actorsCount )
4759}
4860
49- // Test_Combine_OptStopTogether asserts that all actors will end as soon
50- // as first actors ends.
51- func Test_Combine_OptStopTogether (t * testing.T ) {
52- t .Parallel ()
53-
54- const actorsCount = 5 * 2
55-
56- for i := range actorsCount / 2 + 1 {
57- onStartC := make (chan any , actorsCount )
58- onStopC := make (chan any , actorsCount )
59- onStart := OptOnStart (func (Context ) { onStartC <- `🌞` })
60- onStop := OptOnStop (func () { onStopC <- `🌚` })
61- actors := createActors (actorsCount / 2 , onStart , onStop )
62-
63- // append one more actor to actors list
64- cmb := Combine (createActors (actorsCount / 2 , onStart , onStop )... ).Build ()
65- actors = append (actors , cmb )
66-
67- a := Combine (actors ... ).WithOptions (OptStopTogether ()).Build ()
68-
69- a .Start ()
70- drainC (onStartC , actorsCount )
71-
72- // stop actor and assert that all actors will be stopped
73- actors [i ].Stop ()
74- drainC (onStopC , actorsCount )
75- }
76- }
77-
7861func Test_Combine_OptOnStopOptOnStart (t * testing.T ) {
7962 t .Parallel ()
8063
@@ -106,6 +89,40 @@ func testCombineOptOnStopOptOnStart(t *testing.T, count int) {
10689 assert .Empty (t , onStatC )
10790}
10891
92+ // Test_Combine_OptStopTogether asserts that all actors will end as soon
93+ // as first actors ends.
94+ func Test_Combine_OptStopTogether (t * testing.T ) {
95+ t .Parallel ()
96+
97+ // no need to test OptStopTogether for actors count < 2
98+ // because single actor is always "stopped together"
99+
100+ testCombineOptStopTogether (t , 1 )
101+ testCombineOptStopTogether (t , 2 )
102+ testCombineOptStopTogether (t , 10 )
103+ }
104+
105+ func testCombineOptStopTogether (t * testing.T , actorsCount int ) {
106+ t .Helper ()
107+
108+ for i := range actorsCount {
109+ onStartC := make (chan any , actorsCount )
110+ onStopC := make (chan any , actorsCount )
111+ onStart := OptOnStart (func (Context ) { onStartC <- `🌞` })
112+ onStop := OptOnStop (func () { onStopC <- `🌚` })
113+ actors := createActors (actorsCount , onStart , onStop )
114+
115+ a := Combine (actors ... ).WithOptions (OptStopTogether ()).Build ()
116+
117+ a .Start ()
118+ drainC (onStartC , actorsCount )
119+
120+ // stop actor and assert that all actors will be stopped
121+ actors [i ].Stop ()
122+ drainC (onStopC , actorsCount )
123+ }
124+ }
125+
109126func Test_Combine_OptOnStop_AfterActorStops (t * testing.T ) {
110127 t .Parallel ()
111128
0 commit comments