Skip to content

Commit f62ec8d

Browse files
committed
doc: add examples.
1 parent b25e949 commit f62ec8d

File tree

4 files changed

+113
-0
lines changed

4 files changed

+113
-0
lines changed

all_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package async
33
import (
44
"context"
55
"errors"
6+
"fmt"
67
"testing"
78
"time"
89

@@ -109,6 +110,20 @@ func BenchmarkAll(b *testing.B) {
109110
All(tasks...)
110111
}
111112

113+
func ExampleAll() {
114+
out, err := All(func() int {
115+
time.Sleep(100 * time.Millisecond)
116+
return 1
117+
}, func() int {
118+
return 2
119+
})
120+
fmt.Println(out)
121+
fmt.Println(err)
122+
// Output:
123+
// [[1] [2]]
124+
// <nil>
125+
}
126+
112127
func TestAllCompletedWithoutFuncs(t *testing.T) {
113128
a := assert.New(t)
114129

@@ -218,3 +233,17 @@ func BenchmarkAllCompleted(b *testing.B) {
218233

219234
AllCompleted(tasks...)
220235
}
236+
237+
func ExampleAllCompleted() {
238+
out, err := AllCompleted(func() (int, error) {
239+
time.Sleep(100 * time.Millisecond)
240+
return 1, nil
241+
}, func() error {
242+
return errors.New("expected error")
243+
})
244+
fmt.Println(out)
245+
fmt.Println(err)
246+
// Outputs:
247+
// [[1 <nil>] [expected error]]
248+
// function 1 error: expected error
249+
}

forever_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package async
33
import (
44
"context"
55
"errors"
6+
"fmt"
67
"testing"
78

89
"github.com/ghosind/go-assert"
@@ -64,3 +65,22 @@ func TestForeverWithContext(t *testing.T) {
6465
a.EqualNow(i, 5)
6566
a.EqualNow(v, []int{0, 0, 1, 1})
6667
}
68+
69+
func ExampleForever() {
70+
err := Forever(func(ctx context.Context, next func(context.Context)) error {
71+
val := ctx.Value("key")
72+
if val == nil {
73+
//lint:ignore SA1029 for test case only
74+
next(context.WithValue(ctx, "key", 1))
75+
} else if v := val.(int); v < 5 {
76+
//lint:ignore SA1029 for test case only
77+
next(context.WithValue(ctx, "key", v+1))
78+
} else {
79+
return errors.New("value is 5")
80+
}
81+
82+
return nil
83+
})
84+
fmt.Println(err)
85+
// value is 5
86+
}

parallel_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package async
33
import (
44
"context"
55
"errors"
6+
"fmt"
67
"testing"
78
"time"
89

@@ -154,6 +155,27 @@ func BenchmarkParallel(b *testing.B) {
154155
Parallel(5, tasks...)
155156
}
156157

158+
func ExampleParallel() {
159+
start := time.Now()
160+
out, err := Parallel(2, func() int {
161+
time.Sleep(50 * time.Millisecond)
162+
return 1
163+
}, func() int {
164+
time.Sleep(50 * time.Millisecond)
165+
return 2
166+
}, func() int {
167+
time.Sleep(50 * time.Millisecond)
168+
return 3
169+
})
170+
fmt.Println(time.Since(start))
171+
fmt.Println(out)
172+
fmt.Println(err)
173+
// Outputs:
174+
// 101.210974ms
175+
// [[1] [2] [3]]
176+
// <nil>
177+
}
178+
157179
func TestParallelCompleted(t *testing.T) {
158180
a := assert.New(t)
159181

@@ -311,3 +333,24 @@ func BenchmarkParallelCompleted(b *testing.B) {
311333

312334
ParallelCompleted(5, tasks...)
313335
}
336+
337+
func ExampleParallelCompleted() {
338+
start := time.Now()
339+
out, err := ParallelCompleted(2, func() int {
340+
time.Sleep(50 * time.Millisecond)
341+
return 1
342+
}, func() error {
343+
time.Sleep(50 * time.Millisecond)
344+
return errors.New("expected error")
345+
}, func() int {
346+
time.Sleep(50 * time.Millisecond)
347+
return 3
348+
})
349+
fmt.Println(time.Since(start))
350+
fmt.Println(out)
351+
fmt.Println(err)
352+
// Outputs:
353+
// 101.210974ms
354+
// [[1] [expected error] [3]]
355+
// function 1 error: expected error
356+
}

race_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package async
33
import (
44
"context"
55
"errors"
6+
"fmt"
67
"testing"
78
"time"
89

@@ -128,3 +129,23 @@ func BenchmarkRace(b *testing.B) {
128129

129130
Race(tasks...)
130131
}
132+
133+
func ExampleRace() {
134+
start := time.Now()
135+
out, index, err := Race(func() int {
136+
time.Sleep(50 * time.Millisecond)
137+
return 1
138+
}, func() int {
139+
time.Sleep(20 * time.Millisecond)
140+
return 2
141+
})
142+
fmt.Println(time.Since(start))
143+
fmt.Println(out)
144+
fmt.Println(index)
145+
fmt.Println(err)
146+
// Outputs:
147+
// 20.678486ms
148+
// [2]
149+
// 1
150+
// <nil>
151+
}

0 commit comments

Comments
 (0)