Version bump.
[stompngo.git] / hb_test.go
blob7871aa3fb66eaaf56a9bd9fc3be6b46dff3e5d69
1 //
2 // Copyright © 2011-2019 Guy M. Allard
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
17 package stompngo
19 import (
20 //"fmt"
21 "log"
22 "os"
23 "testing"
24 "time"
28 HB Test: None.
30 func TestHBNone(t *testing.T) {
31 for _, sp := range Protocols() {
32 n, _ = openConn(t)
33 ch := login_headers
34 ch = headersProtocol(ch, sp)
35 conn, _ = Connect(n, ch)
37 if conn.hbd != nil {
38 t.Fatalf("TestHBNone Expected no heartbeats, proto: <%s>\n", sp)
40 checkReceived(t, conn, false)
41 e = conn.Disconnect(empty_headers)
42 checkDisconnectError(t, e)
43 _ = closeConn(t, n)
48 HB Test: Zero HB Header.
50 func TestHBZeroHeader(t *testing.T) {
51 for _, sp := range Protocols() {
52 n, _ = openConn(t)
53 ch := login_headers
54 ch = headersProtocol(ch, sp)
55 ch = ch.Add(HK_HEART_BEAT, "0,0")
56 conn, _ = Connect(n, ch)
57 if conn.hbd != nil {
58 t.Fatalf("TestHBZeroHeader Expected no heartbeats, 0,0 header, proto: <%s>\n",
59 sp)
61 checkReceived(t, conn, false)
62 e = conn.Disconnect(empty_headers)
63 checkDisconnectError(t, e)
64 _ = closeConn(t, n)
69 HB Test: 1.1 Initialization Errors.
71 func TestHBInitErrors(t *testing.T) {
73 for _, sp := range Protocols() {
74 n, _ = openConn(t)
75 ch := login_headers
76 ch = headersProtocol(ch, sp)
77 conn, _ = Connect(n, ch)
78 errorE1OrD1(t, conn, sp, "InitErrors", nil)
80 e = conn.initializeHeartBeats(empty_headers)
81 errorE1OrD1(t, conn, sp, "HBEmpty", e)
82 // fmt.Printf("1Err: <%v> <%v>\n", e, sp)
84 h := Headers{HK_HEART_BEAT, "0,0"}
85 e = conn.initializeHeartBeats(h)
86 errorE1OrD1(t, conn, sp, "HB0,0", e)
87 // fmt.Printf("2Err: <%v> <%v>\n", e, sp)
89 crc := conn.ConnectResponse.Headers.Delete(HK_HEART_BEAT)
90 conn.ConnectResponse.Headers = crc.Add(HK_HEART_BEAT, "10,10")
92 h = Headers{HK_HEART_BEAT, "1,2,2"}
93 e = conn.initializeHeartBeats(h)
94 errorE0OrD1(t, conn, sp, "HB1,2,2", e)
95 ee := Error("invalid client heart-beat header: " + "1,2,2")
96 if ee != e {
97 t.Fatalf("TestHBInitErrors HBT 1,2,2: expected:<%v> got:<%v> <%v>\n",
98 ee, e, sp)
101 h = Headers{HK_HEART_BEAT, "a,1"}
102 e = conn.initializeHeartBeats(h)
103 errorE0OrD1(t, conn, sp, "HBa,1", e)
104 ee = Error("non-numeric cx heartbeat value: " + "a")
105 if ee != e {
106 t.Fatalf("TestHBInitErrors HBT a,1: expected:<%v> got:<%v> <%v>\n",
107 ee, e, sp)
110 h = Headers{HK_HEART_BEAT, "1,b"}
111 e = conn.initializeHeartBeats(h)
112 errorE0OrD1(t, conn, sp, "HB1,b", e)
113 ee = Error("non-numeric cy heartbeat value: " + "b")
114 if ee != e {
115 t.Fatalf("TestHBInitErrors HBT 1,b: expected:<%v> got:<%v> <%v>\n",
116 ee, e, sp)
119 h = Headers{HK_HEART_BEAT, "100,100"}
120 conn.ConnectResponse.Headers = crc.Add(HK_HEART_BEAT, "10,10,10")
121 e = conn.initializeHeartBeats(h)
122 errorE0OrD1(t, conn, sp, "HBAdd10,10,10", e)
123 // fmt.Printf("3Err: <%v> <%v>\n", e, sp)
124 ee = Error("invalid server heart-beat header: " + "10,10,10")
125 if ee != e {
126 t.Fatalf("TestHBInitErrors HBT 1,b: expected:<%v> got:<%v> <%v>\n",
127 ee, e, sp)
130 conn.ConnectResponse.Headers = crc.Add(HK_HEART_BEAT, "a,3")
131 e = conn.initializeHeartBeats(h)
132 errorE0OrD1(t, conn, sp, "HBAdda,3", e)
133 ee = Error("non-numeric sx heartbeat value: " + "a")
134 if ee != e {
135 t.Fatalf("TestHBInitErrors HBT a,3: expected:<%v> got:<%v> <%v>\n",
136 ee, e, sp)
139 conn.ConnectResponse.Headers = crc.Add(HK_HEART_BEAT, "3,b")
140 e = conn.initializeHeartBeats(h)
141 errorE0OrD1(t, conn, sp, "HBAdd3,a", e)
142 ee = Error("non-numeric sy heartbeat value: " + "b")
143 if ee != e {
144 t.Fatalf("TestHBInitErrors HBT 3,b: expected:<%v> got:<%v> <%v>\n",
145 ee, e, sp)
148 checkReceived(t, conn, false)
149 e = conn.Disconnect(empty_headers)
150 checkDisconnectError(t, e)
151 _ = closeConn(t, n)
156 HB Test: Connect Test.
158 func TestHBConnect(t *testing.T) {
159 for _, sp := range oneOnePlusProtos {
160 n, _ = openConn(t)
161 ch := login_headers
162 ch = headersProtocol(ch, sp)
163 ch = ch.Delete(HK_HEART_BEAT).Add(HK_HEART_BEAT, "250,250")
164 conn, e = Connect(n, ch)
166 if e != nil {
167 t.Fatalf("TestHBConnect Heartbeat connect error, unexpected: error:%q response:%q\n",
168 e, conn.ConnectResponse)
170 if conn.hbd == nil {
171 t.Fatalf("TestHBConnect Heartbeat expected data, got nil")
173 if conn.SendTickerInterval() == 0 {
174 t.Fatalf("TestHBConnect Send Ticker is zero.")
176 if conn.ReceiveTickerInterval() == 0 {
177 t.Fatalf("TestHBConnect Receive Ticker is zero.")
180 checkReceived(t, conn, false)
181 e = conn.Disconnect(empty_headers)
182 checkDisconnectError(t, e)
183 _ = closeConn(t, n)
188 Test Connect - Test HeartBeat - Receive only, No Sends From Client
190 func TestHBNoSend(t *testing.T) {
191 if !testhbrd.testhbl {
192 t.Skip("TestHBNoSend norun, set STOMP_HB11LONG")
194 if brokerid == TEST_ARTEMIS {
195 t.Skip("TestHBNoSend norun, unset STOMP_ARTEMIS")
198 for _, sp := range oneOnePlusProtos {
199 n, _ = openConn(t)
200 ch := login_headers
201 ch = headersProtocol(ch, sp)
202 ch = ch.Delete(HK_HEART_BEAT).Add(HK_HEART_BEAT, "0,6000")
203 l := log.New(os.Stderr, "THBNS ", log.Ldate|log.Lmicroseconds)
204 l.Printf("ConnHeaders: %v\n", ch)
205 conn, e = Connect(n, ch)
206 // Error checks
207 if e != nil {
208 t.Fatalf("TestHBNoSend connect error, unexpected: error:%q response:%q\n",
209 e, conn.ConnectResponse)
211 if conn.hbd == nil {
212 t.Fatalf("TestHBNoSend error expected hbd value.")
214 if conn.ReceiveTickerInterval() == 0 {
215 t.Fatalf("TestHBNoSend Receive Ticker is zero.")
218 if testhbrd.testhbvb {
219 conn.SetLogger(l)
222 conn.log("TestHBNoSend start sleep")
223 conn.log("TestHBNoSend connect response",
224 conn.ConnectResponse.Command,
225 conn.ConnectResponse.Headers,
226 string(conn.ConnectResponse.Body))
227 conn.log(1, "Send", conn.SendTickerInterval(), "Receive",
228 conn.ReceiveTickerInterval())
229 time.Sleep(hbs * time.Second)
230 conn.log("TestHBNoSend end sleep")
232 conn.hbd.rdl.Lock()
233 if conn.Hbrf {
234 t.Fatalf("Error, dirty heart beat read detected")
236 conn.hbd.rdl.Unlock()
237 checkHBRecv(t, conn, 1)
239 checkReceived(t, conn, false)
240 e = conn.Disconnect(empty_headers)
241 checkDisconnectError(t, e)
242 _ = closeConn(t, n)
247 Test Connect - Test HeartBeat - Send only, No Receives by Client
249 func TestHBNoReceive(t *testing.T) {
250 if !testhbrd.testhbl {
251 t.Skip("TestHBNoReceive norun, set STOMP_HB11LONG")
253 for _, sp := range oneOnePlusProtos {
254 n, _ = openConn(t)
255 ch := login_headers
256 ch = headersProtocol(ch, sp)
257 ch = ch.Delete(HK_HEART_BEAT).Add(HK_HEART_BEAT, "10000,0")
258 l := log.New(os.Stderr, "THBNR ", log.Ldate|log.Lmicroseconds)
259 l.Printf("ConnHeaders: %v\n", ch)
260 conn, e = Connect(n, ch)
261 // Error checks
262 if e != nil {
263 t.Fatalf("TestHBNoReceive connect error, unexpected: error:%q response:%q\n",
264 e, conn.ConnectResponse)
266 if conn.hbd == nil {
267 t.Fatalf("TestHBNoReceive error expected hbd value.")
269 if conn.SendTickerInterval() == 0 {
270 t.Fatalf("TestHBNoReceive Send Ticker is zero.")
273 if testhbrd.testhbvb {
274 conn.SetLogger(l)
277 conn.log("TestHBNoReceive start sleep")
278 conn.log("TestHBNoReceive connect response",
279 conn.ConnectResponse.Command,
280 conn.ConnectResponse.Headers,
281 string(conn.ConnectResponse.Body))
282 conn.log(2, "Send", conn.SendTickerInterval(), "Receive",
283 conn.ReceiveTickerInterval())
284 time.Sleep(hbs * time.Second)
285 conn.log("TestHBNoReceive end sleep")
287 checkHBSend(t, conn, 2)
288 checkReceived(t, conn, false)
289 e = conn.Disconnect(empty_headers)
290 checkDisconnectError(t, e)
291 _ = closeConn(t, n)
296 Test Connect - Test HeartBeat - Send and Receive
298 func TestHBSendReceive(t *testing.T) {
299 if !testhbrd.testhbl {
300 t.Skip("TestHBSendReceive norun, set STOMP_HB11LONG")
302 for _, sp := range oneOnePlusProtos {
303 n, _ = openConn(t)
304 ch := login_headers
305 ch = headersProtocol(ch, sp)
306 ch = ch.Delete(HK_HEART_BEAT).Add(HK_HEART_BEAT, "10000,600")
307 l := log.New(os.Stderr, "THBSR ", log.Ldate|log.Lmicroseconds)
308 l.Printf("ConnHeaders: %v\n", ch)
309 conn, e = Connect(n, ch)
310 // Error checks
311 if e != nil {
312 t.Fatalf("TestHBSendReceive connect error, unexpected: error:%q response:%q\n",
313 e, conn.ConnectResponse)
315 if conn.hbd == nil {
316 t.Fatalf("Heartbeat TestHBSendReceive error expected hbd value.")
318 if conn.ReceiveTickerInterval() == 0 {
319 t.Fatalf("TestHBSendReceive Receive Ticker is zero.")
321 if conn.SendTickerInterval() == 0 {
322 t.Fatalf("TestHBSendReceive Send Ticker is zero.")
325 if testhbrd.testhbvb {
326 conn.SetLogger(l)
329 conn.log("TestHBSendReceive start sleep")
330 conn.log("TestHBSendReceive connect response",
331 conn.ConnectResponse.Command,
332 conn.ConnectResponse.Headers,
333 string(conn.ConnectResponse.Body))
334 conn.log(3, "Send", conn.SendTickerInterval(), "Receive",
335 conn.ReceiveTickerInterval())
336 time.Sleep(hbs * time.Second)
337 conn.log("TestHBSendReceive end sleep")
338 conn.hbd.rdl.Lock()
339 if conn.Hbrf {
340 t.Fatalf("TestHBSendReceive Error, dirty heart beat read detected")
342 conn.hbd.rdl.Unlock()
343 checkHBSendRecv(t, conn, 3)
345 checkReceived(t, conn, false)
346 e = conn.Disconnect(empty_headers)
347 checkDisconnectError(t, e)
348 _ = closeConn(t, n)
353 Test Connect - Test HeartBeat - Send and Receive -
354 Match Apollo defaults.
356 func TestHBSendReceiveApollo(t *testing.T) {
357 if !testhbrd.testhbl {
358 t.Skip("TestHBSendReceiveApollo norun, set STOMP_HB11LONG")
360 for _, sp := range oneOnePlusProtos {
361 n, _ = openConn(t)
362 ch := login_headers
363 ch = headersProtocol(ch, sp)
364 ch = ch.Delete(HK_HEART_BEAT).Add(HK_HEART_BEAT, "10000,100")
365 l := log.New(os.Stderr, "THBSRA ", log.Ldate|log.Lmicroseconds)
366 l.Printf("ConnHeaders: %v\n", ch)
367 conn, e = Connect(n, ch)
368 // Error checks
369 if e != nil {
370 t.Fatalf("TestHBSendReceiveApollo connect error, unexpected: error:%q response:%q\n",
371 e, conn.ConnectResponse)
373 if conn.hbd == nil {
374 t.Fatalf("TestHBSendReceiveApollo error expected hbd value.")
377 if conn.ReceiveTickerInterval() == 0 {
378 t.Fatalf("TestHBSendReceiveApollo Receive Ticker is zero.")
380 if conn.SendTickerInterval() == 0 {
381 t.Fatalf("TestHBSendReceiveApollo Send Ticker is zero.")
384 if testhbrd.testhbvb {
385 conn.SetLogger(l)
388 conn.log("TestHBSendReceiveApollo start sleep")
389 conn.log("TestHBSendReceiveApollo connect response",
390 conn.ConnectResponse.Command,
391 conn.ConnectResponse.Headers,
392 string(conn.ConnectResponse.Body))
393 conn.log(4, "Send", conn.SendTickerInterval(), "Receive",
394 conn.ReceiveTickerInterval())
395 time.Sleep(hbs * time.Second)
396 conn.log("TestHBSendReceiveApollo end sleep")
397 conn.hbd.rdl.Lock()
398 if conn.Hbrf {
399 t.Fatalf("TestHBSendReceiveApollo Error, dirty heart beat read detected")
401 conn.hbd.rdl.Unlock()
402 checkHBSendRecv(t, conn, 4)
404 checkReceived(t, conn, false)
405 e = conn.Disconnect(empty_headers)
406 checkDisconnectError(t, e)
407 _ = closeConn(t, n)
412 Test Connect to - Test HeartBeat - Send and Receive -
413 Match reverse of Apollo defaults.
414 Currently skipped for AMQ.
416 func TestHBSendReceiveRevApollo(t *testing.T) {
417 if !testhbrd.testhbl {
418 t.Skip("TestHBSendReceiveRevApollo norun, set STOMP_HB11LONG")
420 if brokerid == TEST_AMQ {
421 t.Skip("TestHBSendReceiveRevApollo norun, unset STOMP_AMQ")
423 for _, sp := range oneOnePlusProtos {
424 n, _ = openConn(t)
425 ch := login_headers
426 ch = headersProtocol(ch, sp)
427 ch = ch.Delete(HK_HEART_BEAT).Add(HK_HEART_BEAT, "100,10000")
428 l := log.New(os.Stderr, "THBSRRA ", log.Ldate|log.Lmicroseconds)
429 l.Printf("ConnHeaders: %v\n", ch)
430 conn, e = Connect(n, ch)
431 // Error checks
432 if e != nil {
433 t.Fatalf("TestHBSendReceiveRevApollo connect error, unexpected: error:%q response:%q\n",
434 e, conn.ConnectResponse)
436 if conn.hbd == nil {
437 t.Fatalf("TestHBSendReceiveRevApollo error expected hbd value.")
439 if conn.ReceiveTickerInterval() == 0 {
440 t.Fatalf("TestHBSendReceiveRevApollo Receive Ticker is zero.")
442 if conn.SendTickerInterval() == 0 {
443 t.Fatalf("TestHBSendReceiveRevApollo Send Ticker is zero.")
446 l.Printf("TestHBSendReceiveRevApollo CONNECTED Frame: <%q>\n", conn.ConnectResponse)
447 if testhbrd.testhbvb {
448 conn.SetLogger(l)
451 conn.log("TestHBSendReceiveRevApollo start sleep")
452 conn.log("TestHBSendReceiveRevApollo connect response",
453 conn.ConnectResponse.Command,
454 conn.ConnectResponse.Headers,
455 string(conn.ConnectResponse.Body))
456 conn.log(5, "Send", conn.SendTickerInterval(), "Receive",
457 conn.ReceiveTickerInterval())
458 time.Sleep(hbs * time.Second)
459 //time.Sleep(30 * time.Second) // For experimentation
460 conn.log("TestHBSendReceiveRevApollo end sleep")
461 conn.hbd.rdl.Lock()
462 if conn.Hbrf {
463 t.Fatalf("TestHBSendReceiveRevApollo Error, dirty heart beat read detected")
465 conn.hbd.rdl.Unlock()
466 checkHBSendRecv(t, conn, 5)
468 // AMQ seems to always fail with these heartbeat specs.
469 if os.Getenv("STOMP_AMQ11") != "" {
470 // 'true' is specified here.
471 checkReceived(t, conn, true)
472 // Also, do not check for DISCONNECT error here, because it will
473 // always fail.
474 _ = conn.Disconnect(empty_headers)
475 _ = closeConn(t, n)
476 } else {
477 checkReceived(t, conn, false)
478 e = conn.Disconnect(empty_headers)
479 checkDisconnectError(t, e)
480 _ = closeConn(t, n)
486 Check Heart Beat Data when sending and receiving.
488 func checkHBSendRecv(t *testing.T, conn *Connection, i int) {
489 conn.hbd.rdl.Lock()
490 defer conn.hbd.rdl.Unlock()
491 conn.hbd.sdl.Lock()
492 defer conn.hbd.sdl.Unlock()
493 if conn.SendTickerInterval() == 0 {
494 t.Fatalf("Send Ticker is zero. %d", i)
496 if conn.ReceiveTickerInterval() == 0 {
497 t.Fatalf("Receive Ticker is zero. %d", i)
499 if conn.SendTickerCount() == 0 {
500 t.Fatalf("Send Count is zero. %d", i)
502 if conn.ReceiveTickerInterval() == 0 {
503 t.Fatalf("Receive Count is zero. %d", i)
508 Check Heart Beat Data when sending.
510 func checkHBSend(t *testing.T, conn *Connection, i int) {
511 conn.hbd.sdl.Lock()
512 defer conn.hbd.sdl.Unlock()
513 if conn.SendTickerInterval() == 0 {
514 t.Fatalf("Send Ticker is zero. %d", i)
516 if conn.ReceiveTickerInterval() != 0 {
517 t.Fatalf("Receive Ticker is not zero. %d", i)
519 if conn.SendTickerCount() == 0 {
520 t.Fatalf("Send Count is zero. %d", i)
522 if conn.ReceiveTickerInterval() != 0 {
523 t.Fatalf("Receive Count is not zero. %d", i)
528 Check Heart Beat Data when receiving.
530 func checkHBRecv(t *testing.T, conn *Connection, i int) {
531 conn.hbd.rdl.Lock()
532 defer conn.hbd.rdl.Unlock()
533 if conn.SendTickerInterval() != 0 {
534 t.Fatalf("Send Ticker is not zero. %d", i)
536 if conn.ReceiveTickerInterval() == 0 {
537 t.Fatalf("Receive Ticker is zero. %d", i)
539 if conn.SendTickerCount() != 0 {
540 t.Fatalf("Send Count is not zero. %d", i)
542 if conn.ReceiveTickerInterval() == 0 {
543 t.Fatalf("Receive Count is zero. %d", i)
549 func errorE0OrD0(t *testing.T, conn *Connection, sp, id string, e error) {
550 if e == nil || conn.hbd == nil {
551 t.Fatalf("E0OrD0 %v %v %v %v\n", e, conn.hbd, sp, id)
557 func errorE0OrD1(t *testing.T, conn *Connection, sp, id string, e error) {
558 if e == nil || conn.hbd != nil {
559 t.Fatalf("E0OrD1 %v %v %v %v\n", e, conn.hbd, sp, id)
565 func errorE1OrD0(t *testing.T, conn *Connection, sp, id string, e error) {
566 if e != nil || conn.hbd == nil {
567 t.Fatalf("E1OrD0 %v %v %v %v\n", e, conn.hbd, sp, id)
573 func errorE1OrD1(t *testing.T, conn *Connection, sp, id string, e error) {
574 if e != nil || conn.hbd != nil {
575 t.Fatalf("E1OrD1 %v %v %v %v\n", e, conn.hbd, sp, id)