2 // Copyright © 2012-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.
24 Test A zero Byte Message, a corner case.
26 func TestMiscBytes0(t
*testing
.T
) {
27 for _
, sp
:= range Protocols() {
28 //tlg.Printf("TestMiscBytes0 protocol:%s\n", sp)
30 //tlg.Printf("TestMiscBytes0 WRITEPHASE\n")
33 ch
= headersProtocol(ch
, sp
)
34 conn
, e
= Connect(n
, ch
)
36 t
.Fatalf("TestMiscBytes0 CONNECT expected nil, got %v\n", e
)
40 d
:= tdest("/queue/misc.zero.byte.msg." + sp
)
41 sh
:= Headers
{HK_DESTINATION
, d
}
44 t
.Fatalf("TestMiscBytes0 Expected nil error, got [%v]\n", e
)
47 checkReceived(t
, conn
, false)
48 e
= conn
.Disconnect(empty_headers
)
49 checkDisconnectError(t
, e
)
53 //tlg.Printf("TestMiscBytes0 READPHASE\n")
56 ch
= headersProtocol(ch
, sp
)
57 conn
, _
= Connect(n
, ch
)
59 sbh
:= sh
.Add(HK_ID
, d
)
60 sc
, e
= conn
.Subscribe(sbh
)
62 t
.Fatalf("TestMiscBytes0 Expected no subscribe error, got [%v]\n", e
)
65 t
.Fatalf("TestMiscBytes0 Expected subscribe channel, got [nil]\n")
72 case md
= <-conn
.MessageData
:
73 t
.Fatalf("TestMiscBytes0 read channel error: expected [nil], got: [%v]\n",
78 t
.Fatalf("TestMiscBytes0 Expected no message data error, got [%v]\n",
82 // The real tests here
83 if len(md
.Message
.Body
) != 0 {
84 t
.Fatalf("TestMiscBytes0 Expected body length 0, got [%v]\n",
87 if string(md
.Message
.Body
) != ms
{
88 t
.Fatalf("TestMiscBytes0 Expected [%v], got [%v]\n",
89 ms
, string(md
.Message
.Body
))
92 //tlg.Printf("TestMiscBytes0 CLEANUP\n")
93 checkReceived(t
, conn
, false)
94 e
= conn
.Disconnect(empty_headers
)
95 checkDisconnectError(t
, e
)
101 Test A One Byte Message, a corner case.
103 func TestMiscBytes1(t
*testing
.T
) {
104 for _
, sp
:= range Protocols() {
105 //tlg.Printf("TestMiscBytes0 protocol:%s\n", sp)
107 //tlg.Printf("TestMiscBytes0 WRITEPHASE\n")
110 ch
= headersProtocol(ch
, sp
)
111 conn
, e
= Connect(n
, ch
)
113 t
.Fatalf("TestMiscBytes1 CONNECT expected nil, got %v\n", e
)
116 ms
:= "Z" // One Byte
117 d
:= tdest("/queue/misc.zero.byte.msg." + sp
)
118 sh
:= Headers
{HK_DESTINATION
, d
}
119 e
= conn
.Send(sh
, ms
)
121 t
.Fatalf("TestMiscBytes1 Expected nil error, got [%v]\n", e
)
124 checkReceived(t
, conn
, false)
125 e
= conn
.Disconnect(empty_headers
)
126 checkDisconnectError(t
, e
)
130 //tlg.Printf("TestMiscBytes1 READPHASE\n")
133 ch
= headersProtocol(ch
, sp
)
134 conn
, _
= Connect(n
, ch
)
136 sbh
:= sh
.Add(HK_ID
, d
)
137 sc
, e
= conn
.Subscribe(sbh
)
139 t
.Fatalf("TestMiscBytes1 Expected no subscribe error, got [%v]\n", e
)
142 t
.Fatalf("TestMiscBytes1 Expected subscribe channel, got [nil]\n")
149 case md
= <-conn
.MessageData
:
150 t
.Fatalf("TestMiscBytes1 read channel error: expected [nil], got: [%v]\n",
155 t
.Fatalf("TestMiscBytes1 Expected no message data error, got [%v]\n",
159 // The real tests here
160 if len(md
.Message
.Body
) != 1 {
161 t
.Fatalf("TestMiscBytes1 Expected body length 1, got [%v]\n",
162 len(md
.Message
.Body
))
164 if string(md
.Message
.Body
) != ms
{
165 t
.Fatalf("TestMiscBytes1 Expected [%v], got [%v]\n",
166 ms
, string(md
.Message
.Body
))
169 //tlg.Printf("TestMiscBytes1 CLEANUP\n")
170 checkReceived(t
, conn
, false)
171 e
= conn
.Disconnect(empty_headers
)
172 checkDisconnectError(t
, e
)
180 func TestMiscNilHeaders(t
*testing
.T
) {
181 for _
, _
= range Protocols() {
184 _
, e
= Connect(n
, nil)
186 t
.Fatalf("TestMiscNilHeaders Expected [%v], got [nil]\n",
190 t
.Fatalf("TestMiscNilHeaders Expected [%v], got [%v]\n",
194 ch
:= check11(TEST_HEADERS
)
195 conn
, _
= Connect(n
, ch
)
200 t
.Fatalf("TestMiscNilHeaders Abort Expected [%v], got [nil]\n",
207 t
.Fatalf("TestMiscNilHeaders Ack Expected [%v], got [nil]\n",
214 t
.Fatalf("TestMiscNilHeaders Begin Expected [%v], got [nil]\n",
221 t
.Fatalf("TestMiscNilHeaders Commit Expected [%v], got [nil]\n",
226 e
= conn
.Disconnect(nil)
228 t
.Fatalf("TestMiscNilHeaders Disconnect Expected [%v], got [nil]\n",
232 if conn
.Protocol() > SPL_10
{
234 e
= conn
.Disconnect(nil)
236 t
.Fatalf("TestMiscNilHeaders Nack Expected [%v], got [nil]\n",
242 e
= conn
.Send(nil, "")
244 t
.Fatalf("TestMiscNilHeaders Send Expected [%v], got [nil]\n",
254 func TestMiscMax(t
*testing
.T
) {
255 for _
, _
= range Protocols() {
256 var l
int64 = 1 // low
257 var h
int64 = 2 // high
260 t
.Fatalf("TestMiscMax Expected [%v], got [%v]\n", h
, mr
)
264 t
.Fatalf("TestMiscMax Expected [%v], got [%v]\n", h
, mr
)
270 Test hasValue function.
272 func TestMiscHasValue(t
*testing
.T
) {
273 for _
, _
= range Protocols() {
274 sa
:= []string{"a", "b"}
275 if !hasValue(sa
, "a") {
276 t
.Fatalf("TestMiscHasValue Expected [true], got [false] for [%v]\n", "a")
278 if hasValue(sa
, "z") {
279 t
.Fatalf("TestMiscHasValue Expected [false], got [true] for [%v]\n", "z")
287 func TestMiscUuid(t
*testing
.T
) {
288 for _
, _
= range Protocols() {
291 t
.Fatalf("TestMiscUuid Expected a UUID, got empty string\n")
294 t
.Fatalf("TestMiscUuid Expected a 36 character UUID, got length [%v]\n",
303 func TestMiscBadHeaders(t
*testing
.T
) {
304 for _
, sp
= range Protocols() {
307 neh
:= Headers
{"a", "b", "c"} // not even number header count
308 conn
, e
= Connect(n
, neh
)
310 // Connection should be nil (i.e. no connection)
312 t
.Fatalf("TestMiscBadHeaders Expected [%v], got [nil]\n", EHDRLEN
)
315 t
.Fatalf("TestMiscBadHeaders Expected [%v], got [%v]\n", EHDRLEN
, e
)
318 bvh
:= Headers
{HK_HOST
, "localhost", HK_ACCEPT_VERSION
, "3.14159"}
319 conn
, e
= Connect(n
, bvh
)
321 t
.Fatalf("TestMiscBadHeaders Expected [%v], got [nil]\n", EBADVERCLI
)
324 t
.Fatalf("TestMiscBadHeaders Expected [%v], got [%v]\n", EBADVERCLI
, e
)
329 ch
= headersProtocol(ch
, sp
)
330 //log.Printf("TestMiscBadHeaders Protocol %s, CONNECT Headers: %v\n", sp, ch)
331 conn
, e
= Connect(n
, ch
)
333 t
.Fatalf("TestMiscBadHeaders CONNECT 2 expected nil, got %v connectresponse: %v\n",
334 e
, conn
.ConnectResponse
)
337 // Connection should not be nil (i.e. good connection)
338 _
, e
= conn
.Subscribe(neh
)
340 t
.Fatalf("TestMiscBadHeaders Expected [%v], got [nil]\n", EHDRLEN
)
343 t
.Fatalf("TestMiscBadHeaders Expected [%v], got [%v]\n", EHDRLEN
, e
)
346 e
= conn
.Unsubscribe(neh
)
348 t
.Fatalf("TestMiscBadHeaders Expected [%v], got [nil]\n", EHDRLEN
)
351 t
.Fatalf("TestMiscBadHeaders Expected [%v], got [%v]\n", EHDRLEN
, e
)
354 if conn
!= nil && conn
.Connected() {
355 e
= conn
.Disconnect(empty_headers
)
356 checkDisconnectError(t
, e
)