Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(go): fixed deepsource issues #3

Merged
merged 1 commit into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (code_refinement): Changing the order of return types for the Wait, WaitAny, and WaitN methods improves consistency with Go's conventional error handling, where the error is typically the last return value. This change enhances readability and aligns with common Go practices.

Suggested change
Wait(context.Context) ([]T, error, []error)
// Wait waits for all tasks to be completed. It returns a slice of results, a slice of errors for individual tasks that failed, and an error if the waiting process itself encounters an error.
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() },
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (code_refinement): Simplifying the ctx field in test cases by directly assigning context.Background instead of wrapping it in a function enhances clarity and reduces unnecessary complexity. This change makes the test setup more straightforward.

Suggested change
ctx: func() context.Context { return context.Background() },
ctx: 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
Loading