Skip to content

Commit

Permalink
chore(go): fixed deepsource issues
Browse files Browse the repository at this point in the history
  • Loading branch information
cnlangzi committed Mar 12, 2024
1 parent 865a3e5 commit 42cf979
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 45 deletions.
24 changes: 12 additions & 12 deletions awaiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ type Awaiter[T any] interface {
// Add add a task
Add(task func(context.Context) (T, error))
// Wait wail for all tasks to completed
Wait(context.Context) ([]T, error, []error)
Wait(context.Context) ([]T, []error, error)
// WaitAny wait for any task to completed without error, can cancel other tasks
WaitAny(context.Context) (T, error, []error)
WaitAny(context.Context) (T, []error, error)
// WaitN wait for N tasks to completed without error
WaitN(context.Context, int) ([]T, error, []error)
WaitN(context.Context, int) ([]T, []error, error)
}

type awaiter[T any] struct {
Expand All @@ -23,7 +23,7 @@ func (a *awaiter[T]) Add(task func(ctx context.Context) (T, error)) {
a.tasks = append(a.tasks, task)
}

func (a *awaiter[T]) Wait(ctx context.Context) ([]T, error, []error) {
func (a *awaiter[T]) Wait(ctx context.Context) ([]T, []error, error) {
wait := make(chan Result[T])

for _, task := range a.tasks {
Expand All @@ -50,18 +50,18 @@ func (a *awaiter[T]) Wait(ctx context.Context) ([]T, error, []error) {
items = append(items, r.Data)
}
case <-ctx.Done():
return items, ctx.Err(), taskErrs
return items, taskErrs, ctx.Err()
}
}

if len(items) == tt {
return items, nil, taskErrs
return items, taskErrs, nil
}

return items, ErrTooLessDone, taskErrs
return items, taskErrs, ErrTooLessDone
}

func (a *awaiter[T]) WaitN(ctx context.Context, n int) ([]T, error, []error) {
func (a *awaiter[T]) WaitN(ctx context.Context, n int) ([]T, []error, error) {
wait := make(chan Result[T])

cancelCtx, cancel := context.WithCancel(ctx)
Expand Down Expand Up @@ -91,19 +91,19 @@ func (a *awaiter[T]) WaitN(ctx context.Context, n int) ([]T, error, []error) {
items = append(items, r.Data)
done++
if done == n {
return items, nil, taskErrs
return items, taskErrs, nil
}
}
case <-ctx.Done():
return items, ctx.Err(), taskErrs
return items, taskErrs, ctx.Err()
}

}

return items, ErrTooLessDone, taskErrs
return items, taskErrs, ErrTooLessDone
}

