79 lines
1.3 KiB
Go
79 lines
1.3 KiB
Go
package errgroups
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func fn_test1(ctx context.Context) error {
|
|
select {
|
|
case <-ctx.Done():
|
|
fmt.Println("fn_test1 called cancel")
|
|
return ctx.Err()
|
|
case <-time.After(time.Second):
|
|
fmt.Println("fn_test1 called ok")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func fn_test2(ctx context.Context) error {
|
|
fmt.Println("fn_test2 called")
|
|
return nil
|
|
}
|
|
|
|
func fn_test3(ctx context.Context) error {
|
|
fmt.Println("fn_test3 called")
|
|
return errors.New("call fn_test3 get error")
|
|
}
|
|
|
|
type err_test1 struct{}
|
|
|
|
func(e *err_test1)Run(ctx context.Context)error{
|
|
return fn_test1(ctx)
|
|
}
|
|
|
|
type err_test2 struct{}
|
|
|
|
func(e *err_test2)Run(ctx context.Context)error{
|
|
return fn_test2(ctx)
|
|
}
|
|
|
|
type err_test3 struct{}
|
|
|
|
func(e *err_test3)Run(ctx context.Context)error{
|
|
return fn_test3(ctx)
|
|
}
|
|
|
|
func Test_errGroupsFunc(t *testing.T) {
|
|
errgws := NewErrGroupFunc()
|
|
|
|
errgws.Add(fn_test1)
|
|
errgws.Add(fn_test2)
|
|
errgws.Add(fn_test3)
|
|
|
|
err := errgws.Run(context.Background())
|
|
if err != nil {
|
|
t.Errorf("err: %v", err)
|
|
}
|
|
|
|
t.Log("ok")
|
|
}
|
|
|
|
func Test_errGroups(t *testing.T) {
|
|
errgws := NewErrGroup()
|
|
|
|
errgws.Add(&err_test1{})
|
|
errgws.Add(&err_test2{})
|
|
errgws.Add(&err_test3{})
|
|
|
|
err := errgws.Run(context.Background())
|
|
if err != nil {
|
|
t.Errorf("err: %v", err)
|
|
}
|
|
|
|
t.Log("ok")
|
|
}
|