Reformat: comments.
[stompngo.git] / sub_test.go
blob8c837b60cb414d8b8c41d9dada78fbe9644fc706
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 "fmt"
21 "log"
22 //"os"
23 "testing"
24 //"time"
27 func TestSubNoHeader(t *testing.T) {
28 n, _ = openConn(t)
29 ch := login_headers
30 ch = headersProtocol(ch, SPL_10) // Start with 1.0
31 conn, e = Connect(n, ch)
32 if e != nil {
33 t.Fatalf("CONNECT Failed: e:<%q> connresponse:<%q>\n", e,
34 conn.ConnectResponse)
37 for ti, tv := range subNoHeaderDataList {
38 conn.protocol = tv.proto // Cheat, fake all protocols
39 _, e = conn.Subscribe(empty_headers)
40 if e == nil {
41 t.Fatalf("TestSubNoHeader[%d] proto:%s expected:%v got:nil\n",
42 ti, tv.proto, tv.exe)
44 if e != tv.exe {
45 t.Fatalf("TestSubNoHeader[%d] proto:%s expected:%v got:%v\n",
46 ti, tv.proto, tv.exe, e)
50 e = conn.Disconnect(empty_headers)
51 checkDisconnectError(t, e)
52 _ = closeConn(t, n)
53 log.Printf("TestSubNoHeader %d tests complete.\n", len(subNoHeaderDataList))
56 func TestSubNoID(t *testing.T) {
57 n, _ = openConn(t)
58 ch := login_headers
59 ch = headersProtocol(ch, SPL_10) // Start with 1.0
60 conn, e = Connect(n, ch)
61 if e != nil {
62 t.Fatalf("CONNECT Failed: e:<%q> connresponse:<%q>\n", e,
63 conn.ConnectResponse)
66 for ti, tv := range subNoIDDataList {
67 conn.protocol = tv.proto // Cheat, fake all protocols
68 ud := tdest(tv.subh.Value(HK_DESTINATION))
69 _, e = conn.Subscribe(Headers{HK_DESTINATION, ud})
70 if e != tv.exe {
71 t.Fatalf("TestSubNoID[%d] proto:%s expected:%v got:%v\n",
72 ti, tv.proto, tv.exe, e)
76 e = conn.Disconnect(empty_headers)
77 checkDisconnectError(t, e)
78 _ = closeConn(t, n)
79 log.Printf("TestSubNoID %d tests complete.\n", len(subNoIDDataList))
82 func TestSubPlain(t *testing.T) {
83 for ti, tv := range subPlainDataList {
84 n, _ = openConn(t)
85 ch := login_headers
86 ch = headersProtocol(ch, tv.proto)
87 conn, e = Connect(n, ch)
88 if e != nil {
89 t.Fatalf("CONNECT Failed: e:<%q> connresponse:<%q>\n", e,
90 conn.ConnectResponse)
93 // SUBSCRIBE Phase
94 sh := fixHeaderDest(tv.subh) // destination fixed if needed
95 sc, e = conn.Subscribe(sh)
96 if sc == nil {
97 t.Fatalf("TestSubPlain[%d] SUBSCRIBE, proto:[%s], channel is nil\n",
98 ti, tv.proto)
100 if e != tv.exe1 {
101 t.Fatalf("TestSubPlain[%d] SUBSCRIBE, proto:%s expected:%v got:%v\n",
102 ti, tv.proto, tv.exe1, e)
105 // UNSUBSCRIBE Phase
106 sh = fixHeaderDest(tv.unsubh) // destination fixed if needed
107 e = conn.Unsubscribe(sh)
108 if e != tv.exe2 {
109 t.Fatalf("TestSubPlain[%d] UNSUBSCRIBE, proto:%s expected:%v got:%v\n",
110 ti, tv.proto, tv.exe2, e)
113 e = conn.Disconnect(empty_headers)
114 checkDisconnectError(t, e)
115 _ = closeConn(t, n)
117 log.Printf("TestSubPlain %d tests complete.\n", len(subPlainDataList))
120 func TestSubNoTwice(t *testing.T) {
121 for ti, tv := range subTwiceDataList {
122 n, _ = openConn(t)
123 ch := login_headers
124 ch = headersProtocol(ch, tv.proto)
125 conn, e = Connect(n, ch)
126 if e != nil {
127 t.Fatalf("CONNECT Failed: e:<%q> connresponse:<%q>\n", e,
128 conn.ConnectResponse)
131 // SUBSCRIBE Phase 1
132 sh := fixHeaderDest(tv.subh) // destination fixed if needed
133 sc, e = conn.Subscribe(sh)
134 if sc == nil {
135 t.Fatalf("TestSubNoTwice[%d] SUBSCRIBE1, proto:[%s], channel is nil\n",
136 ti, tv.proto)
138 if e != tv.exe1 {
139 t.Fatalf("TestSubNoTwice[%d] SUBSCRIBE1, proto:%s expected:%v got:%v\n",
140 ti, tv.proto, tv.exe1, e)
143 // SUBSCRIBE Phase 2
144 sc, e = conn.Subscribe(sh)
145 if e != tv.exe2 {
146 t.Fatalf("TestSubNoTwice[%d] SUBSCRIBE2, proto:%s expected:%v got:%v\n",
147 ti, tv.proto, tv.exe2, e)
150 e = conn.Disconnect(empty_headers)
151 checkDisconnectError(t, e)
152 _ = closeConn(t, n)
154 log.Printf("TestSubNoTwice %d tests complete.\n", len(subTwiceDataList))
157 func TestSubRoundTrip(t *testing.T) {
158 for ti, tv := range subPlainDataList { // *NOTE* Use the PlainData table
159 n, _ = openConn(t)
160 ch := login_headers
161 ch = headersProtocol(ch, tv.proto)
162 conn, e = Connect(n, ch)
163 if e != nil {
164 t.Fatalf("CONNECT Failed: e:<%q> connresponse:<%q>\n", e,
165 conn.ConnectResponse)
167 sh := fixHeaderDest(tv.subh) // destination fixed if needed
169 // SEND Phase
170 msg := "SUBROUNDTRIP: " + tv.proto
171 nh := Headers{HK_DESTINATION, sh.Value(HK_DESTINATION)}
172 e = conn.Send(nh, msg)
173 if e != nil {
174 t.Fatalf("TestSubRoundTrip[%d] SEND, proto:%s expected:%v got:%v\n",
175 ti, tv.proto, nil, e)
178 // SUBSCRIBE Phase
179 sc, e = conn.Subscribe(sh)
180 if sc == nil {
181 t.Fatalf("TestSubRoundTrip[%d] SUBSCRIBE, proto:[%s], channel is nil\n",
182 ti, tv.proto)
184 if e != tv.exe1 {
185 t.Fatalf("TestSubRoundTrip[%d] SUBSCRIBE, proto:%s expected:%v got:%v\n",
186 ti, tv.proto, tv.exe1, e)
189 // RECEIVE Phase
190 id := fmt.Sprintf("TestSubRoundTrip[%d] RECEIVE, proto:%s", ti, tv.proto)
191 checkReceivedMD(t, conn, sc, id)
192 if msg != md.Message.BodyString() {
193 t.Fatalf("TestSubRoundTrip[%d] RECEIVE, proto:%s expected:%v got:%v\n",
194 ti, tv.proto, msg, md.Message.BodyString())
197 // UNSUBSCRIBE Phase
198 e = conn.Unsubscribe(sh)
199 if e != tv.exe2 {
200 t.Fatalf("TestSubRoundTrip[%d] UNSUBSCRIBE, proto:%s expected:%v got:%v\n",
201 ti, tv.proto, tv.exe2, e)
204 e = conn.Disconnect(empty_headers)
205 checkDisconnectError(t, e)
206 _ = closeConn(t, n)
208 log.Printf("TestSubRoundTrip %d tests complete.\n", len(subPlainDataList))
211 func TestSubAckModes(t *testing.T) {
212 for ti, tv := range subAckDataList {
213 n, _ = openConn(t)
214 ch := login_headers
215 ch = headersProtocol(ch, tv.proto)
216 conn, e = Connect(n, ch)
217 if e != nil {
218 t.Fatalf("CONNECT Failed: e:<%q> connresponse:<%q>\n", e,
219 conn.ConnectResponse)
222 // SUBSCRIBE Phase 1
223 sh := fixHeaderDest(tv.subh) // destination fixed if needed
224 sc, e = conn.Subscribe(sh)
225 if e == nil {
226 if sc == nil {
227 t.Fatalf("TestSubAckModes[%d] SUBSCRIBE, proto:[%s], channel is nil\n",
228 ti, tv.proto)
231 if e != tv.exe {
232 t.Fatalf("TestSubAckModes[%d] SUBSCRIBE, proto:%s expected:%v got:%v\n",
233 ti, tv.proto, tv.exe, e)
236 e = conn.Disconnect(empty_headers)
237 checkDisconnectError(t, e)
238 _ = closeConn(t, n)
240 log.Printf("TestSubAckModes %d tests complete.\n", len(subAckDataList))