1- package async
1+ package async_test
22
33import (
44 "context"
@@ -8,12 +8,13 @@ import (
88 "time"
99
1010 "github.com/ghosind/go-assert"
11+ "github.com/ghosind/go-async"
1112)
1213
1314func TestAllWithoutFuncs (t * testing.T ) {
1415 a := assert .New (t )
1516
16- out , err := All ()
17+ out , err := async . All ()
1718 a .NilNow (err )
1819 a .NilNow (out )
1920}
@@ -22,7 +23,7 @@ func TestAllSuccess(t *testing.T) {
2223 a := assert .New (t )
2324
2425 data := make ([]bool , 5 )
25- funcs := make ([]AsyncFn , 0 , 5 )
26+ funcs := make ([]async. AsyncFn , 0 , 5 )
2627 for i := 0 ; i < 5 ; i ++ {
2728 n := i
2829 funcs = append (funcs , func (ctx context.Context ) (int , error ) {
@@ -32,7 +33,7 @@ func TestAllSuccess(t *testing.T) {
3233 })
3334 }
3435
35- out , err := All (funcs ... )
36+ out , err := async . All (funcs ... )
3637 a .NilNow (err )
3738 a .EqualNow (data , []bool {true , true , true , true , true })
3839 a .EqualNow (out , [][]any {{0 , nil }, {1 , nil }, {2 , nil }, {3 , nil }, {4 , nil }})
@@ -43,7 +44,7 @@ func TestAllFailure(t *testing.T) {
4344 expectedErr := errors .New ("n = 2" )
4445
4546 data := make ([]bool , 5 )
46- funcs := make ([]AsyncFn , 0 , 5 )
47+ funcs := make ([]async. AsyncFn , 0 , 5 )
4748 for i := 0 ; i < 5 ; i ++ {
4849 n := i
4950 funcs = append (funcs , func (ctx context.Context ) error {
@@ -56,7 +57,7 @@ func TestAllFailure(t *testing.T) {
5657 })
5758 }
5859
59- out , err := All (funcs ... )
60+ out , err := async . All (funcs ... )
6061 a .NotNilNow (err )
6162 a .EqualNow (err .Error (), "function 2 error: n = 2" )
6263 a .EqualNow (data , []bool {true , true , false , false , false })
@@ -67,7 +68,7 @@ func TestAllWithNilContext(t *testing.T) {
6768 a := assert .New (t )
6869
6970 //lint:ignore SA1012 for test case only
70- out , err := AllWithContext (nil , func (ctx context.Context ) error {
71+ out , err := async . AllWithContext (nil , func (ctx context.Context ) error {
7172 time .Sleep (100 * time .Millisecond )
7273 return nil
7374 })
@@ -79,7 +80,7 @@ func TestAllWithTimeoutContext(t *testing.T) {
7980 a := assert .New (t )
8081
8182 data := make ([]bool , 5 )
82- funcs := make ([]AsyncFn , 0 , 5 )
83+ funcs := make ([]async. AsyncFn , 0 , 5 )
8384 for i := 0 ; i < 5 ; i ++ {
8485 n := i
8586 funcs = append (funcs , func (ctx context.Context ) error {
@@ -92,26 +93,26 @@ func TestAllWithTimeoutContext(t *testing.T) {
9293 ctx , canFunc := context .WithTimeout (context .Background (), 150 * time .Millisecond )
9394 defer canFunc ()
9495
95- out , err := AllWithContext (ctx , funcs ... )
96+ out , err := async . AllWithContext (ctx , funcs ... )
9697 a .NotNilNow (err )
97- a .TrueNow (errors .Is (err , ErrContextCanceled ))
98+ a .TrueNow (errors .Is (err , async . ErrContextCanceled ))
9899 a .EqualNow (data , []bool {true , true , false , false , false })
99100 a .EqualNow (out , [][]any {{nil }, {nil }, nil , nil , nil })
100101}
101102
102103func BenchmarkAll (b * testing.B ) {
103- tasks := make ([]AsyncFn , 0 , 1000 )
104+ tasks := make ([]async. AsyncFn , 0 , 1000 )
104105 for i := 0 ; i < 1000 ; i ++ {
105106 tasks = append (tasks , func (ctx context.Context ) error {
106107 return nil
107108 })
108109 }
109110
110- All (tasks ... )
111+ async . All (tasks ... )
111112}
112113
113114func ExampleAll () {
114- out , err := All (func () int {
115+ out , err := async . All (func () int {
115116 time .Sleep (100 * time .Millisecond )
116117 return 1
117118 }, func () int {
@@ -127,7 +128,7 @@ func ExampleAll() {
127128func TestAllCompletedWithoutFuncs (t * testing.T ) {
128129 a := assert .New (t )
129130
130- out , err := AllCompleted ()
131+ out , err := async . AllCompleted ()
131132 a .NilNow (err )
132133 a .EqualNow (out , [][]any {})
133134}
@@ -136,7 +137,7 @@ func TestAllCompletedSuccess(t *testing.T) {
136137 a := assert .New (t )
137138
138139 data := make ([]bool , 5 )
139- funcs := make ([]AsyncFn , 0 , 5 )
140+ funcs := make ([]async. AsyncFn , 0 , 5 )
140141 for i := 0 ; i < 5 ; i ++ {
141142 n := i
142143 funcs = append (funcs , func (ctx context.Context ) (int , error ) {
@@ -146,7 +147,7 @@ func TestAllCompletedSuccess(t *testing.T) {
146147 })
147148 }
148149
149- out , err := AllCompleted (funcs ... )
150+ out , err := async . AllCompleted (funcs ... )
150151 a .NilNow (err )
151152 a .EqualNow (data , []bool {true , true , true , true , true })
152153 a .EqualNow (out , [][]any {{0 , nil }, {1 , nil }, {2 , nil }, {3 , nil }, {4 , nil }})
@@ -158,7 +159,7 @@ func TestAllCompletedPartialFailure(t *testing.T) {
158159 errNIs2 := errors .New ("n = 2" )
159160
160161 data := make ([]bool , 5 )
161- funcs := make ([]AsyncFn , 0 , 5 )
162+ funcs := make ([]async. AsyncFn , 0 , 5 )
162163 for i := 0 ; i < 5 ; i ++ {
163164 n := i
164165 funcs = append (funcs , func (ctx context.Context ) (int , error ) {
@@ -171,7 +172,7 @@ func TestAllCompletedPartialFailure(t *testing.T) {
171172 })
172173 }
173174
174- out , err := AllCompleted (funcs ... )
175+ out , err := async . AllCompleted (funcs ... )
175176 a .NotNilNow (err )
176177 a .EqualNow (err .Error (), "function 2 error: n = 2" )
177178 a .EqualNow (data , []bool {true , true , false , true , true })
@@ -182,7 +183,7 @@ func TestAllCompletedWithNilContext(t *testing.T) {
182183 a := assert .New (t )
183184
184185 //lint:ignore SA1012 for test case only
185- out , err := AllCompletedWithContext (nil , func (ctx context.Context ) error {
186+ out , err := async . AllCompletedWithContext (nil , func (ctx context.Context ) error {
186187 time .Sleep (100 * time .Millisecond )
187188 return nil
188189 })
@@ -196,7 +197,7 @@ func TestAllCompletedWithTimeoutContext(t *testing.T) {
196197 errTimeout := errors .New ("timeout" )
197198
198199 data := make ([]bool , 5 )
199- funcs := make ([]AsyncFn , 0 , 5 )
200+ funcs := make ([]async. AsyncFn , 0 , 5 )
200201 for i := 0 ; i < 5 ; i ++ {
201202 n := i
202203 funcs = append (funcs , func (ctx context.Context ) (int , error ) {
@@ -214,7 +215,7 @@ func TestAllCompletedWithTimeoutContext(t *testing.T) {
214215 ctx , canFunc := context .WithTimeout (context .Background (), 150 * time .Millisecond )
215216 defer canFunc ()
216217
217- out , err := AllCompletedWithContext (ctx , funcs ... )
218+ out , err := async . AllCompletedWithContext (ctx , funcs ... )
218219 a .NotNilNow (err )
219220 a .EqualNow (err .Error (), `function 2 error: timeout
220221function 3 error: timeout
@@ -224,18 +225,18 @@ function 4 error: timeout`)
224225}
225226
226227func BenchmarkAllCompleted (b * testing.B ) {
227- tasks := make ([]AsyncFn , 0 , 1000 )
228+ tasks := make ([]async. AsyncFn , 0 , 1000 )
228229 for i := 0 ; i < 1000 ; i ++ {
229230 tasks = append (tasks , func (ctx context.Context ) error {
230231 return nil
231232 })
232233 }
233234
234- AllCompleted (tasks ... )
235+ async . AllCompleted (tasks ... )
235236}
236237
237238func ExampleAllCompleted () {
238- out , err := AllCompleted (func () (int , error ) {
239+ out , err := async . AllCompleted (func () (int , error ) {
239240 time .Sleep (100 * time .Millisecond )
240241 return 1 , nil
241242 }, func () error {
0 commit comments