A correction to the previous commit.
[stompngo.git] / utils_test.go
blob42903f94ac4e5dbdde2152bb02a2c2c1f7e5ff3a
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 "encoding/hex"
21 "fmt"
22 "log"
23 "net"
24 "os"
25 "runtime/debug"
26 "strings"
27 "testing"
29 "github.com/gmallard/stompngo/senv"
33 Host and port for Dial.
35 func badVerHostAndPort() (string, string) {
36 h := os.Getenv("STOMP_HOSTBV") // export only if you understand these tests
37 if h == "" {
38 h = "localhost"
40 p := os.Getenv("STOMP_PORTBV") // export only if you understand these tests
41 if p == "" {
42 p = "61613"
44 return h, p
48 Check if 1.1+ style Headers are needed, and return appropriate Headers.
50 func check11(h Headers) Headers {
51 v := os.Getenv("STOMP_TEST11p")
52 if v == "" {
53 return h
55 if !Supported(v) {
56 v = SPL_11 // Just use 1.1
58 h = h.Add(HK_ACCEPT_VERSION, v)
59 s := "localhost" // STOMP 1.1 vhost (configure for Apollo)
60 if os.Getenv("STOMP_RMQ") != "" { // Rabbitmq default vhost
61 s = "/"
63 h = h.Add(HK_HOST, s)
64 return h
68 Return headers appropriate for the protocol level.
70 func headersProtocol(h Headers, protocol string) Headers {
71 if protocol == SPL_10 {
72 return h
74 h = h.Add(HK_ACCEPT_VERSION, protocol)
75 vh := "localhost" // STOMP 1.{1,2} vhost
76 if os.Getenv("STOMP_RMQ") != "" { // Rabbitmq default vhost
77 vh = "/"
79 h = h.Add(HK_HOST, vh).Add(HK_HEART_BEAT, senv.Heartbeats())
80 return h
84 Test helper.
86 func checkReceived(t *testing.T, conn *Connection, eofok bool) {
87 var md MessageData
88 select {
89 case md = <-conn.MessageData:
90 log.Printf("md is [%q]\n", md)
91 if eofok && md.Error == nil {
92 return
94 if eofok && md.Error.Error() == "EOF" {
95 return
97 debug.PrintStack()
98 t.Fatalf("Unexpected frame received, got [%#v]\n", md)
99 default:
104 Test helper.
106 func checkReceivedMD(t *testing.T, conn *Connection,
107 sc <-chan MessageData, id string) {
108 select {
109 case md = <-sc:
110 case md = <-conn.MessageData:
111 debug.PrintStack()
112 t.Fatalf("id: read channel error: expected [nil], got: [%#v] [%#v]\n",
113 id, md.Message.Command)
115 if md.Error != nil {
116 debug.PrintStack()
117 t.Fatalf("id: receive error: [%#v] [%#v]\n",
118 id, md.Error)
120 return
124 Close a network connection.
126 func closeConn(t *testing.T, n net.Conn) error {
127 err := n.Close()
128 if err != nil {
129 debug.PrintStack()
130 t.Fatalf("Unexpected n.Close() error: %#v\n", err)
132 return err
136 Test helper.
138 func getMessageData(sc <-chan MessageData, conn *Connection, t *testing.T) (md MessageData) {
139 // When this is called, there should not be any MessageData instance
140 // available on the connection level MessageData channel.
141 select {
142 case md = <-sc:
143 case md = <-conn.MessageData:
144 debug.PrintStack()
145 t.Fatalf("read channel error: expected [nil], got: [%#v]\n",
146 md.Message.Command)
148 return md
152 Open a network connection.
154 func openConn(t *testing.T) (net.Conn, error) {
155 h, p := senv.HostAndPort()
156 hap := net.JoinHostPort(h, p)
157 n, err := net.Dial(NetProtoTCP, hap)
158 if err != nil {
159 debug.PrintStack()
160 t.Fatalf("Unexpected net.Dial error: %#v\n", err)
162 return n, err
166 Test helper. Send multiple messages.
168 func sendMultiple(md multi_send_data) error {
169 h := Headers{HK_DESTINATION, md.dest}
170 for i := 0; i < md.count; i++ {
171 cstr := fmt.Sprintf("%d", i)
172 mts := md.mpref + cstr
173 e = md.conn.Send(h, mts)
174 if e != nil {
175 return e // now
178 return nil
182 Test helper. Send multiple []byte messages.
184 func sendMultipleBytes(md multi_send_data) error {
185 h := Headers{HK_DESTINATION, md.dest}
186 for i := 0; i < md.count; i++ {
187 cstr := fmt.Sprintf("%d", i)
188 mts := md.mpref + cstr
189 e = md.conn.SendBytes(h, []byte(mts))
190 if e != nil {
191 return e // now
194 return nil
198 Test helper. Get properly formatted destination.
200 func tdest(d string) string {
201 if brokerid != TEST_ARTEMIS {
202 return d
204 pref := "jms.queue"
205 if strings.Index(d, "topic") >= 0 {
206 pref = "jms.topic"
208 return pref + strings.Replace(d, "/", ".", -1)
212 Test debug helper. Get properly formatted destination.
214 func tdumpmd(md MessageData) {
215 fmt.Printf("Command: %s\n", md.Message.Command)
216 fmt.Println("Headers:")
217 for i := 0; i < len(md.Message.Headers); i += 2 {
218 fmt.Printf("key:%s\t\tvalue:%s\n",
219 md.Message.Headers[i], md.Message.Headers[i+1])
221 hdb := hex.Dump(md.Message.Body)
222 fmt.Printf("Body: %s\n", hdb)
223 if md.Error != nil {
224 fmt.Printf("Error: %s\n", md.Error.Error())
225 } else {
226 fmt.Println("Error: nil")
231 Test helper. Check disconnect error.
233 func checkDisconnectError(t *testing.T, e error) {
234 if e == nil {
235 return
237 debug.PrintStack()
238 t.Fatalf("DISCONNECT Error: expected nil, got:<%#v>\n", e)
242 Test helper. Fix up destination
244 func fixHeaderDest(h Headers) Headers {
245 r := h.Clone()
246 for i := 0; i < len(h); i += 2 {
247 if r[i] == HK_DESTINATION {
248 r[i+1] = tdest(r[i+1])
251 return r
255 Test helper. Set which broker is being tested.
257 func setTestBroker() int {
258 brokerid = TEST_ANYBROKER
259 if os.Getenv("STOMP_AMQ") != "" {
260 brokerid = TEST_AMQ
261 } else if os.Getenv("STOMP_RMQ") != "" {
262 brokerid = TEST_RMQ
263 } else if os.Getenv("STOMP_ARTEMIS") != "" {
264 brokerid = TEST_ARTEMIS
265 } else if os.Getenv("STOMP_APOLLO") != "" {
266 brokerid = TEST_APOLLO
268 return brokerid
272 Test helper. Set long heartbeat test flag.
274 func setHeartBeatFlags() {
275 if os.Getenv("STOMP_HBLONG") == "Y" { // Note: a single value to run long hb tests
276 testhbrd.testhbl = true
278 if os.Getenv("STOMP_HBVERBOSE") != "" { // Any value will do
279 testhbrd.testhbvb = true
281 return
285 Test helper. Check for missing headers
287 func checkDupeHeaders(ms, wh Headers) error {
288 for i := 0; i < len(wh); i += 2 {
289 if !ms.ContainsKV(wh[i], wh[i+1]) {
290 return Error("missing header values")
293 return nil