This repository has been archived by the owner on Apr 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
ack_test.go
259 lines (249 loc) · 6.84 KB
/
ack_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
//
// Copyright © 2011-2019 Guy M. Allard
//
// Licensed under the Apache License, Veridon 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permisidons and
// limitations under the License.
//
package stompws
import (
"fmt"
"log"
"os"
"testing"
"time"
)
var _ = fmt.Println
/*
Test Ack errors.
*/
func TestAckErrors(t *testing.T) {
n, _ = openConn(t)
ch := login_headers
conn, e = Connect(n, ch)
if e != nil {
t.Fatalf("TestAckErrors CONNECT expected nil, got %v\n", e)
}
//
for _, tv := range terrList {
conn.protocol = tv.proto // Fake it
e = conn.Ack(tv.headers)
if e != tv.errval {
t.Fatalf("ACK -%s- expected error [%v], got [%v]\n",
tv.proto, tv.errval, e)
}
}
checkReceived(t, conn, false)
e = conn.Disconnect(empty_headers)
checkDisconnectError(t, e)
_ = closeConn(t, n)
}
/*
Test Ack Same Connection.
*/
func TestAckSameConn(t *testing.T) {
for _, sp := range Protocols() {
n, _ = openConn(t)
ch := login_headers
ch = headersProtocol(ch, sp)
conn, e = Connect(n, ch)
if e != nil {
t.Fatalf("TestAckSameConn CONNECT expected nil, got %v\n", e)
}
//
// Basic headers
wh := Headers{HK_DESTINATION,
tdest(TEST_TDESTPREF + "acksc1-" + conn.Protocol())}
// Subscribe Headers
sbh := wh.Add(HK_ACK, AckModeClient)
id := TEST_TDESTPREF + "acksc1.chkprotocol-" + conn.Protocol()
sbh = sbh.Add(HK_ID, id) // Always use an 'id'
ms := "acksc1 message 1"
//
// Subscribe
sc, e = conn.Subscribe(sbh)
if e != nil {
t.Fatalf("TestAckSameConn SUBSCRIBE expected [nil], got: [%v]\n", e)
}
//
// Send
sh := wh.Clone()
// For RabbitMQ and STOMP 1.0, do not add current-time header, where the
// value contains ':' characters.
switch conn.Protocol() {
case SPL_10:
if os.Getenv("STOMP_RMQ") == "" {
sh = sh.Add("current-time", time.Now().String()) // The added header value has ':' characters
}
default:
sh = sh.Add("current-time", time.Now().String()) // The added header value has ':' characters
}
e = conn.Send(sh, ms)
if e != nil {
t.Fatalf("TestAckSameConn SEND expected [nil], got: [%v]\n", e)
}
//
// Read MessageData
select {
case md = <-sc:
case md = <-conn.MessageData:
t.Fatalf("TestAckSameConn read channel error: expected [nil], got: [%v] msg: [%v] err: [%v]\n",
md.Message.Command, md.Message, md.Error)
}
if md.Error != nil {
t.Fatalf("TestAckSameConn read error: expected [nil], got: [%v]\n",
md.Error)
}
if ms != md.Message.BodyString() {
t.Fatalf("TestAckSameConn message error: expected: [%v], got: [%v] Message: [%q]\n",
ms, md.Message.BodyString(), md.Message)
}
// Ack headers
ah := Headers{}
if conn.Protocol() == SPL_12 {
ah = ah.Add(HK_ID, md.Message.Headers.Value(HK_ACK))
} else {
ah = ah.Add(HK_MESSAGE_ID, md.Message.Headers.Value(HK_MESSAGE_ID))
}
//
if conn.Protocol() == SPL_11 {
ah = ah.Add(HK_SUBSCRIPTION, id) // Always use subscription for 1.1
}
// Ack
e = conn.Ack(ah)
if e != nil {
t.Fatalf("ACK expected [nil], got: [%v]\n", e)
}
// Make sure Apollo Jira issue APLO-88 stays fixed.
select {
case md = <-sc:
t.Fatalf("TestAckSameConn RECEIVE not expected, got: [%v]\n", md)
default:
}
// Unsubscribe
uh := wh.Add(HK_ID, id)
e = conn.Unsubscribe(uh)
if e != nil {
t.Fatalf("TestAckSameConn UNSUBSCRIBE expected [nil], got: [%v]\n", e)
}
//
checkReceived(t, conn, false)
e = conn.Disconnect(empty_headers)
checkDisconnectError(t, e)
_ = closeConn(t, n)
}
}
/*
Test Ack Different Connection.
*/
func TestAckDiffConn(t *testing.T) {
for _, sp := range Protocols() {
n, _ = openConn(t)
ch := login_headers
ch = headersProtocol(ch, sp)
conn, e = Connect(n, ch)
if e != nil {
t.Fatalf("TestAckDiffConn CONNECT expected nil, got %v\n", e)
}
//
// Basic headers
wh := Headers{HK_DESTINATION,
tdest(TEST_TDESTPREF + "ackdc1-" + conn.Protocol())}
ms := "ackdc1 message 1"
// Send
sh := wh.Clone()
// For RabbitMQ and STOMP 1.0, do not add current-time header, where the
// value contains ':' characters.
switch conn.Protocol() {
case SPL_10:
if os.Getenv("STOMP_RMQ") == "" {
sh = sh.Add("current-time", time.Now().String()) // The added header value has ':' characters
}
default:
sh = sh.Add("current-time", time.Now().String()) // The added header value has ':' characters
}
e = conn.Send(sh, ms)
if e != nil {
t.Fatalf("TestAckDiffConn SEND expected [nil], got: [%v]\n", e)
}
//
checkReceived(t, conn, false)
e = conn.Disconnect(empty_headers)
checkDisconnectError(t, e)
_ = closeConn(t, n)
//
n, _ = openConn(t)
ch = login_headers
ch = headersProtocol(ch, sp)
conn, e = Connect(n, ch) // Reconnect
if e != nil {
t.Fatalf("TestAckDiffConn Second Connect, expected no error, got:<%v>\n", e)
}
//
// Subscribe Headers
sbh := wh.Add(HK_ACK, AckModeClient)
id := "ackdc1.chkprotocol-" + conn.Protocol()
sbh = sbh.Add(HK_ID, id) // Always use an 'id'
// Subscribe
log.Printf("SUB Headers: [%q]\n", sbh)
sc, e = conn.Subscribe(sbh)
if e != nil {
t.Fatalf("TestAckDiffConn SUBSCRIBE expected [nil], got: [%v]\n", e)
}
// Read MessageData
select {
case md = <-sc:
case md = <-conn.MessageData:
t.Fatalf("TestAckDiffConn read channel error: expected [nil], got: [%v], msg: [%v], err: [%v]\n",
md.Message.Command, md.Message, md.Error)
}
if md.Error != nil {
t.Fatalf("read error: expected [nil], got: [%v]\n", md.Error)
}
if ms != md.Message.BodyString() {
t.Fatalf("TestAckDiffConn message error: expected: [%v], got: [%v] Message: [%q]\n",
ms, md.Message.BodyString(), md.Message)
}
// Ack headers
ah := Headers{}
if conn.Protocol() == SPL_12 {
ah = ah.Add(HK_ID, md.Message.Headers.Value(HK_ACK))
} else {
ah = ah.Add(HK_MESSAGE_ID, md.Message.Headers.Value(HK_MESSAGE_ID))
}
//
if conn.Protocol() == SPL_11 {
ah = ah.Add(HK_SUBSCRIPTION, id) // Always use subscription for 1.1
}
// Ack
e = conn.Ack(ah)
if e != nil {
t.Fatalf("TestAckDiffConn ACK expected [nil], got: [%v]\n", e)
}
// Make sure Apollo Jira issue APLO-88 stays fixed.
select {
case md = <-sc:
t.Fatalf("TestAckDiffConn RECEIVE not expected, got: [%v]\n", md)
default:
}
// Unsubscribe
uh := wh.Add(HK_ID, id)
e = conn.Unsubscribe(uh)
if e != nil {
t.Fatalf("TestAckDiffConn UNSUBSCRIBE expected [nil], got: [%v]\n", e)
}
//
checkReceived(t, conn, false)
e = conn.Disconnect(empty_headers)
checkDisconnectError(t, e)
_ = closeConn(t, n)
}
}