-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwait_group_listener_test.go
95 lines (71 loc) · 2.22 KB
/
wait_group_listener_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
package listeners
import (
"sync"
"testing"
"github.com/smarty/assertions/should"
"github.com/smarty/gunit"
)
func TestWaitGroupListenerFixture(t *testing.T) {
gunit.Run(new(WaitGroupListenerFixture), t)
}
type WaitGroupListenerFixture struct {
*gunit.Fixture
inner *FakeForWaitGroupListener
waiter *WrappedWaitGroup
listener *WaitGroupListener
}
func (this *WaitGroupListenerFixture) Setup() {
this.waiter = NewWrappedWaitGroup()
this.inner = &FakeForWaitGroupListener{}
this.listener = NewWaitGroupListener(this.inner, this.waiter)
}
func (this *WaitGroupListenerFixture) TestWaitGroupListenerCallsDone() {
this.listener.Listen()
this.waiter.Wait() // This ensures that .Add(1) and .Done() were called.
this.So(this.waiter.added, should.BeTrue)
this.So(this.waiter.done, should.BeTrue)
this.So(this.inner.listenCalls, should.Equal, 1)
}
////////////////////////////////////////////////////////////////////////////////
func (this *WaitGroupListenerFixture) TestNilInnerListener() {
this.listener = NewWaitGroupListener(nil, this.waiter)
this.So(this.listener.Listen, should.NotPanic)
this.So(this.listener.Close, should.NotPanic)
}
////////////////////////////////////////////////////////////////////////////////
func (this *WaitGroupListenerFixture) TestCloseCallsInner() {
this.listener.Close()
this.So(this.inner.closeCalls, should.Equal, 1)
}
////////////////////////////////////////////////////////////////////////////////
type FakeForWaitGroupListener struct {
listenCalls int
closeCalls int
}
func (this *FakeForWaitGroupListener) Listen() {
this.listenCalls++
}
func (this *FakeForWaitGroupListener) Close() {
this.closeCalls++
}
////////////////////////////////////////////////////////////////////////////////
type WrappedWaitGroup struct {
inner *sync.WaitGroup
added bool
done bool
}
func NewWrappedWaitGroup() *WrappedWaitGroup {
return &WrappedWaitGroup{inner: new(sync.WaitGroup)}
}
func (this *WrappedWaitGroup) Add(delta int) {
this.added = true
this.inner.Add(delta)
}
func (this *WrappedWaitGroup) Done() {
this.done = true
this.inner.Done()
}
func (this *WrappedWaitGroup) Wait() {
this.inner.Wait()
}
////////////////////////////////////////////////////////////////////////////////