2 // Copyright © 2011-2019 Guy M. Allard
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
8 // http://www.apache.org/licenses/LICENSE-2.0
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.
27 Test Logger Basic, confirm by observation.
29 func TestLoggerBasic(t
*testing
.T
) {
30 for _
, sp
= range Protocols() {
33 ch
= headersProtocol(ch
, sp
)
34 log
.Printf("Connect Headers: %v\n", ch
)
35 conn
, e
= Connect(n
, ch
)
37 t
.Errorf("TestLoggerBasic CONNECT expected nil, got %v\n", e
)
39 t
.Errorf("TestLoggerBasic CONNECT ERROR, got %v\n",
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.
53 checkReceived(t
, conn
, false)
54 e
= conn
.Disconnect(empty_headers
)
55 checkDisconnectError(t
, e
)
56 time
.Sleep(testlgslt
* time
.Millisecond
)
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
67 func TestLoggerMiscBytes0(t
*testing
.T
) {
68 for _
, sp
:= range Protocols() {
69 ll
:= log
.New(os
.Stdout
, "TLM01 ", log
.Ldate|log
.Lmicroseconds|log
.Lshortfile
)
73 ch
= headersProtocol(ch
, sp
)
74 conn
, e
= Connect(n
, ch
)
76 t
.Fatalf("TestLoggerMiscBytes0 CONNECT expected nil, got %v\n", e
)
78 conn
.SetLogger(ll
) // Force a logger here
81 d
:= tdest("/queue/logger.zero.byte.msg." + sp
)
82 sh
:= Headers
{HK_DESTINATION
, d
}
85 t
.Fatalf("Expected nil error, got [%v]\n", e
)
88 _
= conn
.Disconnect(empty_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
)
102 t
.Fatalf("TestLoggerMiscBytes0 Expected no subscribe error, got [%v]\n",
106 t
.Fatalf("TestLoggerMiscBytes0 Expected subscribe channel, got [nil]\n")
113 case md
= <-conn
.MessageData
:
114 t
.Fatalf("TestLoggerMiscBytes0 read channel error: expected [nil], got: [%v]\n",
119 t
.Fatalf("TestLoggerMiscBytes0 Expected no message data error, got [%v]\n",
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
)