@@ -58,7 +58,7 @@ func TestAllFailure(t *testing.T) {
5858 a .EqualNow (data , []bool {true , true , false , false , false })
5959}
6060
61- func TestAllWithTimeoutedContext (t * testing.T ) {
61+ func TestAllWithTimeoutContext (t * testing.T ) {
6262 a := assert .New (t )
6363
6464 data := make ([]bool , 5 )
@@ -80,3 +80,86 @@ func TestAllWithTimeoutedContext(t *testing.T) {
8080 a .Equal (err .Error (), "context canceled" )
8181 a .EqualNow (data , []bool {true , true , false , false , false })
8282}
83+
84+ func TestAllCompletedWithoutFuncs (t * testing.T ) {
85+ a := assert .New (t )
86+
87+ errs , hasError := AllCompleted ()
88+ a .NotTrueNow (hasError )
89+ a .EqualNow (errs , []error {})
90+ }
91+
92+ func TestAllCompletedSuccess (t * testing.T ) {
93+ a := assert .New (t )
94+
95+ data := make ([]bool , 5 )
96+ funcs := make ([]func (context.Context ) error , 0 , 5 )
97+ for i := 0 ; i < 5 ; i ++ {
98+ n := i
99+ funcs = append (funcs , func (ctx context.Context ) error {
100+ time .Sleep (time .Duration (n * 100 ) * time .Millisecond )
101+ data [n ] = true
102+ return nil
103+ })
104+ }
105+
106+ errs , hasError := AllCompleted (funcs ... )
107+ a .NotTrueNow (hasError )
108+ a .EqualNow (data , []bool {true , true , true , true , true })
109+ a .EqualNow (errs , []error {nil , nil , nil , nil , nil })
110+ }
111+
112+ func TestAllCompletedPartialFailure (t * testing.T ) {
113+ a := assert .New (t )
114+
115+ errNIs2 := errors .New ("n = 2" )
116+
117+ data := make ([]bool , 5 )
118+ funcs := make ([]func (context.Context ) error , 0 , 5 )
119+ for i := 0 ; i < 5 ; i ++ {
120+ n := i
121+ funcs = append (funcs , func (ctx context.Context ) error {
122+ time .Sleep (time .Duration (n * 100 ) * time .Millisecond )
123+ if n == 2 {
124+ return errNIs2
125+ }
126+ data [n ] = true
127+ return nil
128+ })
129+ }
130+
131+ errs , hasError := AllCompleted (funcs ... )
132+ a .TrueNow (hasError )
133+ a .EqualNow (data , []bool {true , true , false , true , true })
134+ a .EqualNow (errs , []error {nil , nil , errNIs2 , nil , nil })
135+ }
136+
137+ func TestAllCompletedWithTimeoutContext (t * testing.T ) {
138+ a := assert .New (t )
139+
140+ errTimeout := errors .New ("timeout" )
141+
142+ data := make ([]bool , 5 )
143+ funcs := make ([]func (context.Context ) error , 0 , 5 )
144+ for i := 0 ; i < 5 ; i ++ {
145+ n := i
146+ funcs = append (funcs , func (ctx context.Context ) error {
147+ time .Sleep (time .Duration (n * 100 ) * time .Millisecond )
148+ select {
149+ case <- ctx .Done ():
150+ return errTimeout
151+ default :
152+ data [n ] = true
153+ return nil
154+ }
155+ })
156+ }
157+
158+ ctx , canFunc := context .WithTimeout (context .Background (), 150 * time .Millisecond )
159+ defer canFunc ()
160+
161+ errs , hasError := AllCompletedWithContext (ctx , funcs ... )
162+ a .TrueNow (hasError )
163+ a .EqualNow (data , []bool {true , true , false , false , false })
164+ a .EqualNow (errs , []error {nil , nil , errTimeout , errTimeout , errTimeout })
165+ }
0 commit comments