Version bump.
[stompngo.git] / logger_test.go
blob06de0a67cf4206ed9c24f3bdc0fba33cf39a559e
1 //
2 // Copyright © 2011-2016 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"
26 Test Logger Basic, confirm by observation.
28 func TestLoggerBasic(t *testing.T) {
29 n, _ := openConn(t)
30 ch := check11(TEST_HEADERS)
31 conn, _ := Connect(n, ch)
33 l := log.New(os.Stdout, "", log.Ldate|log.Lmicroseconds)
34 conn.SetLogger(l)
36 _ = conn.Disconnect(empty_headers)
37 _ = closeConn(t, n)
42 Test Logger with a zero Byte Message, a corner case. This is merely
43 to demonstrate the basics of log output when a logger is set for the
44 connection.
46 func TestLoggerMiscBytes0(t *testing.T) {
47 ll := log.New(os.Stdout, "TLM01 ", log.Ldate|log.Lmicroseconds|log.Lshortfile)
48 // Write phase
49 n, _ := openConn(t)
50 ch := check11(TEST_HEADERS)
51 conn, _ := Connect(n, ch)
52 conn.SetLogger(ll)
54 ms := "" // No data
55 d := tdest("/queue/logger.zero.byte.msg")
56 sh := Headers{HK_DESTINATION, d}
57 e := conn.Send(sh, ms)
58 if e != nil {
59 t.Fatalf("Expected nil error, got [%v]\n", e)
62 _ = conn.Disconnect(empty_headers)
63 _ = closeConn(t, n)
65 // Read phase
66 n, _ = openConn(t)
67 ch = check11(TEST_HEADERS)
68 conn, _ = Connect(n, ch)
69 ll = log.New(os.Stdout, "TLM02 ", log.Ldate|log.Lmicroseconds|log.Lshortfile)
70 conn.SetLogger(ll)
72 sbh := sh.Add(HK_ID, d)
73 sc, e := conn.Subscribe(sbh)
74 if e != nil {
75 t.Fatalf("Expected no subscribe error, got [%v]\n", e)
77 if sc == nil {
78 t.Fatalf("Expected subscribe channel, got [nil]\n")
81 // Read MessageData
82 var md MessageData
83 select {
84 case md = <-sc:
85 case md = <-conn.MessageData:
86 t.Fatalf("read channel error: expected [nil], got: [%v]\n",
87 md.Message.Command)
90 if md.Error != nil {
91 t.Fatalf("Expected no message data error, got [%v]\n", md.Error)
94 // The real tests here
95 if len(md.Message.Body) != 0 {
96 t.Fatalf("Expected body length 0, got [%v]\n", len(md.Message.Body))
98 if string(md.Message.Body) != ms {
99 t.Fatalf("Expected [%v], got [%v]\n", ms, string(md.Message.Body))
102 _ = conn.Disconnect(empty_headers)
103 _ = closeConn(t, n)