-
Notifications
You must be signed in to change notification settings - Fork 7
/
suite_test.go
204 lines (166 loc) · 4.64 KB
/
suite_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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package gozk_test
import (
"fmt"
"gozk"
"io/ioutil"
. "launchpad.net/gocheck"
"os"
"path"
"testing"
"time"
)
func TestAll(t *testing.T) {
TestingT(t)
}
var _ = Suite(&S{})
type S struct {
zkRoot string
zkTestRoot string
zkTestPort int
zkServerSh string
zkServerOut *os.File
zkAddr string
handles []*gozk.ZooKeeper
events []*gozk.Event
liveWatches int
deadWatches chan bool
}
var logLevel = 0 //gozk.LOG_ERROR
var testZooCfg = ("dataDir=%s\n" +
"clientPort=%d\n" +
"tickTime=2000\n" +
"initLimit=10\n" +
"syncLimit=5\n" +
"")
var testLog4jPrp = ("log4j.rootLogger=INFO,CONSOLE\n" +
"log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender\n" +
"log4j.appender.CONSOLE.Threshold=DEBUG\n" +
"log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout\n" +
"log4j.appender.CONSOLE.layout.ConversionPattern=%d{ISO8601} [myid:%X{myid}] - %-5p [%t:%C{1}@%L] - %m%n" +
"")
func (s *S) init(c *C) (*gozk.ZooKeeper, chan gozk.Event) {
zk, watch, err := gozk.Init(s.zkAddr, 5e9)
c.Assert(err, IsNil)
s.handles = append(s.handles, zk)
event := <-watch
c.Assert(event.Type, Equals, gozk.EVENT_SESSION)
c.Assert(event.State, Equals, gozk.STATE_CONNECTED)
bufferedWatch := make(chan gozk.Event, 256)
bufferedWatch <- event
s.liveWatches += 1
go func() {
loop:
for {
select {
case event, ok := <-watch:
if !ok {
close(bufferedWatch)
break loop
}
select {
case bufferedWatch <- event:
default:
panic("Too many events in buffered watch!")
}
}
}
s.deadWatches <- true
}()
return zk, bufferedWatch
}
func (s *S) SetUpTest(c *C) {
c.Assert(gozk.CountPendingWatches(), Equals, 0,
Bug("Test got a dirty watch state before running!"))
gozk.SetLogLevel(logLevel)
}
func (s *S) TearDownTest(c *C) {
// Close all handles opened in s.init().
for _, handle := range s.handles {
handle.Close()
}
// Wait for all the goroutines created in s.init() to terminate.
for s.liveWatches > 0 {
select {
case <-s.deadWatches:
s.liveWatches -= 1
case <-time.After(5e9):
panic("There's a locked watch goroutine :-(")
}
}
// Reset the list of handles.
s.handles = make([]*gozk.ZooKeeper, 0)
c.Assert(gozk.CountPendingWatches(), Equals, 0,
Bug("Test left live watches behind!"))
}
// We use the suite set up and tear down to manage a custom zookeeper
//
func (s *S) SetUpSuite(c *C) {
s.deadWatches = make(chan bool)
var err error
s.zkRoot = os.Getenv("ZKROOT")
if s.zkRoot == "" {
panic("You must define $ZKROOT ($ZKROOT/bin/zkServer.sh is needed)")
}
s.zkTestRoot = c.MkDir()
s.zkTestPort = 21812
println("ZooKeeper test server directory:", s.zkTestRoot)
println("ZooKeeper test server port:", s.zkTestPort)
s.zkAddr = fmt.Sprintf("localhost:%d", s.zkTestPort)
s.zkServerSh = path.Join(s.zkRoot, "bin/zkServer.sh")
s.zkServerOut, err = os.OpenFile(path.Join(s.zkTestRoot, "stdout.txt"),
os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
if err != nil {
panic("Can't open stdout.txt file for server: " + err.Error())
}
dataDir := path.Join(s.zkTestRoot, "data")
confDir := path.Join(s.zkTestRoot, "conf")
os.Mkdir(dataDir, 0755)
os.Mkdir(confDir, 0755)
err = os.Setenv("ZOOCFGDIR", confDir)
if err != nil {
panic("Can't set $ZOOCFGDIR: " + err.Error())
}
zooCfg := []byte(fmt.Sprintf(testZooCfg, dataDir, s.zkTestPort))
err = ioutil.WriteFile(path.Join(confDir, "zoo.cfg"), zooCfg, 0644)
if err != nil {
panic("Can't write zoo.cfg: " + err.Error())
}
log4jPrp := []byte(testLog4jPrp)
err = ioutil.WriteFile(path.Join(confDir, "log4j.properties"), log4jPrp, 0644)
if err != nil {
panic("Can't write log4j.properties: " + err.Error())
}
s.StartZK()
}
func (s *S) TearDownSuite(c *C) {
s.StopZK()
s.zkServerOut.Close()
}
func (s *S) StartZK() {
attr := os.ProcAttr{Files: []*os.File{os.Stdin, s.zkServerOut, os.Stderr}}
proc, err := os.StartProcess(s.zkServerSh, []string{s.zkServerSh, "start"}, &attr)
if err != nil {
panic("Problem executing zkServer.sh start: " + err.Error())
}
result, err := proc.Wait(0)
if err != nil {
panic(err.Error())
} else if result.ExitStatus() != 0 {
panic("'zkServer.sh start' exited with non-zero status")
}
}
func (s *S) StopZK() {
attr := os.ProcAttr{Files: []*os.File{os.Stdin, s.zkServerOut, os.Stderr}}
proc, err := os.StartProcess(s.zkServerSh, []string{s.zkServerSh, "stop"}, &attr)
if err != nil {
panic("Problem executing zkServer.sh stop: " + err.Error() +
" (look for runaway java processes!)")
}
result, err := proc.Wait(0)
if err != nil {
panic(err.Error())
} else if result.ExitStatus() != 0 {
panic("'zkServer.sh stop' exited with non-zero status " +
"(look for runaway java processes!)")
}
}