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 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
|
package zk
import (
"net"
"testing"
"time"
)
var (
zkSrvrOut = `Zookeeper version: 3.4.6-1569965, built on 02/20/2014 09:09 GMT
Latency min/avg/max: 0/1/10
Received: 4207
Sent: 4220
Connections: 81
Outstanding: 1
Zxid: 0x110a7a8f37
Mode: leader
Node count: 306
`
zkConsOut = ` /10.42.45.231:45361[1](queued=0,recved=9435,sent=9457,sid=0x94c2989e04716b5,lop=PING,est=1427238717217,to=20001,lcxid=0x55120915,lzxid=0xffffffffffffffff,lresp=1427259255908,llat=0,minlat=0,avglat=1,maxlat=17)
/10.55.33.98:34342[1](queued=0,recved=9338,sent=9350,sid=0x94c2989e0471731,lop=PING,est=1427238849319,to=20001,lcxid=0x55120944,lzxid=0xffffffffffffffff,lresp=1427259252294,llat=0,minlat=0,avglat=1,maxlat=18)
/10.44.145.114:46556[1](queued=0,recved=109253,sent=109617,sid=0x94c2989e0471709,lop=DELE,est=1427238791305,to=20001,lcxid=0x55139618,lzxid=0x110a7b187d,lresp=1427259257423,llat=2,minlat=0,avglat=1,maxlat=23)
`
)
func TestFLWRuok(t *testing.T) {
t.Parallel()
l, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatal(err)
}
defer l.Close()
go tcpServer(l, "")
oks := FLWRuok([]string{l.Addr().String()}, time.Second*10)
if len(oks) == 0 {
t.Errorf("no values returned")
}
if !oks[0] {
t.Errorf("instance should be marked as OK")
}
//
// Confirm that it also returns false for dead instances
//
l, err = net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatal(err)
}
defer l.Close()
go tcpServer(l, "dead")
oks = FLWRuok([]string{l.Addr().String()}, time.Second*10)
if len(oks) == 0 {
t.Errorf("no values returned")
}
if oks[0] {
t.Errorf("instance should be marked as not OK")
}
}
func TestFLWSrvr(t *testing.T) {
t.Parallel()
l, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatal(err)
}
defer l.Close()
go tcpServer(l, "")
statsSlice, ok := FLWSrvr([]string{l.Addr().String()}, time.Second*10)
if !ok {
t.Errorf("failure indicated on 'srvr' parsing")
}
if len(statsSlice) == 0 {
t.Errorf("no *ServerStats instances returned")
}
stats := statsSlice[0]
if stats.Error != nil {
t.Fatalf("error seen in stats: %v", err.Error())
}
if stats.Sent != 4220 {
t.Errorf("Sent != 4220")
}
if stats.Received != 4207 {
t.Errorf("Received != 4207")
}
if stats.NodeCount != 306 {
t.Errorf("NodeCount != 306")
}
if stats.MinLatency != 0 {
t.Errorf("MinLatency != 0")
}
if stats.AvgLatency != 1 {
t.Errorf("AvgLatency != 1")
}
if stats.MaxLatency != 10 {
t.Errorf("MaxLatency != 10")
}
if stats.Connections != 81 {
t.Errorf("Connection != 81")
}
if stats.Outstanding != 1 {
t.Errorf("Outstanding != 1")
}
if stats.Epoch != 17 {
t.Errorf("Epoch != 17")
}
if stats.Counter != 175804215 {
t.Errorf("Counter != 175804215")
}
if stats.Mode != ModeLeader {
t.Errorf("Mode != ModeLeader")
}
if stats.Version != "3.4.6-1569965" {
t.Errorf("Version expected: 3.4.6-1569965")
}
}
func TestFLWCons(t *testing.T) {
t.Parallel()
l, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatal(err)
}
defer l.Close()
go tcpServer(l, "")
clients, ok := FLWCons([]string{l.Addr().String()}, time.Second*10)
if !ok {
t.Errorf("failure indicated on 'cons' parsing")
}
if len(clients) == 0 {
t.Errorf("no *ServerClients instances returned")
}
results := []*ServerClient{
{
Queued: 0,
Received: 9435,
Sent: 9457,
SessionID: 669956116721374901,
LastOperation: "PING",
Established: time.Unix(1427238717217, 0),
Timeout: 20001,
Lcxid: 1427245333,
Lzxid: -1,
LastResponse: time.Unix(1427259255908, 0),
LastLatency: 0,
MinLatency: 0,
AvgLatency: 1,
MaxLatency: 17,
Addr: "10.42.45.231:45361",
},
{
Queued: 0,
Received: 9338,
Sent: 9350,
SessionID: 669956116721375025,
LastOperation: "PING",
Established: time.Unix(1427238849319, 0),
Timeout: 20001,
Lcxid: 1427245380,
Lzxid: -1,
LastResponse: time.Unix(1427259252294, 0),
LastLatency: 0,
MinLatency: 0,
AvgLatency: 1,
MaxLatency: 18,
Addr: "10.55.33.98:34342",
},
{
Queued: 0,
Received: 109253,
Sent: 109617,
SessionID: 669956116721374985,
LastOperation: "DELE",
Established: time.Unix(1427238791305, 0),
Timeout: 20001,
Lcxid: 1427346968,
Lzxid: 73190283389,
LastResponse: time.Unix(1427259257423, 0),
LastLatency: 2,
MinLatency: 0,
AvgLatency: 1,
MaxLatency: 23,
Addr: "10.44.145.114:46556",
},
}
for _, z := range clients {
if z.Error != nil {
t.Errorf("error seen: %v", err.Error())
}
for i, v := range z.Clients {
c := results[i]
if v.Error != nil {
t.Errorf("client error seen: %v", err.Error())
}
if v.Queued != c.Queued {
t.Errorf("Queued value mismatch (%d/%d)", v.Queued, c.Queued)
}
if v.Received != c.Received {
t.Errorf("Received value mismatch (%d/%d)", v.Received, c.Received)
}
if v.Sent != c.Sent {
t.Errorf("Sent value mismatch (%d/%d)", v.Sent, c.Sent)
}
if v.SessionID != c.SessionID {
t.Errorf("SessionID value mismatch (%d/%d)", v.SessionID, c.SessionID)
}
if v.LastOperation != c.LastOperation {
t.Errorf("LastOperation value mismatch ('%v'/'%v')", v.LastOperation, c.LastOperation)
}
if v.Timeout != c.Timeout {
t.Errorf("Timeout value mismatch (%d/%d)", v.Timeout, c.Timeout)
}
if v.Lcxid != c.Lcxid {
t.Errorf("Lcxid value mismatch (%d/%d)", v.Lcxid, c.Lcxid)
}
if v.Lzxid != c.Lzxid {
t.Errorf("Lzxid value mismatch (%d/%d)", v.Lzxid, c.Lzxid)
}
if v.LastLatency != c.LastLatency {
t.Errorf("LastLatency value mismatch (%d/%d)", v.LastLatency, c.LastLatency)
}
if v.MinLatency != c.MinLatency {
t.Errorf("MinLatency value mismatch (%d/%d)", v.MinLatency, c.MinLatency)
}
if v.AvgLatency != c.AvgLatency {
t.Errorf("AvgLatency value mismatch (%d/%d)", v.AvgLatency, c.AvgLatency)
}
if v.MaxLatency != c.MaxLatency {
t.Errorf("MaxLatency value mismatch (%d/%d)", v.MaxLatency, c.MaxLatency)
}
if v.Addr != c.Addr {
t.Errorf("Addr value mismatch ('%v'/'%v')", v.Addr, c.Addr)
}
if !c.Established.Equal(v.Established) {
t.Errorf("Established value mismatch (%v/%v)", c.Established, v.Established)
}
if !c.LastResponse.Equal(v.LastResponse) {
t.Errorf("Established value mismatch (%v/%v)", c.LastResponse, v.LastResponse)
}
}
}
}
func tcpServer(listener net.Listener, thing string) {
for {
conn, err := listener.Accept()
if err != nil {
return
}
go connHandler(conn, thing)
}
}
func connHandler(conn net.Conn, thing string) {
defer conn.Close()
data := make([]byte, 4)
_, err := conn.Read(data)
if err != nil {
return
}
switch string(data) {
case "ruok":
switch thing {
case "dead":
return
default:
conn.Write([]byte("imok"))
}
case "srvr":
switch thing {
case "dead":
return
default:
conn.Write([]byte(zkSrvrOut))
}
case "cons":
switch thing {
case "dead":
return
default:
conn.Write([]byte(zkConsOut))
}
default:
conn.Write([]byte("This ZooKeeper instance is not currently serving requests."))
}
}
|