Skip to content

Commit 6deeeed

Browse files
committed
fix: test examples
Signed-off-by: Hiram J. Pérez <hi@worg.xyz>
1 parent fc01894 commit 6deeeed

File tree

1 file changed

+44
-58
lines changed

1 file changed

+44
-58
lines changed

example_test.go

Lines changed: 44 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,32 @@ import (
99
"github.com/go-stomp/stomp/v3/frame"
1010
)
1111

12-
func ExampleConn_Send(c *stomp.Conn) error {
12+
func handle(_ error) {}
13+
14+
func ExampleConn_Send() {
15+
c, err := stomp.Dial("tcp", "localhost:61613")
16+
if err != nil {
17+
handle(err)
18+
}
19+
1320
// send with receipt and an optional header
14-
err := c.Send(
21+
err = c.Send(
1522
"/queue/test-1", // destination
1623
"text/plain", // content-type
1724
[]byte("Message number 1"), // body
1825
stomp.SendOpt.Receipt,
1926
stomp.SendOpt.Header("expires", "2049-12-31 23:59:59"))
2027
if err != nil {
21-
return err
28+
handle(err)
2229
}
2330

2431
// send with no receipt and no optional headers
2532
err = c.Send("/queue/test-2", "application/xml",
2633
[]byte("<message>hello</message>"))
2734
if err != nil {
28-
return err
35+
handle(err)
2936
}
3037

31-
return nil
3238
}
3339

3440
// Creates a new Header.
@@ -49,27 +55,6 @@ func ExampleNewHeader() {
4955
doSomethingWith(h)
5056
}
5157

52-
// Creates a STOMP frame.
53-
func ExampleNewFrame() {
54-
/*
55-
Creates a STOMP frame that looks like the following:
56-
57-
CONNECT
58-
login:scott
59-
passcode:tiger
60-
host:stompserver
61-
accept-version:1.1,1.2
62-
63-
^@
64-
*/
65-
f := frame.New("CONNECT",
66-
"login", "scott",
67-
"passcode", "tiger",
68-
"host", "stompserver",
69-
"accept-version", "1.1,1.2")
70-
doSomethingWith(f)
71-
}
72-
7358
func doSomethingWith(f ...interface{}) {
7459

7560
}
@@ -78,78 +63,81 @@ func doAnotherThingWith(f interface{}, g interface{}) {
7863

7964
}
8065

81-
func ExampleConn_Subscribe_1() error {
66+
func ExampleConn_Subscribe() {
8267
conn, err := stomp.Dial("tcp", "localhost:61613")
8368
if err != nil {
84-
return err
69+
handle(err)
8570
}
8671

8772
sub, err := conn.Subscribe("/queue/test-2", stomp.AckClient)
8873
if err != nil {
89-
return err
74+
handle(err)
9075
}
9176

9277
// receive 5 messages and then quit
9378
for i := 0; i < 5; i++ {
9479
msg := <-sub.C
9580
if msg.Err != nil {
96-
return msg.Err
81+
handle(msg.Err)
9782
}
9883

9984
doSomethingWith(msg)
10085

10186
// acknowledge the message
10287
err = conn.Ack(msg)
10388
if err != nil {
104-
return err
89+
handle(err)
10590
}
10691
}
10792

10893
err = sub.Unsubscribe()
10994
if err != nil {
110-
return err
95+
handle(err)
11196
}
11297

113-
return conn.Disconnect()
98+
conn.Disconnect()
11499
}
115100

116101
// Example of creating subscriptions with various options.
117-
func ExampleConn_Subscribe_2(c *stomp.Conn) error {
102+
func ExampleConn_Subscribe_with_options() {
103+
c, err := stomp.Dial("tcp", "localhost:61613")
104+
if err != nil {
105+
handle(err)
106+
}
107+
118108
// Subscribe to queue with automatic acknowledgement
119109
sub1, err := c.Subscribe("/queue/test-1", stomp.AckAuto)
120110
if err != nil {
121-
return err
111+
handle(err)
122112
}
123113

124114
// Subscribe to queue with client acknowledgement and a custom header value
125115
sub2, err := c.Subscribe("/queue/test-2", stomp.AckClient,
126116
stomp.SubscribeOpt.Header("x-custom-header", "some-value"))
127117
if err != nil {
128-
return err
118+
handle(err)
129119
}
130120

131121
doSomethingWith(sub1, sub2)
132-
133-
return nil
134122
}
135123

136-
func ExampleTransaction() error {
124+
func ExampleTransaction() {
137125
conn, err := stomp.Dial("tcp", "localhost:61613")
138126
if err != nil {
139-
return err
127+
handle(err)
140128
}
141129
defer conn.Disconnect()
142130

143131
sub, err := conn.Subscribe("/queue/test-2", stomp.AckClient)
144132
if err != nil {
145-
return err
133+
handle(err)
146134
}
147135

148136
// receive 5 messages and then quit
149137
for i := 0; i < 5; i++ {
150138
msg := <-sub.C
151139
if msg.Err != nil {
152-
return msg.Err
140+
handle(msg.Err)
153141
}
154142

155143
tx := conn.Begin()
@@ -162,81 +150,79 @@ func ExampleTransaction() error {
162150
// acknowledge the message
163151
err = tx.Ack(msg)
164152
if err != nil {
165-
return err
153+
handle(err)
166154
}
167155

168156
err = tx.Commit()
169157
if err != nil {
170-
return err
158+
handle(err)
171159
}
172160
}
173161

174162
err = sub.Unsubscribe()
175163
if err != nil {
176-
return err
164+
handle(err)
177165
}
178166

179-
return nil
180167
}
181168

182169
// Example of connecting to a STOMP server using an existing network connection.
183-
func ExampleConnect() error {
170+
func ExampleConnect() {
184171
netConn, err := net.DialTimeout("tcp", "stomp.server.com:61613", 10*time.Second)
185172
if err != nil {
186-
return err
173+
handle(err)
187174
}
188175

189176
stompConn, err := stomp.Connect(netConn)
190177
if err != nil {
191-
return err
178+
handle(err)
192179
}
193180

194181
defer stompConn.Disconnect()
195182

196183
doSomethingWith(stompConn)
197-
return nil
198184
}
199185

200186
// Connect to a STOMP server using default options.
201-
func ExampleDial_1() error {
187+
func ExampleDial() {
202188
conn, err := stomp.Dial("tcp", "192.168.1.1:61613")
203189
if err != nil {
204-
return err
190+
handle(err)
205191
}
206192

207193
err = conn.Send(
208194
"/queue/test-1", // destination
209195
"text/plain", // content-type
210196
[]byte("Test message #1")) // body
211197
if err != nil {
212-
return err
198+
handle(err)
213199
}
214200

215-
return conn.Disconnect()
201+
conn.Disconnect()
216202
}
217203

218204
// Connect to a STOMP server that requires authentication. In addition,
219205
// we are only prepared to use STOMP protocol version 1.1 or 1.2, and
220206
// the virtual host is named "dragon". In this example the STOMP
221207
// server also accepts a non-standard header called 'nonce'.
222-
func ExampleDial_2() error {
208+
func ExampleDial_with_options() {
223209
conn, err := stomp.Dial("tcp", "192.168.1.1:61613",
224210
stomp.ConnOpt.Login("scott", "leopard"),
225211
stomp.ConnOpt.AcceptVersion(stomp.V11),
226212
stomp.ConnOpt.AcceptVersion(stomp.V12),
227213
stomp.ConnOpt.Host("dragon"),
228214
stomp.ConnOpt.Header("nonce", "B256B26D320A"))
229215
if err != nil {
230-
return err
216+
handle(err)
231217
}
232218

233219
err = conn.Send(
234220
"/queue/test-1", // destination
235221
"text/plain", // content-type
236222
[]byte("Test message #1")) // body
237223
if err != nil {
238-
return err
224+
handle(err)
239225
}
240226

241-
return conn.Disconnect()
227+
conn.Disconnect()
242228
}

0 commit comments

Comments
 (0)