Skip to content

Commit

Permalink
simply examples
Browse files Browse the repository at this point in the history
  • Loading branch information
makasim committed Dec 26, 2020
1 parent 1dd69d6 commit 350c562
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 17 deletions.
9 changes: 4 additions & 5 deletions consumer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

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

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

c, _ := dialer.Consumer(
c, _ := d.Consumer(
consumer.WithQueue("a_queue"),
consumer.WithHandler(h),
)

// close consumer
c.Close()
<-c.NotifyClosed()

// close connection
dialer.Close()
// close dialer
d.Close()

// Output:
}
Expand Down
24 changes: 12 additions & 12 deletions publisher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,38 @@ package amqpextra_test
import (
"log"

"context"
"time"

"github.com/makasim/amqpextra"
"github.com/makasim/amqpextra/publisher"
"github.com/streadway/amqp"
)

func ExampleDialer_Publisher() {
// open connection
dialer, err := amqpextra.NewDialer(amqpextra.WithURL("amqp://guest:guest@localhost:5672/%2f"))
if err != nil {
log.Fatal(err)
}
d, _ := amqpextra.NewDialer(amqpextra.WithURL("amqp://guest:guest@localhost:5672/%2f"))

// create publisher
p, err := dialer.Publisher()
if err != nil {
log.Fatal(err)
}
p, _ := d.Publisher()

ctx, cancelFunc := context.WithTimeout(context.Background(), time.Millisecond*100)
defer cancelFunc()

// publish a message
go p.Publish(publisher.Message{
Key: "test_queue",
p.Publish(publisher.Message{
Key: "test_queue",
Context: ctx,
Publishing: amqp.Publishing{
Body: []byte(`{"foo": "fooVal"}`),
},
})

// close publisher
p.Close()
<-p.NotifyClosed()

// close connection
dialer.Close()
d.Close()

// Output:
}
Expand Down

0 comments on commit 350c562

Please sign in to comment.