-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask_test.go
174 lines (137 loc) · 3.25 KB
/
task_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package task_test
import (
"context"
"errors"
"fmt"
"sync/atomic"
"testing"
"time"
"ella.to/task"
)
func TestTaskRunnerWihtoutError(t *testing.T) {
runner := task.NewRunner(
task.WithBufferSize(10),
task.WithWorkerSize(10),
)
defer runner.Close(context.TODO())
future := runner.Submit(context.TODO(), func(ctx context.Context) error {
return nil
})
if err := future.Await(context.TODO()); err != nil {
t.Fatal(err)
}
}
func TestTaskRunnerWithError(t *testing.T) {
runner := task.NewRunner(
task.WithBufferSize(10),
task.WithWorkerSize(10),
)
defer runner.Close(context.TODO())
future := runner.Submit(context.TODO(), func(ctx context.Context) error {
return fmt.Errorf("error")
})
if err := future.Await(context.TODO()); err == nil {
t.Fatal("expected error, got nil")
} else if err.Error() != "error" {
t.Fatalf("expected error, got %v", err)
}
}
func TestTaskRunnerClosed(t *testing.T) {
runner := task.NewRunner(
task.WithBufferSize(10),
task.WithWorkerSize(10),
)
var count atomic.Int64
future := runner.Submit(context.TODO(), func(ctx context.Context) error {
select {
case <-ctx.Done():
return ctx.Err()
case <-time.After(1 * time.Second):
count.Add(1)
}
return nil
})
runner.Close(context.TODO())
if err := future.Await(context.TODO()); !errors.Is(err, task.ErrRunnerClosed) {
t.Fatalf("expected %v, got %v", context.Canceled, err)
}
if count.Load() > 0 {
t.Fatalf("expected 0, got %v", count.Load())
}
}
func TestTaskRunnerMultiple(t *testing.T) {
runner := task.NewRunner(
task.WithBufferSize(1),
task.WithWorkerSize(10),
)
var count atomic.Int64
for i := 0; i < 100; i++ {
runner.Submit(context.TODO(), func(ctx context.Context) error {
count.Add(1)
return nil
})
}
runner.Close(context.TODO())
if count.Load() != 100 {
t.Fatalf("expected 100, got %v", count.Load())
}
}
func TestYeild(t *testing.T) {
runner := task.NewRunner(
task.WithBufferSize(10),
task.WithWorkerSize(1),
)
count := 0
expectedCount := 1_000_000
err := runner.Submit(context.Background(), func(ctx context.Context) error {
if count < expectedCount {
count++
return task.Yeild(ctx)
}
return nil
}).Await(context.Background())
if err != nil {
t.Fatal(err)
}
if count != expectedCount {
t.Fatalf("expected %d but got %d", expectedCount, count)
}
}
func TestYeildDelay(t *testing.T) {
runner := task.NewRunner(
task.WithBufferSize(10),
task.WithWorkerSize(1),
)
count := 0
err := runner.Submit(context.Background(), func(ctx context.Context) error {
if count < 10 {
count++
fmt.Println("count", count)
return task.Yeild(ctx, task.WithDelay(1*time.Second))
}
return nil
}).Await(context.Background())
if err != nil {
t.Fatal(err)
}
}
func TestYeildMultiple(t *testing.T) {
runner := task.NewRunner(
task.WithBufferSize(10),
task.WithWorkerSize(1),
)
i := 0
j := 0
ctx, cancel := context.WithTimeout(context.Background(), 4*time.Second)
defer cancel()
runner.Submit(ctx, func(ctx context.Context) error {
i += 1
return task.Yeild(ctx, task.WithDelay(1*time.Second))
})
runner.Submit(ctx, func(ctx context.Context) error {
j += 1
return task.Yeild(ctx, task.WithDelay(1*time.Second))
})
<-ctx.Done()
fmt.Printf("i: %d, j: %d\n", i, j)
}