Version bump.
[stompngo.git] / codec_test.go
blob65330dc13cd9a1da79d445b2be575ef6c488b90d
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 "log"
21 "os"
22 "testing"
23 "time"
26 var _ = log.Println
28 // Test STOMP 1.1 Header Codec - Basic Encode.
29 func TestCodecEncodeBasic(t *testing.T) {
30 for _, _ = range Protocols() {
31 for _, ede := range tdList {
32 ev := encode(ede.decoded)
33 if ede.encoded != ev {
34 t.Fatalf("TestCodecEncodeBasic ENCODE ERROR: expected: [%v] got: [%v]",
35 ede.encoded, ev)
42 Test STOMP 1.1 Header Codec - Basic Decode.
44 func TestCodecDecodeBasic(t *testing.T) {
45 for _, _ = range Protocols() {
46 for _, ede := range tdList {
47 dv := decode(ede.encoded)
48 if ede.decoded != dv {
49 t.Fatalf("TestCodecDecodeBasic DECODE ERROR: expected: [%v] got: [%v]",
50 ede.decoded, dv)
56 func BenchmarkCodecEncode(b *testing.B) {
57 for _, _ = range Protocols() {
58 for i := 0; i < len(tdList); i++ {
59 for n := 0; n < b.N; n++ {
60 _ = encode(tdList[i].decoded)
66 func BenchmarkCodecDecode(b *testing.B) {
67 for _, _ = range Protocols() {
68 for i := 0; i < len(tdList); i++ {
69 for n := 0; n < b.N; n++ {
70 _ = decode(tdList[i].encoded)
77 Test STOMP 1.1 Send / Receive - no codec error.
79 func TestCodecSendRecvCodec(t *testing.T) {
80 if os.Getenv("STOMP_ARTEMIS") != "" {
81 return
84 for _, p := range Protocols() {
85 usemap := srcdmap[p]
86 //log.Printf("Protocol: %s\n", p)
87 //log.Printf("MapLen: %d\n", len(usemap))
88 for _, v := range usemap {
91 // RMQ and STOMP Level 1.0 :
92 // Headers are encoded (as if the STOMP protocol were 1.1
93 // or 1.2).
94 // MAYBEDO: Report issue. (Is this a bug or a feature?)
96 if p == SPL_10 && os.Getenv("STOMP_RMQ") != "" {
97 continue
100 n, _ = openConn(t)
101 ch := login_headers
102 ch = headersProtocol(ch, p)
103 conn, e = Connect(n, ch)
104 if e != nil {
105 t.Fatalf("TestCodecSendRecvCodec CONNECT expected nil, got %v\n", e)
108 d := tdest("/queue/gostomp.codec.sendrecv.1.protocol." + p)
109 ms := "msg.codec.sendrecv.1.protocol." + p + " - a message"
110 wh := Headers{HK_DESTINATION, d}
112 //log.Printf("TestData: %+v\n", v)
113 sh := wh.Clone()
114 for i := range v.sk {
115 sh = sh.Add(v.sk[i], v.sv[i])
117 // Send
118 //log.Printf("Send Headers: %v\n", sh)
119 e = conn.Send(sh, ms)
120 if e != nil {
121 t.Fatalf("TestCodecSendRecvCodec Send failed: %v protocol:%s\n",
122 e, p)
124 // Check for ERROR frame
125 time.Sleep(1e9 / 8) // Wait one eigth
126 // Poll for adhoc ERROR from server
127 select {
128 case vx := <-conn.MessageData:
129 t.Fatalf("TestCodecSendRecvCodec Send Error: [%v] protocol:%s\n",
130 vx, p)
131 default:
134 // Subscribe
135 sbh := wh.Add(HK_ID, v.sid)
136 //log.Printf("Subscribe Headers: %v\n", sbh)
137 sc, e = conn.Subscribe(sbh)
138 if e != nil {
139 t.Fatalf("TestCodecSendRecvCodec Subscribe failed: %v protocol:%s\n",
140 e, p)
142 if sc == nil {
143 t.Fatalf("TestCodecSendRecvCodec Subscribe sub chan is nil protocol:%s\n",
147 checkReceivedMD(t, conn, sc, "codec_test_"+p) // Receive
148 // Check body data
149 b := md.Message.BodyString()
150 if b != ms {
151 t.Fatalf("TestCodecSendRecvCodec Receive expected: [%v] got: [%v] protocol:%s\n",
152 ms, b, p)
154 // Unsubscribe
155 //log.Printf("Unsubscribe Headers: %v\n", sbh)
156 e = conn.Unsubscribe(sbh)
157 if e != nil {
158 t.Fatalf("TestCodecSendRecvCodec Unsubscribe failed: %v protocol:%s\n",
159 e, p)
161 // Check headers
162 log.Printf("Receive Headers: %v\n", md.Message.Headers)
163 log.Printf("Check map: %v\n", v.rv)
164 for key, value := range v.rv {
165 log.Printf("Want Key: [%v] Value: [%v] \n", key, value)
166 hv, ok = md.Message.Headers.Contains(key)
167 if !ok {
168 t.Fatalf("TestCodecSendRecvCodec Header key expected: [%v] got: [%v] protocol:%s\n",
169 key, hv, p)
171 if value != hv {
172 t.Fatalf("TestCodecSendRecvCodec Header value expected: [%v] got: [%v] protocol:%s\n",
173 value, hv, p)
177 checkReceived(t, conn, false)
178 e = conn.Disconnect(empty_headers)
179 checkDisconnectError(t, e)
180 _ = closeConn(t, n)