func (a *awaiter[T]) WaitAny(ctx context.Context) (T, error, []error) {
func (a *awaiter[T]) WaitAny(ctx context.Context) (T, []error, error) {
var t T
result, err, taskErrs := a.WaitN(ctx, 1)

Expand Down
54 changes: 21 additions & 33 deletions awaiter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func TestWait(t *testing.T) {
}{
{
name: "wait_should_work",
ctx: func() context.Context { return context.Background() },
ctx: context.Background,
setup: func() Awaiter[int] {
a := New[int](func(ctx context.Context) (int, error) {
return 1, nil
Expand All @@ -45,7 +45,7 @@ func TestWait(t *testing.T) {
},
{
name: "error_should_work",
ctx: func() context.Context { return context.Background() },
ctx: context.Background,
setup: func() Awaiter[int] {
return New[int](func(ctx context.Context) (int, error) {
return 1, nil
Expand All @@ -61,7 +61,7 @@ func TestWait(t *testing.T) {
},
{
name: "errors_should_work",
ctx: func() context.Context { return context.Background() },
ctx: context.Background,
setup: func() Awaiter[int] {
return New[int](func(ctx context.Context) (int, error) {
return 0, wantedErr
Expand Down Expand Up @@ -96,9 +96,7 @@ func TestWait(t *testing.T) {
},
{
name: "cancel_should_work",
ctx: func() context.Context {
return context.Background()
},
ctx: context.Background,
setup: func() Awaiter[int] {
return New[int](func(ctx context.Context) (int, error) {
time.Sleep(5 * time.Second)
Expand Down Expand Up @@ -129,9 +127,9 @@ func TestWait(t *testing.T) {
time.Sleep(1 * time.Second)
cancel()
}()
result, err, taskErrs = a.Wait(ctx)
result, taskErrs, err = a.Wait(ctx)
} else {
result, err, taskErrs = a.Wait(test.ctx())
result, taskErrs, err = a.Wait(test.ctx())
}

slices.Sort(result)
Expand Down Expand Up @@ -160,9 +158,7 @@ func TestWaitAny(t *testing.T) {
}{
{
name: "1st_should_work",
ctx: func() context.Context {
return context.Background()
},
ctx: context.Background,
setup: func() Awaiter[int] {
a := New[int](func(ctx context.Context) (int, error) {
time.Sleep(5 * time.Second)
Expand All @@ -182,9 +178,7 @@ func TestWaitAny(t *testing.T) {
},
{
name: "2nd_should_work",
ctx: func() context.Context {
return context.Background()
},
ctx: context.Background,
setup: func() Awaiter[int] {
return New[int](func(ctx context.Context) (int, error) {
time.Sleep(5 * time.Second)
Expand All @@ -200,9 +194,7 @@ func TestWaitAny(t *testing.T) {
},
{
name: "3rd_should_work",
ctx: func() context.Context {
return context.Background()
},
ctx: context.Background,
setup: func() Awaiter[int] {
return New[int](func(ctx context.Context) (int, error) {
time.Sleep(5 * time.Second)
Expand All @@ -219,7 +211,7 @@ func TestWaitAny(t *testing.T) {
},
{
name: "slowest_should_work",
ctx: func() context.Context { return context.Background() },
ctx: context.Background,
setup: func() Awaiter[int] {
return New[int](func(ctx context.Context) (int, error) {
time.Sleep(3 * time.Second)
Expand All @@ -235,7 +227,7 @@ func TestWaitAny(t *testing.T) {
},
{
name: "fastest_should_work",
ctx: func() context.Context { return context.Background() },
ctx: context.Background,
setup: func() Awaiter[int] {
return New[int](func(ctx context.Context) (int, error) {
return 1, nil
Expand All @@ -251,7 +243,7 @@ func TestWaitAny(t *testing.T) {
},
{
name: "errors_should_work",
ctx: func() context.Context { return context.Background() },
ctx: context.Background,
setup: func() Awaiter[int] {
return New[int](func(ctx context.Context) (int, error) {
return 0, wantedErr
Expand All @@ -266,7 +258,7 @@ func TestWaitAny(t *testing.T) {
},
{
name: "error_should_work",
ctx: func() context.Context { return context.Background() },
ctx: context.Background,
setup: func() Awaiter[int] {
return New[int](func(ctx context.Context) (int, error) {
return 0, wantedErr
Expand Down Expand Up @@ -297,9 +289,7 @@ func TestWaitAny(t *testing.T) {
},
{
name: "cancel_should_work",
ctx: func() context.Context {
return context.Background()
},
ctx: context.Background,
setup: func() Awaiter[int] {
return New[int](func(ctx context.Context) (int, error) {
time.Sleep(5 * time.Second)
Expand Down Expand Up @@ -330,9 +320,9 @@ func TestWaitAny(t *testing.T) {
time.Sleep(1 * time.Second)
cancel()
}()
result, err, taskErrs = a.WaitAny(ctx)
result, taskErrs, err = a.WaitAny(ctx)
} else {
result, err, taskErrs = a.WaitAny(test.ctx())
result, taskErrs, err = a.WaitAny(test.ctx())
}

require.Equal(t, test.wantedResult, result)
Expand All @@ -359,7 +349,7 @@ func TestWaitN(t *testing.T) {
}{
{
name: "wait_n_should_work",
ctx: func() context.Context { return context.Background() },
ctx: context.Background,
setup: func() Awaiter[int] {
a := New[int](func(ctx context.Context) (int, error) {
return 1, nil
Expand All @@ -379,7 +369,7 @@ func TestWaitN(t *testing.T) {
},
{
name: "error_n_should_work",
ctx: func() context.Context { return context.Background() },
ctx: context.Background,
setup: func() Awaiter[int] {
return New[int](func(ctx context.Context) (int, error) {
time.Sleep(1 * time.Second)
Expand Down Expand Up @@ -417,9 +407,7 @@ func TestWaitN(t *testing.T) {
},
{
name: "cancel_should_work",
ctx: func() context.Context {
return context.Background()
},
ctx: context.Background,
setup: func() Awaiter[int] {
return New[int](func(ctx context.Context) (int, error) {
time.Sleep(5 * time.Second)
Expand Down Expand Up @@ -450,9 +438,9 @@ func TestWaitN(t *testing.T) {
time.Sleep(1 * time.Second)
cancel()
}()
result, err, taskErrs = a.WaitN(ctx, test.wantedN)
result, taskErrs, err = a.WaitN(ctx, test.wantedN)
} else {
result, err, taskErrs = a.WaitN(test.ctx(), test.wantedN)
result, taskErrs, err = a.WaitN(test.ctx(), test.wantedN)
}

slices.Sort(result)
Expand Down

0 comments on commit 42cf979

Please sign in to comment.