Rework writer deadline handling.
[stompngo.git] / utils_test.go
blob453ebb6ab1c8b4b24c9e729dceec8d97b21974bd
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 "encoding/hex"
21 "fmt"
22 "net"
23 "os"
24 "runtime/debug"
25 "strings"
26 "testing"
28 "github.com/gmallard/stompngo/senv"
32 Host and port for Dial.
34 func badVerHostAndPort() (string, string) {
35 h := os.Getenv("STOMP_HOSTBV") // export only if you understand these tests
36 if h == "" {
37 h = "localhost"
39 p := os.Getenv("STOMP_PORTBV") // export only if you understand these tests
40 if p == "" {
41 p = "61613"
43 return h, p
47 Check if 1.1+ style Headers are needed, and return appropriate Headers.
49 func check11(h Headers) Headers {
50 v := os.Getenv("STOMP_TEST11p")
51 if v == "" {
52 return h
54 if !Supported(v) {
55 v = SPL_11 // Just use 1.1
57 h = h.Add(HK_ACCEPT_VERSION, v)
58 s := "localhost" // STOMP 1.1 vhost (configure for Apollo)
59 if os.Getenv("STOMP_RMQ") != "" { // Rabbitmq default vhost
60 s = "/"
62 h = h.Add(HK_HOST, s)
63 return h
67 Return headers appropriate for the protocol level.
69 func headersProtocol(h Headers, protocol string) Headers {
70 if protocol == SPL_10 {
71 return h
73 h = h.Add(HK_ACCEPT_VERSION, protocol)
74 vh := "localhost" // STOMP 1.{1,2} vhost
75 if os.Getenv("STOMP_RMQ") != "" { // Rabbitmq default vhost
76 vh = "/"
78 h = h.Add(HK_HOST, vh).Add(HK_HEART_BEAT, senv.Heartbeats())
79 return h
83 Test helper.
85 func checkReceived(t *testing.T, conn *Connection) {
86 var md MessageData
87 select {
88 case md = <-conn.MessageData:
89 debug.PrintStack()
90 t.Fatalf("Unexpected frame received, got [%v]\n", md)
91 default:
96 Test helper.
98 func checkReceivedMD(t *testing.T, conn *Connection,
99 sc <-chan MessageData, id string) {
100 select {
101 case md = <-sc:
102 case md = <-conn.MessageData:
103 debug.PrintStack()
104 t.Fatalf("id: read channel error: expected [nil], got: [%v]\n",
105 id, md.Message.Command)
107 if md.Error != nil {
108 debug.PrintStack()
109 t.Fatalf("id: receive error: [%v]\n",
110 id, md.Error)
112 return
116 Close a network connection.
118 func closeConn(t *testing.T, n net.Conn) error {
119 err := n.Close()
120 if err != nil {
121 debug.PrintStack()
122 t.Fatalf("Unexpected n.Close() error: %v\n", err)
124 return err
128 Test helper.
130 func getMessageData(sc <-chan MessageData, conn *Connection, t *testing.T) (md MessageData) {
131 // When this is called, there should not be any MessageData instance
132 // available on the connection level MessageData channel.
133 select {
134 case md = <-sc:
135 case md = <-conn.MessageData:
136 debug.PrintStack()
137 t.Fatalf("read channel error: expected [nil], got: [%v]\n",
138 md.Message.Command)
140 return md
144 Open a network connection.
146 func openConn(t *testing.T) (net.Conn, error) {
147 h, p := senv.HostAndPort()
148 hap := net.JoinHostPort(h, p)
149 n, err := net.Dial(NetProtoTCP, hap)
150 if err != nil {
151 debug.PrintStack()
152 t.Fatalf("Unexpected net.Dial error: %v\n", err)
154 return n, err
158 Test helper. Send multiple messages.
160 func sendMultiple(md multi_send_data) error {
161 h := Headers{HK_DESTINATION, md.dest}
162 for i := 0; i < md.count; i++ {
163 cstr := fmt.Sprintf("%d", i)
164 mts := md.mpref + cstr
165 e = md.conn.Send(h, mts)
166 if e != nil {
167 return e // now
170 return nil
174 Test helper. Send multiple []byte messages.
176 func sendMultipleBytes(md multi_send_data) error {
177 h := Headers{HK_DESTINATION, md.dest}
178 for i := 0; i < md.count; i++ {
179 cstr := fmt.Sprintf("%d", i)
180 mts := md.mpref + cstr
181 e = md.conn.SendBytes(h, []byte(mts))
182 if e != nil {
183 return e // now
186 return nil
190 Test helper. Get properly formatted destination.
192 func tdest(d string) string {
193 if brokerid != TEST_ARTEMIS {
194 return d
196 pref := "jms.queue"
197 if strings.Index(d, "topic") >= 0 {
198 pref = "jms.topic"
200 return pref + strings.Replace(d, "/", ".", -1)
204 Test debug helper. Get properly formatted destination.
206 func tdumpmd(md MessageData) {
207 fmt.Printf("Command: %s\n", md.Message.Command)
208 fmt.Println("Headers:")
209 for i := 0; i < len(md.Message.Headers); i += 2 {
210 fmt.Printf("key:%s\t\tvalue:%s\n",
211 md.Message.Headers[i], md.Message.Headers[i+1])
213 hdb := hex.Dump(md.Message.Body)
214 fmt.Printf("Body: %s\n", hdb)
215 if md.Error != nil {
216 fmt.Printf("Error: %s\n", md.Error.Error())
217 } else {
218 fmt.Println("Error: nil")
223 Test helper. Check disconnect error.
225 func checkDisconnectError(t *testing.T, e error) {
226 if e == nil {
227 return
229 debug.PrintStack()
230 t.Fatalf("DISCONNECT Error: expected nil, got:<%v>\n", e)
234 Test helper. Fix up destination
236 func fixHeaderDest(h Headers) Headers {
237 r := h.Clone()
238 for i := 0; i < len(h); i += 2 {
239 if r[i] == HK_DESTINATION {
240 r[i+1] = tdest(r[i+1])
243 return r
247 Test helper. Set which broker is being tested.
249 func setTestBroker() int {
250 brokerid = TEST_ANYBROKER
251 if os.Getenv("STOMP_AMQ") != "" {
252 brokerid = TEST_AMQ
253 } else if os.Getenv("STOMP_RMQ") != "" {
254 brokerid = TEST_RMQ
255 } else if os.Getenv("STOMP_ARTEMIS") != "" {
256 brokerid = TEST_ARTEMIS
257 } else if os.Getenv("STOMP_APOLLO") != "" {
258 brokerid = TEST_APOLLO
260 return brokerid
264 Test helper. Set long heartbeat test flag.
266 func setHeartBeatFlags() {
267 if os.Getenv("STOMP_HBLONG") == "Y" { // Note: a single value to run long hb tests
268 testhbrd.testhbl = true
270 if os.Getenv("STOMP_HBVERBOSE") != "" { // Any value will do
271 testhbrd.testhbvb = true
273 return
277 Test helper. Check for missing headers
279 func checkDupeHeaders(ms, wh Headers) error {
280 for i := 0; i < len(wh); i += 2 {
281 if !ms.ContainsKV(wh[i], wh[i+1]) {
282 return Error("missing header values")
285 return nil