@@ -2,6 +2,7 @@ package parallelisation
22
33import (
44 "context"
5+ "fmt"
56 "testing"
67
78 "github.com/stretchr/testify/assert"
@@ -25,6 +26,16 @@ func TestCloseAll(t *testing.T) {
2526 require .NoError (t , CloseAll (closerMock , closerMock , closerMock ))
2627 })
2728
29+ t .Run ("close and join errors" , func (t * testing.T ) {
30+ ctlr := gomock .NewController (t )
31+ defer ctlr .Finish ()
32+
33+ closerMock := mocks .NewMockCloser (ctlr )
34+ closerMock .EXPECT ().Close ().Return (nil ).MinTimes (1 )
35+
36+ require .NoError (t , CloseAllAndCollateErrors (closerMock , closerMock , closerMock ))
37+ })
38+
2839 t .Run ("close with error" , func (t * testing.T ) {
2940 ctlr := gomock .NewController (t )
3041 defer ctlr .Finish ()
@@ -36,12 +47,29 @@ func TestCloseAll(t *testing.T) {
3647 errortest .AssertError (t , CloseAll (closerMock , closerMock , closerMock ), closeError )
3748 })
3849
50+ t .Run ("close with errors" , func (t * testing.T ) {
51+ ctlr := gomock .NewController (t )
52+ defer ctlr .Finish ()
53+ closeError := commonerrors .ErrUnexpected
54+
55+ closerMock := mocks .NewMockCloser (ctlr )
56+ closerMock .EXPECT ().Close ().Return (closeError ).MinTimes (1 )
57+
58+ errortest .AssertError (t , CloseAllAndCollateErrors (closerMock , closerMock , closerMock ), closeError )
59+ })
60+
3961 t .Run ("close with 1 error" , func (t * testing.T ) {
4062 closeError := commonerrors .ErrUnexpected
4163
4264 errortest .AssertError (t , CloseAllFunc (func () error { return nil }, func () error { return nil }, func () error { return closeError }, func () error { return nil }), closeError )
4365 })
4466
67+ t .Run ("close with 1 error but error collection" , func (t * testing.T ) {
68+ closeError := commonerrors .ErrUnexpected
69+
70+ errortest .AssertError (t , CloseAllFuncAndCollateErrors (func () error { return nil }, func () error { return nil }, func () error { return closeError }, func () error { return nil }), closeError )
71+ })
72+
4573}
4674
4775func TestCancelOnClose (t * testing.T ) {
@@ -98,39 +126,63 @@ func TestCancelOnClose(t *testing.T) {
98126 })
99127}
100128
101- func TestStopOnFirstError (t * testing.T ) {
102- t .Run ("sequentially" , func (t * testing.T ) {
103- closeStore := NewCloseFunctionStore (StopOnFirstError , Sequential )
104- ctx1 , cancel1 := context .WithCancel (context .Background ())
105- closeStore .RegisterCloseFunction (func () error { cancel1 (); return DetermineContextError (ctx1 ) })
106- ctx2 , cancel2 := context .WithCancel (context .Background ())
107- closeStore .RegisterCloseFunction (func () error { cancel2 (); return DetermineContextError (ctx2 ) })
108- ctx3 , cancel3 := context .WithCancel (context .Background ())
109- closeStore .RegisterCloseFunction (func () error { cancel3 (); return DetermineContextError (ctx3 ) })
110- assert .Equal (t , 3 , closeStore .Len ())
111- require .NoError (t , DetermineContextError (ctx1 ))
112- require .NoError (t , DetermineContextError (ctx2 ))
113- require .NoError (t , DetermineContextError (ctx3 ))
114- errortest .AssertError (t , closeStore .Close (), commonerrors .ErrCancelled )
115- errortest .AssertError (t , DetermineContextError (ctx1 ), commonerrors .ErrCancelled )
116- assert .NoError (t , DetermineContextError (ctx2 ))
117- assert .NoError (t , DetermineContextError (ctx3 ))
118- })
119- t .Run ("reverse" , func (t * testing.T ) {
120- closeStore := NewCloseFunctionStore (StopOnFirstError , SequentialInReverse )
121- ctx1 , cancel1 := context .WithCancel (context .Background ())
122- closeStore .RegisterCloseFunction (func () error { cancel1 (); return DetermineContextError (ctx1 ) })
123- ctx2 , cancel2 := context .WithCancel (context .Background ())
124- closeStore .RegisterCloseFunction (func () error { cancel2 (); return DetermineContextError (ctx2 ) })
125- ctx3 , cancel3 := context .WithCancel (context .Background ())
126- closeStore .RegisterCloseFunction (func () error { cancel3 (); return DetermineContextError (ctx3 ) })
127- assert .Equal (t , 3 , closeStore .Len ())
128- require .NoError (t , DetermineContextError (ctx1 ))
129- require .NoError (t , DetermineContextError (ctx2 ))
130- require .NoError (t , DetermineContextError (ctx3 ))
131- errortest .AssertError (t , closeStore .Close (), commonerrors .ErrCancelled )
132- assert .NoError (t , DetermineContextError (ctx1 ))
133- assert .NoError (t , DetermineContextError (ctx2 ))
134- errortest .AssertError (t , DetermineContextError (ctx3 ), commonerrors .ErrCancelled )
135- })
129+ func TestSequentialExecution (t * testing.T ) {
130+ tests := []struct {
131+ option StoreOption
132+ }{
133+ {StopOnFirstError },
134+ {JoinErrors },
135+ }
136+ for i := range tests {
137+ test := tests [i ]
138+ t .Run (fmt .Sprintf ("%v-%#v" , i , test .option ), func (t * testing.T ) {
139+ opt := test .option (& StoreOptions {})
140+ t .Run ("sequentially" , func (t * testing.T ) {
141+ closeStore := NewCloseFunctionStore (test .option , Sequential )
142+ ctx1 , cancel1 := context .WithCancel (context .Background ())
143+ closeStore .RegisterCloseFunction (func () error { cancel1 (); return DetermineContextError (ctx1 ) })
144+ ctx2 , cancel2 := context .WithCancel (context .Background ())
145+ closeStore .RegisterCloseFunction (func () error { cancel2 (); return DetermineContextError (ctx2 ) })
146+ ctx3 , cancel3 := context .WithCancel (context .Background ())
147+ closeStore .RegisterCloseFunction (func () error { cancel3 (); return DetermineContextError (ctx3 ) })
148+ assert .Equal (t , 3 , closeStore .Len ())
149+ require .NoError (t , DetermineContextError (ctx1 ))
150+ require .NoError (t , DetermineContextError (ctx2 ))
151+ require .NoError (t , DetermineContextError (ctx3 ))
152+
153+ errortest .AssertError (t , closeStore .Close (), commonerrors .ErrCancelled )
154+ errortest .AssertError (t , DetermineContextError (ctx1 ), commonerrors .ErrCancelled )
155+ if opt .stopOnFirstError {
156+ assert .NoError (t , DetermineContextError (ctx2 ))
157+ assert .NoError (t , DetermineContextError (ctx3 ))
158+ } else {
159+ errortest .AssertError (t , DetermineContextError (ctx2 ), commonerrors .ErrCancelled )
160+ errortest .AssertError (t , DetermineContextError (ctx3 ), commonerrors .ErrCancelled )
161+ }
162+
163+ })
164+ t .Run ("reverse" , func (t * testing.T ) {
165+ closeStore := NewCloseFunctionStore (test .option , SequentialInReverse )
166+ ctx1 , cancel1 := context .WithCancel (context .Background ())
167+ closeStore .RegisterCloseFunction (func () error { cancel1 (); return DetermineContextError (ctx1 ) })
168+ ctx2 , cancel2 := context .WithCancel (context .Background ())
169+ closeStore .RegisterCloseFunction (func () error { cancel2 (); return DetermineContextError (ctx2 ) })
170+ ctx3 , cancel3 := context .WithCancel (context .Background ())
171+ closeStore .RegisterCloseFunction (func () error { cancel3 (); return DetermineContextError (ctx3 ) })
172+ assert .Equal (t , 3 , closeStore .Len ())
173+ require .NoError (t , DetermineContextError (ctx1 ))
174+ require .NoError (t , DetermineContextError (ctx2 ))
175+ require .NoError (t , DetermineContextError (ctx3 ))
176+ errortest .AssertError (t , closeStore .Close (), commonerrors .ErrCancelled )
177+ if opt .stopOnFirstError {
178+ assert .NoError (t , DetermineContextError (ctx1 ))
179+ assert .NoError (t , DetermineContextError (ctx2 ))
180+ } else {
181+ errortest .AssertError (t , DetermineContextError (ctx1 ), commonerrors .ErrCancelled )
182+ errortest .AssertError (t , DetermineContextError (ctx2 ), commonerrors .ErrCancelled )
183+ }
184+ errortest .AssertError (t , DetermineContextError (ctx3 ), commonerrors .ErrCancelled )
185+ })
186+ })
187+ }
136188}
0 commit comments