Rework writer deadline handling.
[stompngo.git] / logger_test.go
blobfb3691167d52f5559fff6df2c40e77b2082f6753
1 //
2 // Copyright © 2011-2017 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, "", log.Ldate|log.Lmicroseconds)
45 // Show broker's CONNECT response (CONNECTED frame).
46 l.Printf("TestLoggerBasic ConnectResponse:\n%v\n", conn.ConnectResponse)
48 // The original purpose of these tests was merely to show that setting
49 // a logger produced output. However this makes the test output very noisy.
50 // Do not set a logger inless spectifically requested by test environment
51 // variables.
52 if os.Getenv("STOMP_SETLOGGER") == "Y" {
53 conn.SetLogger(l)
57 checkReceived(t, conn)
58 e = conn.Disconnect(empty_headers)
59 checkDisconnectError(t, e)
60 time.Sleep(testlgslt * time.Millisecond)
61 _ = closeConn(t, n)
67 Test Logger with a zero Byte Message, a corner case. This is merely
68 to demonstrate the basics of log output when a logger is set for the
69 connection.
71 func TestLoggerMiscBytes0(t *testing.T) {
72 for _, sp := range Protocols() {
73 ll := log.New(os.Stdout, "TLM01 ", log.Ldate|log.Lmicroseconds|log.Lshortfile)
74 // Write phase
75 n, _ = openConn(t)
76 ch := login_headers
77 ch = headersProtocol(ch, sp)
78 conn, e = Connect(n, ch)
79 if e != nil {
80 t.Fatalf("TestLoggerMiscBytes0 CONNECT expected nil, got %v\n", e)
82 if os.Getenv("STOMP_SETLOGGER") == "Y" {
83 conn.SetLogger(ll)
86 ms := "" // No data
87 d := tdest("/queue/logger.zero.byte.msg." + sp)
88 sh := Headers{HK_DESTINATION, d}
89 e = conn.Send(sh, ms)
90 if e != nil {
91 t.Fatalf("Expected nil error, got [%v]\n", e)
94 _ = conn.Disconnect(empty_headers)
95 _ = closeConn(t, n)
97 // Read phase
98 n, _ = openConn(t)
99 ch = login_headers
100 ch = headersProtocol(ch, sp)
101 conn, _ = Connect(n, ch)
102 ll = log.New(os.Stdout, "TLM02 ", log.Ldate|log.Lmicroseconds|log.Lshortfile)
103 if os.Getenv("STOMP_SETLOGGER") == "Y" {
104 conn.SetLogger(ll)
107 sbh := sh.Add(HK_ID, d)
108 sc, e = conn.Subscribe(sbh)
109 if e != nil {
110 t.Fatalf("TestLoggerMiscBytes0 Expected no subscribe error, got [%v]\n",
113 if sc == nil {
114 t.Fatalf("TestLoggerMiscBytes0 Expected subscribe channel, got [nil]\n")
117 // Read MessageData
118 var md MessageData
119 select {
120 case md = <-sc:
121 case md = <-conn.MessageData:
122 t.Fatalf("TestLoggerMiscBytes0 read channel error: expected [nil], got: [%v]\n",
123 md.Message.Command)
126 if md.Error != nil {
127 t.Fatalf("TestLoggerMiscBytes0 Expected no message data error, got [%v]\n",
128 md.Error)
131 // The real tests here
132 if len(md.Message.Body) != 0 {
133 t.Fatalf("TestLoggerMiscBytes0 Expected body length 0, got [%v]\n",
134 len(md.Message.Body))
136 if string(md.Message.Body) != ms {
137 t.Fatalf("TestLoggerMiscBytes0 Expected [%v], got [%v]\n",
138 ms, string(md.Message.Body))
141 checkReceived(t, conn)
142 e = conn.Disconnect(empty_headers)
143 checkDisconnectError(t, e)
144 _ = closeConn(t, n)