-
Notifications
You must be signed in to change notification settings - Fork 9
/
chanGroup_test.go
111 lines (96 loc) · 2.45 KB
/
chanGroup_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
package hotload
import (
"context"
"database/sql/driver"
"sync"
"github.com/infobloxopen/hotload/logger"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
type testConn struct {
closed bool
}
func (tc *testConn) Open(name string) (driver.Conn, error) {
return tc, nil
}
func (tc *testConn) Prepare(query string) (driver.Stmt, error) {
return nil, nil
}
func (tc *testConn) Begin() (driver.Tx, error) {
return nil, nil
}
func (tc *testConn) Close() error {
tc.closed = true
return nil
}
var _ = Describe("Driver", func() {
var pctx context.Context
var ctx context.Context
var cg *chanGroup
var conns []*managedConn
var values chan string
Context("chanGroup", func() {
BeforeEach(func() {
var cancel context.CancelFunc
values = make(chan string)
pctx = context.Background()
ctx, cancel = context.WithCancel(pctx)
conns = []*managedConn{{ctx: ctx, conn: nil}, {ctx: ctx, conn: nil}, {ctx: ctx, conn: nil}}
cg = &chanGroup{
value: "",
values: values,
parentCtx: pctx,
ctx: ctx,
cancel: cancel,
sqlDriver: nil,
mu: sync.RWMutex{},
conns: conns,
log: logger.DefaultLogger,
}
})
It("Should change value when a value is pushed to the values channel", func() {
newVal := "new DSN"
go cg.run()
values <- newVal
cg.mu.RLock()
defer cg.mu.RUnlock()
Expect(cg.value).To(Equal(newVal))
})
It("Should not reset conns when the same value is pushed to the values channel", func() {
sameVal := ""
go cg.run()
values <- sameVal
Expect(cg.value).To(Equal(sameVal))
for _, c := range cg.conns {
Expect(c.reset).To(BeFalse())
}
})
It("Should change value and reset connections", func() {
newVal := "new DSN"
cg.valueChanged(newVal)
Expect(cg.value).To(Equal(newVal))
})
It("Should mark all connections for reset", func() {
cg.resetConnections()
for _, c := range conns {
Expect(c.reset).To(BeTrue())
}
})
It("Should kill all connections when specified", func() {
cg.forceKill = true
testConns := make([]*testConn, 0)
for _, c := range cg.conns {
tc := &testConn{}
c.conn = tc
testConns = append(testConns, tc)
}
cg.resetConnections()
for _, c := range conns {
Expect(c.killed).To(BeTrue(), "connection should be marked killed")
}
for _, tc := range testConns {
Expect(tc.closed).To(BeTrue(), "Closed() should have been called on the underlying connection")
}
})
})
})