Rework writer deadline handling.
[stompngo.git] / codec_test.go
blob206b7e6c7871d88e5325f036ad1ec41029ea5a7f
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 "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) {
81 for _, p := range Protocols() {
82 usemap := srcdmap[p]
83 //log.Printf("Protocol: %s\n", p)
84 //log.Printf("MapLen: %d\n", len(usemap))
85 for _, v := range usemap {
88 // RMQ and STOMP Level 1.0 :
89 // Headers are encoded (as if the STOMP protocol were 1.1
90 // or 1.2).
91 // MAYBEDO: Report issue. (Is this a bug or a feature?)
93 if p == SPL_10 && os.Getenv("STOMP_RMQ") != "" {
94 continue
97 n, _ = openConn(t)
98 ch := login_headers
99 ch = headersProtocol(ch, p)
100 conn, e = Connect(n, ch)
101 if e != nil {
102 t.Fatalf("TestCodecSendRecvCodec CONNECT expected nil, got %v\n", e)
105 d := tdest("/queue/gostomp.codec.sendrecv.1.protocol." + p)
106 ms := "msg.codec.sendrecv.1.protocol." + p + " - a message"
107 wh := Headers{HK_DESTINATION, d}
109 //log.Printf("TestData: %+v\n", v)
110 sh := wh.Clone()
111 for i := range v.sk {
112 sh = sh.Add(v.sk[i], v.sv[i])
114 // Send
115 //log.Printf("Send Headers: %v\n", sh)
116 e = conn.Send(sh, ms)
117 if e != nil {
118 t.Fatalf("TestCodecSendRecvCodec Send failed: %v protocol:%s\n",
119 e, p)
121 // Check for ERROR frame
122 time.Sleep(1e9 / 8) // Wait one eigth
123 // Poll for adhoc ERROR from server
124 select {
125 case vx := <-conn.MessageData:
126 t.Fatalf("TestCodecSendRecvCodec Send Error: [%v] protocol:%s\n",
127 vx, p)
128 default:
131 // Subscribe
132 sbh := wh.Add(HK_ID, v.sid)
133 //log.Printf("Subscribe Headers: %v\n", sbh)
134 sc, e = conn.Subscribe(sbh)
135 if e != nil {
136 t.Fatalf("TestCodecSendRecvCodec Subscribe failed: %v protocol:%s\n",
137 e, p)
139 if sc == nil {
140 t.Fatalf("TestCodecSendRecvCodec Subscribe sub chan is nil protocol:%s\n",
144 checkReceivedMD(t, conn, sc, "codec_test_"+p) // Receive
145 // Check body data
146 b := md.Message.BodyString()
147 if b != ms {
148 t.Fatalf("TestCodecSendRecvCodec Receive expected: [%v] got: [%v] protocol:%s\n",
149 ms, b, p)
151 // Unsubscribe
152 //log.Printf("Unsubscribe Headers: %v\n", sbh)
153 e = conn.Unsubscribe(sbh)
154 if e != nil {
155 t.Fatalf("TestCodecSendRecvCodec Unsubscribe failed: %v protocol:%s\n",
156 e, p)
158 // Check headers
159 //log.Printf("Receive Headers: %v\n", md.Message.Headers)
160 for key, value := range v.rv {
161 hv, ok = md.Message.Headers.Contains(key)
162 if !ok {
163 t.Fatalf("TestCodecSendRecvCodec Header key expected: [%v] got: [%v] protocol:%s\n",
164 key, ok, p)
166 if value != hv {
167 t.Fatalf("TestCodecSendRecvCodec Header value expected: [%v] got: [%v] protocol:%s\n",
168 value, hv, p)
172 checkReceived(t, conn)
173 e = conn.Disconnect(empty_headers)
174 checkDisconnectError(t, e)
175 _ = closeConn(t, n)