Skip to content

Commit 350c562

Browse files
committed
simply examples
1 parent 1dd69d6 commit 350c562

File tree

2 files changed

+16
-17
lines changed

2 files changed

+16
-17
lines changed

consumer_test.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212

1313
func ExampleDialer_Consumer() {
1414
// open connection
15-
dialer, _ := amqpextra.NewDialer(amqpextra.WithURL("amqp://guest:guest@localhost:5672/%2f"))
15+
d, _ := amqpextra.NewDialer(amqpextra.WithURL("amqp://guest:guest@localhost:5672/%2f"))
1616

1717
h := consumer.HandlerFunc(func(ctx context.Context, msg amqp.Delivery) interface{} {
1818
// process message
@@ -21,17 +21,16 @@ func ExampleDialer_Consumer() {
2121
return nil
2222
})
2323

24-
c, _ := dialer.Consumer(
24+
c, _ := d.Consumer(
2525
consumer.WithQueue("a_queue"),
2626
consumer.WithHandler(h),
2727
)
2828

2929
// close consumer
3030
c.Close()
31-
<-c.NotifyClosed()
3231

33-
// close connection
34-
dialer.Close()
32+
// close dialer
33+
d.Close()
3534

3635
// Output:
3736
}

publisher_test.go

+12-12
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,38 @@ package amqpextra_test
33
import (
44
"log"
55

6+
"context"
7+
"time"
8+
69
"github.com/makasim/amqpextra"
710
"github.com/makasim/amqpextra/publisher"
811
"github.com/streadway/amqp"
912
)
1013

1114
func ExampleDialer_Publisher() {
1215
// open connection
13-
dialer, err := amqpextra.NewDialer(amqpextra.WithURL("amqp://guest:guest@localhost:5672/%2f"))
14-
if err != nil {
15-
log.Fatal(err)
16-
}
16+
d, _ := amqpextra.NewDialer(amqpextra.WithURL("amqp://guest:guest@localhost:5672/%2f"))
1717

1818
// create publisher
19-
p, err := dialer.Publisher()
20-
if err != nil {
21-
log.Fatal(err)
22-
}
19+
p, _ := d.Publisher()
20+
21+
ctx, cancelFunc := context.WithTimeout(context.Background(), time.Millisecond*100)
22+
defer cancelFunc()
2323

2424
// publish a message
25-
go p.Publish(publisher.Message{
26-
Key: "test_queue",
25+
p.Publish(publisher.Message{
26+
Key: "test_queue",
27+
Context: ctx,
2728
Publishing: amqp.Publishing{
2829
Body: []byte(`{"foo": "fooVal"}`),
2930
},
3031
})
3132

3233
// close publisher
3334
p.Close()
34-
<-p.NotifyClosed()
3535

3636
// close connection
37-
dialer.Close()
37+
d.Close()
3838

3939
// Output:
4040
}

0 commit comments

Comments
 (0)