-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpool_test.go
138 lines (119 loc) · 3.45 KB
/
pool_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
package pool
import (
"context"
"testing"
"time"
"github.com/DoNewsCode/core/events"
"github.com/oklog/run"
)
func TestPool_Go(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithCancel(context.Background())
dispatcher := events.SyncDispatcher{}
p := NewPool(WithConcurrency(1), WithShutdownEvents())(&dispatcher)
go p.Go(context.Background(), func(asyncContext context.Context) {
cancel()
})
p.Run(ctx)
}
func TestPool_Timeout(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond)
defer cancel()
dispatcher := events.SyncDispatcher{}
p := NewPool(WithTimeout(time.Second), WithConcurrency(1), WithShutdownEvents())(&dispatcher)
go p.Go(context.Background(), func(asyncContext context.Context) {
select {
case <-asyncContext.Done():
if asyncContext.Err() == nil {
t.Fatalf("asyncContext should return err")
}
return
case <-time.After(5 * time.Second):
t.Fatal("should have timed out")
}
})
p.Run(ctx)
}
func TestPool_FallbackToSyncMode(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond)
defer cancel()
dispatcher := events.SyncDispatcher{}
p := NewPool(WithTimeout(time.Second), WithConcurrency(1), WithShutdownEvents())(&dispatcher)
p.Run(ctx)
var executed = make(chan struct{})
go func() {
// saturate the pool
p.Go(ctx, func(asyncContext context.Context) {
time.Sleep(time.Second)
})
// fallback to sync mode
p.Go(ctx, func(asyncContext context.Context) {
close(executed)
})
}()
<-executed
}
func TestPool_contextValue(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond)
defer cancel()
dispatcher := events.SyncDispatcher{}
p := NewPool(WithConcurrency(1), WithShutdownEvents())(&dispatcher)
key := struct{}{}
requestContext := context.WithValue(context.Background(), key, "foo")
go p.Go(requestContext, func(asyncContext context.Context) {
if _, ok := asyncContext.Deadline(); !ok {
t.Fatalf("asyncContext should have deadline set")
}
value := asyncContext.Value(key)
if value != "foo" {
t.Fatalf("want foo, got %s", value)
}
})
p.Run(ctx)
}
func TestPool_ProvideRunGroup(t *testing.T) {
t.Parallel()
t.Run("run group should exit if no shutdown event is specified", func(t *testing.T) {
dispatcher := events.SyncDispatcher{}
p := NewPool(WithConcurrency(1), WithShutdownEvents())(&dispatcher)
var group run.Group
group.Add(func() error { return nil }, func(err error) {})
p.ProvideRunGroup(&group)
group.Run()
})
t.Run("run group should wait until all shutdown events", func(t *testing.T) {
dispatcher := events.SyncDispatcher{}
var fooEvent = "fooEvent"
var barEvent = "barEvent"
p := NewPool(WithConcurrency(1), WithShutdownEvents(fooEvent, barEvent))(&dispatcher)
var group run.Group
group.Add(func() error { return nil }, func(err error) {})
p.ProvideRunGroup(&group)
var final = make(chan struct{})
go func() {
group.Run()
final <- struct{}{}
}()
select {
case <-final:
t.Fatal("group run should not exit now")
case <-time.After(time.Second):
}
dispatcher.Dispatch(context.Background(), fooEvent, nil)
select {
case <-final:
t.Fatal("group run should not exit now")
case <-time.After(time.Second):
}
dispatcher.Dispatch(context.Background(), barEvent, nil)
select {
case <-final:
return
case <-time.After(4 * time.Second):
t.Fatal("group should exit by now")
}
})
}