-
Notifications
You must be signed in to change notification settings - Fork 0
/
multiblocker_test.go
116 lines (85 loc) · 2.13 KB
/
multiblocker_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
package blockit_test
import (
"context"
"testing"
"time"
"github.com/gomicro/blockit"
"github.com/gomicro/blockit/dbblocker"
"github.com/franela/goblin"
. "github.com/onsi/gomega"
"gopkg.in/DATA-DOG/go-sqlmock.v1"
)
func TestMultiBlocker(t *testing.T) {
g := goblin.Goblin(t)
RegisterFailHandler(func(m string, _ ...int) { g.Fail(m) })
g.Describe("MultiBlocker", func() {
g.Describe("Multiple Blockers", func() {
g.It("should block with several blockers", func() {
b := blockit.MultiBlocker{}
b.Add(&foo{})
b.Add(&bar{})
Eventually(b.Blockit()).Should(Receive())
})
g.It("should block until context is cancelled", func() {
ctx, cancel := context.WithCancel(context.Background())
b := blockit.MultiBlocker{}
b.Add(&never{})
go func() {
<-time.After(1 * time.Millisecond)
cancel()
}()
Eventually(b.BlockitWithContext(ctx)).Should(Receive())
})
})
g.Describe("DB Blocker", func() {
g.It("should work in a multiblocker", func() {
mockDB, _, _ := sqlmock.New()
b := blockit.MultiBlocker{}
b.Add(dbblocker.New(mockDB))
Eventually(b.Blockit()).Should(Receive())
})
})
})
}
type foo struct{}
func (f *foo) Blockit() <-chan struct{} {
return f.BlockitWithContext(context.Background())
}
func (f *foo) BlockitWithContext(ctx context.Context) <-chan struct{} {
out := make(chan struct{})
go func() {
defer close(out)
<-time.After(10 * time.Millisecond)
}()
return out
}
type bar struct{}
func (b *bar) Blockit() <-chan struct{} {
return b.BlockitWithContext(context.Background())
}
func (b *bar) BlockitWithContext(ctx context.Context) <-chan struct{} {
out := make(chan struct{})
go func() {
defer close(out)
<-time.After(30 * time.Millisecond)
}()
return out
}
type never struct{}
func (n *never) Blockit() <-chan struct{} {
return n.BlockitWithContext(context.Background())
}
func (n *never) BlockitWithContext(ctx context.Context) <-chan struct{} {
out := make(chan struct{})
go func() {
defer close(out)
for {
select {
case <-ctx.Done():
return
case <-time.After(2 * time.Second):
}
}
}()
return out
}