Skip to content

Commit 45aa3c2

Browse files
committed
feat: check nil context.
1 parent ce0a501 commit 45aa3c2

File tree

4 files changed

+46
-0
lines changed

4 files changed

+46
-0
lines changed

all.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ func all(parent context.Context, funcs ...func(context.Context) error) error {
1919
return nil
2020
}
2121

22+
if parent == nil {
23+
parent = context.Background()
24+
}
25+
2226
ctx, canFunc := context.WithCancel(parent)
2327
errCh := make(chan error)
2428
retCh := make(chan struct{}, len(funcs))
@@ -82,6 +86,10 @@ func allCompleted(parent context.Context, funcs ...func(context.Context) error)
8286
return
8387
}
8488

89+
if parent == nil {
90+
parent = context.Background()
91+
}
92+
8593
wg := sync.WaitGroup{}
8694
wg.Add(len(funcs))
8795

all_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,17 @@ func TestAllFailure(t *testing.T) {
5858
a.EqualNow(data, []bool{true, true, false, false, false})
5959
}
6060

61+
func TestAllWithNilContext(t *testing.T) {
62+
a := assert.New(t)
63+
64+
//lint:ignore SA1012 for test case only
65+
err := AllWithContext(nil, func(ctx context.Context) error {
66+
time.Sleep(100 * time.Millisecond)
67+
return nil
68+
})
69+
a.NilNow(err)
70+
}
71+
6172
func TestAllWithTimeoutContext(t *testing.T) {
6273
a := assert.New(t)
6374

@@ -134,6 +145,18 @@ func TestAllCompletedPartialFailure(t *testing.T) {
134145
a.EqualNow(errs, []error{nil, nil, errNIs2, nil, nil})
135146
}
136147

148+
func TestAllCompletedWithNilContext(t *testing.T) {
149+
a := assert.New(t)
150+
151+
//lint:ignore SA1012 for test case only
152+
errs, hasError := AllCompletedWithContext(nil, func(ctx context.Context) error {
153+
time.Sleep(100 * time.Millisecond)
154+
return nil
155+
})
156+
a.NotTrueNow(hasError)
157+
a.EqualNow(errs, []error{nil})
158+
}
159+
137160
func TestAllCompletedWithTimeoutContext(t *testing.T) {
138161
a := assert.New(t)
139162

race.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ func race(ctx context.Context, funcs ...func(context.Context) error) error {
1818
return nil
1919
}
2020

21+
if ctx == nil {
22+
ctx = context.Background()
23+
}
24+
2125
finished := atomic.Bool{}
2226
ch := make(chan error)
2327
defer close(ch)

race_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,17 @@ func TestRace(t *testing.T) {
3838
a.EqualNow(data, []bool{true, true, true, true, true})
3939
}
4040

41+
func TestRaceWithNilContext(t *testing.T) {
42+
a := assert.New(t)
43+
44+
//lint:ignore SA1012 for test case only
45+
err := RaceWithContext(nil, func(ctx context.Context) error {
46+
time.Sleep(100 * time.Millisecond)
47+
return nil
48+
})
49+
a.NilNow(err)
50+
}
51+
4152
func TestRaceWithContext(t *testing.T) {
4253
a := assert.New(t)
4354

0 commit comments

Comments
 (0)