Version bump.
[stompngo.git] / logger_test.go
blob2c6ed6fe554bc7b02d782f4cbb626fc0f270ad3c
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 "log"
21 "os"
22 "testing"
23 "time"
27 Test Logger Basic, confirm by observation.
29 func TestLoggerBasic(t *testing.T) {
30 for _, sp = range Protocols() {
31 n, _ = openConn(t)
32 ch := login_headers
33 ch = headersProtocol(ch, sp)
34 log.Printf("Connect Headers: %v\n", ch)
35 conn, e = Connect(n, ch)
36 if e != nil {
37 t.Errorf("TestLoggerBasic CONNECT expected nil, got %v\n", e)
38 if conn != nil {
39 t.Errorf("TestLoggerBasic CONNECT ERROR, got %v\n",
40 conn.ConnectResponse)
44 l := log.New(os.Stdout, "TLB ", log.Ldate|log.Lmicroseconds)
45 // Show broker's CONNECT response (CONNECTED frame).
46 l.Printf("TestLoggerBasic ConnectResponse:\n%v\n", conn.ConnectResponse)
48 // We force a logger for this test.
49 conn.SetLogger(l)
53 checkReceived(t, conn, false)
54 e = conn.Disconnect(empty_headers)
55 checkDisconnectError(t, e)
56 time.Sleep(testlgslt * time.Millisecond)
57 _ = closeConn(t, n)
63 Test Logger with a zero Byte Message, a corner case. This is merely
64 to demonstrate the basics of log output when a logger is set for the
65 connection.
67 func TestLoggerMiscBytes0(t *testing.T) {
68 for _, sp := range Protocols() {
69 ll := log.New(os.Stdout, "TLM01 ", log.Ldate|log.Lmicroseconds|log.Lshortfile)
70 // Write phase
71 n, _ = openConn(t)
72 ch := login_headers
73 ch = headersProtocol(ch, sp)
74 conn, e = Connect(n, ch)
75 if e != nil {
76 t.Fatalf("TestLoggerMiscBytes0 CONNECT expected nil, got %v\n", e)
78 conn.SetLogger(ll) // Force a logger here
80 ms := "" // No data
81 d := tdest("/queue/logger.zero.byte.msg." + sp)
82 sh := Headers{HK_DESTINATION, d}
83 e = conn.Send(sh, ms)
84 if e != nil {
85 t.Fatalf("Expected nil error, got [%v]\n", e)
88 _ = conn.Disconnect(empty_headers)
89 _ = closeConn(t, n)
91 // Read phase
92 n, _ = openConn(t)
93 ch = login_headers
94 ch = headersProtocol(ch, sp)
95 conn, _ = Connect(n, ch)
96 ll = log.New(os.Stdout, "TLM02 ", log.Ldate|log.Lmicroseconds|log.Lshortfile)
97 conn.SetLogger(ll) // Force a logger here
99 sbh := sh.Add(HK_ID, d)
100 sc, e = conn.Subscribe(sbh)
101 if e != nil {
102 t.Fatalf("TestLoggerMiscBytes0 Expected no subscribe error, got [%v]\n",
105 if sc == nil {
106 t.Fatalf("TestLoggerMiscBytes0 Expected subscribe channel, got [nil]\n")
109 // Read MessageData
110 var md MessageData
111 select {
112 case md = <-sc:
113 case md = <-conn.MessageData:
114 t.Fatalf("TestLoggerMiscBytes0 read channel error: expected [nil], got: [%v]\n",
115 md.Message.Command)
118 if md.Error != nil {
119 t.Fatalf("TestLoggerMiscBytes0 Expected no message data error, got [%v]\n",
120 md.Error)
123 // The real tests here
124 if len(md.Message.Body) != 0 {
125 t.Fatalf("TestLoggerMiscBytes0 Expected body length 0, got [%v]\n",
126 len(md.Message.Body))
128 if string(md.Message.Body) != ms {
129 t.Fatalf("TestLoggerMiscBytes0 Expected [%v], got [%v]\n",
130 ms, string(md.Message.Body))
133 checkReceived(t, conn, false)
134 e = conn.Disconnect(empty_headers)
135 checkDisconnectError(t, e)
136 _ = closeConn(t, n)