2 // Copyright © 2011-2016 Guy M. Allard
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
8 // http://www.apache.org/licenses/LICENSE-2.0
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.
27 Test Subscribe, no destination.
29 func TestSubNoDest(t
*testing
.T
) {
32 ch
:= check11(TEST_HEADERS
)
33 conn
, _
:= Connect(n
, ch
)
35 _
, e
:= conn
.Subscribe(empty_headers
)
37 t
.Fatalf("Expected subscribe error, got [nil]\n")
40 t
.Fatalf("Subscribe error, expected [%v], got [%v]\n", EREQDSTSUB
, e
)
43 _
= conn
.Disconnect(empty_headers
)
48 Test subscribe, no ID.
50 func TestSubNoIdOnce(t
*testing
.T
) {
53 ch
:= check11(TEST_HEADERS
)
54 conn
, _
:= Connect(n
, ch
)
56 d
:= tdest("/queue/subunsub.genl.01")
57 sbh
:= Headers
{HK_DESTINATION
, d
}
59 s
, e
:= conn
.Subscribe(sbh
)
61 t
.Fatalf("Expected no subscribe error, got [%v]\n", e
)
64 t
.Fatalf("Expected subscribe channel, got [nil]\n")
67 case v
:= <-conn
.MessageData
:
68 t
.Fatalf("Unexpected frame received, got [%v]\n", v
)
72 _
= conn
.Disconnect(empty_headers
)
77 Test subscribe, no ID, twice to same destination, protocol level 1.0.
79 func TestSubNoIdTwice10(t
*testing
.T
) {
80 if os
.Getenv("STOMP_TEST11p") != "" {
81 t
.Skip("TestSubNoIdTwice10 norun, need 1.0")
84 t
.Log("TestSubNoIdTwice10", "starts")
86 ch
:= check11(TEST_HEADERS
)
87 conn
, _
:= Connect(n
, ch
)
88 //l := log.New(os.Stdout, "", log.Ldate|log.Lmicroseconds)
91 if conn
.Protocol() != SPL_10
{
92 t
.Fatalf("Protocol error, got [%v], expected [%v]\n", conn
.Protocol(), SPL_10
)
95 d
:= tdest("/queue/subdup.p10.01")
96 sbh
:= Headers
{HK_DESTINATION
, d
}
98 sc
, e
:= conn
.Subscribe(sbh
)
100 t
.Fatalf("Expected no subscribe error (T1), got [%v]\n", e
)
103 t
.Fatalf("Expected subscribe channel (T1), got [nil]\n")
105 time
.Sleep(500 * time
.Millisecond
) // give a broker a break
108 t
.Fatalf("Unexpected frame received (T1), got [%v]\n", v
)
109 case v
:= <-conn
.MessageData
:
110 t
.Fatalf("Unexpected frame received (T1), got [%v]\n", v
)
114 sc
, e
= conn
.Subscribe(sbh
)
116 t
.Fatalf("Expected no subscribe error (T2), got [%v]\n", e
)
119 t
.Fatalf("Expected no subscribe error (T2), got [%v]\n", e
)
122 t
.Fatalf("Expected subscribe channel (T2), got nil\n")
124 time
.Sleep(500 * time
.Millisecond
) // give a broker a break
125 // Stomp 1.0 brokers are allowed significant latitude regarding a response
126 // to a duplicate subscription request. Currently, only do these checks for
127 // brokers other than AMQ. AMQ does not return an ERROR frame for duplicate
128 // subscriptions with 1.0, choosing to ignore it.
129 // Apollo and RabbitMQ both return an ERROR frame *and* tear down the
131 if os
.Getenv("STOMP_APOLLO") != "" || os
.Getenv("STOMP_RMQ") != "" {
132 // fmt.Println("sccheck runs ....", conn.Connected())
135 t
.Logf("Server frame expected and received (T2-A), got [%v] [%v] [%v] [%s]\n",
136 v
.Message
.Command
, v
.Error
, v
.Message
.Headers
, string(v
.Message
.Body
))
137 case v
:= <-conn
.MessageData
:
138 t
.Logf("Server frame expected and received (T2-B), got [%v] [%v] [%v] [%s]\n",
139 v
.Message
.Command
, v
.Error
, v
.Message
.Headers
, string(v
.Message
.Body
))
141 t
.Fatalf("Server frame expected (T2-E), not received.\n")
144 // For both Apollo and RabbitMQ, the connection teardown by the server can
145 // mean the client side connection is no longer usable.
146 if os
.Getenv("STOMP_APOLLO") == "" && os
.Getenv("STOMP_RMQ") == "" {
147 _
= conn
.Disconnect(empty_headers
)
150 t
.Log("TestSubNoIdTwice10", "ends")
154 Test subscribe, no ID, twice to same destination, protocol level 1.1+.
156 func TestSubNoIdTwice11p(t
*testing
.T
) {
157 if os
.Getenv("STOMP_TEST11p") == "" {
158 t
.Skip("TestSubNoIdTwice11p norun, need 1.1+")
161 t
.Log("TestSubNoIdTwice11p", "starts")
163 ch
:= check11(TEST_HEADERS
)
164 conn
, _
:= Connect(n
, ch
)
165 l
:= log
.New(os
.Stdout
, "TSNI211P ", log
.Ldate|log
.Lmicroseconds
)
168 d
:= tdest("/queue/subdup.p11.01")
169 id
:= "TestSubNoIdTwice11p"
170 sbh
:= Headers
{HK_DESTINATION
, d
, HK_ID
, id
}
172 t
.Logf("%s\n", "INFO TestSubNoIdTwice11p - start 1st SUBSCRIBE")
173 sc
, e
:= conn
.Subscribe(sbh
)
174 t
.Logf("%s\n", "INFO TestSubNoIdTwice11p - end 1st SUBSCRIBE")
176 t
.Fatalf("ERROR Expected no subscribe error (T1), got [%v]\n", e
)
179 t
.Fatalf("ERROR Expected subscribe channel (T2), got [nil]\n")
181 time
.Sleep(500 * time
.Millisecond
) // give a broker a break
184 t
.Fatalf("ERROR Unexpected frame received (T3), got [%v]\n", v
)
185 case v
:= <-conn
.MessageData
:
186 t
.Fatalf("ERROR Unexpected frame received (T4), got [%v]\n", v
)
190 // Second time. The stompngo package maintains a list of all current
191 // subscription ids. An attempt to subscribe using an existing id is
192 // immediately rejected by the package (never actually sent to the broker).
193 t
.Logf("%s\n", "INFO TestSubNoIdTwice11p - start 2nd SUBSCRIBE")
194 sc
, e
= conn
.Subscribe(sbh
)
195 t
.Logf("%s\n", "INFO TestSubNoIdTwice11p - end 2nd SUBSCRIBE")
197 t
.Fatalf("ERROR Expected subscribe error (T5), got [nil]\n")
200 t
.Fatalf("ERROR Expected subscribe error (T6), [%v] got [%v]\n", EDUPSID
, e
)
202 t
.Logf("INFO wanted/got actual (T7), [%v]\n", e
)
205 t
.Fatalf("ERROR Expected nil subscribe channel (T8), got [%v]\n", sc
)
207 _
= conn
.Disconnect(empty_headers
)
209 t
.Log("TestSubNoIdTwice11p", "ends")
213 Test send, subscribe, read, unsubscribe.
215 func TestSubUnsubBasic(t
*testing
.T
) {
218 ch
:= check11(TEST_HEADERS
)
219 conn
, _
:= Connect(n
, ch
)
222 d
:= tdest("/queue/subunsub.basiconn.01")
223 h
:= Headers
{HK_DESTINATION
, d
}
227 sc
, e
:= conn
.Subscribe(h
)
229 t
.Fatalf("Expected no subscribe error, got [%v]\n", e
)
232 t
.Fatalf("Expected subscribe channel, got [nil]\n")
239 case md
= <-conn
.MessageData
:
240 t
.Fatalf("read channel error: expected [nil], got: [%v]\n",
246 t
.Fatalf("Expected no message data error, got [%v]\n", md
.Error
)
249 rd
:= mdm
.Headers
.Value(HK_DESTINATION
)
251 t
.Fatalf("Expected destination [%v], got [%v]\n", d
, rd
)
253 ri
:= mdm
.Headers
.Value(HK_SUBSCRIPTION
)
255 t
.Fatalf("Expected subscription [%v], got [%v]\n", d
, ri
)
258 e
= conn
.Unsubscribe(h
)
260 t
.Fatalf("Expected no unsubscribe error, got [%v]\n", e
)
263 _
= conn
.Disconnect(empty_headers
)
268 Test send, subscribe, read, unsubscribe, 1.0 only, no sub id.
270 func TestSubUnsubBasic10(t
*testing
.T
) {
271 if os
.Getenv("STOMP_TEST11p") != "" {
272 t
.Skip("TestSubUnsubBasic10 norun, need 1.0")
276 ch
:= check11(TEST_HEADERS
)
277 conn
, _
:= Connect(n
, ch
)
280 d
:= tdest("/queue/subunsub.basiconn.r10.01")
281 sh
:= Headers
{HK_DESTINATION
, d
}
282 _
= conn
.Send(sh
, ms
)
285 sc
, e
:= conn
.Subscribe(sbh
)
287 t
.Fatalf("Expected no subscribe error, got [%v]\n", e
)
290 t
.Fatalf("Expected subscribe channel, got [nil]\n")
297 case md
= <-conn
.MessageData
:
298 t
.Fatalf("read channel error: expected [nil], got: [%v]\n",
304 t
.Fatalf("Expected no message data error, got [%v]\n", md
.Error
)
307 rd
:= mdm
.Headers
.Value(HK_DESTINATION
)
309 t
.Fatalf("Expected destination [%v], got [%v]\n", d
, rd
)
312 e
= conn
.Unsubscribe(sbh
)
314 t
.Fatalf("Expected no unsubscribe error, got [%v]\n", e
)
317 _
= conn
.Disconnect(empty_headers
)
322 Test establishSubscription.
324 func TestSubEstablishSubscription(t
*testing
.T
) {
327 ch
:= check11(TEST_HEADERS
)
328 conn
, _
:= Connect(n
, ch
)
330 d
:= tdest("/queue/estabsub.01")
331 sbh
:= Headers
{HK_DESTINATION
, d
}
333 s
, e
:= conn
.Subscribe(sbh
)
335 t
.Fatalf("Expected no subscribe error, got [%v]\n", e
)
338 t
.Fatalf("Expected subscribe channel, got [nil]\n")
341 _
= conn
.Disconnect(empty_headers
)
346 Test unsubscribe, set subscribe channel capacity.
348 func TestSubSetCap(t
*testing
.T
) {
349 if os
.Getenv("STOMP_TEST11p") == "" {
350 t
.Skip("TestSubSetCap norun, need 1.1+")
355 ch
:= check11(TEST_HEADERS
)
356 conn
, _
:= Connect(n
, ch
)
359 conn
.SetSubChanCap(p
)
360 r
:= conn
.SubChanCap()
362 t
.Fatalf("Expected get capacity [%v], got [%v]\n", p
, r
)
365 d
:= tdest("/queue/subsetcap.basiconn.01")
366 h
:= Headers
{HK_DESTINATION
, d
, HK_ID
, d
}
367 s
, e
:= conn
.Subscribe(h
)
369 t
.Fatalf("Expected no subscribe error, got [%v]\n", e
)
372 t
.Fatalf("Expected subscribe channel, got [nil]\n")
375 t
.Fatalf("Expected subchan capacity [%v], got [%v]\n", p
, cap(s
))
378 e
= conn
.Unsubscribe(h
)
380 t
.Fatalf("Expected no unsubscribe error, got [%v]\n", e
)
383 _
= conn
.Disconnect(empty_headers
)