Skip to content

Commit

Permalink
📝 use WithContext in example tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sapk committed Jul 11, 2022
1 parent 36d74cc commit 4e4daa9
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
7 changes: 6 additions & 1 deletion example_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package amqp091_test

import (
"context"
"errors"
"fmt"
"log"
Expand Down Expand Up @@ -243,7 +244,11 @@ func (client *Client) UnsafePush(data []byte) error {
return errNotConnected
}

return client.channel.Publish(
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()

return client.channel.PublishWithContext(
ctx,
"", // Exchange
client.queueName, // Routing key
false, // Mandatory
Expand Down
11 changes: 9 additions & 2 deletions examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package amqp091_test

import (
"context"
"crypto/tls"
"crypto/x509"
"io/ioutil"
Expand Down Expand Up @@ -168,6 +169,9 @@ func ExampleChannel_Confirm_bridge() {
log.Fatalf("confirm.select destination: %s", err)
}

ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()

// Now pump the messages, one by one, a smarter implementation
// would batch the deliveries and use multiple ack/nacks
for {
Expand All @@ -176,7 +180,7 @@ func ExampleChannel_Confirm_bridge() {
log.Fatalf("source channel closed, see the reconnect example for handling this")
}

err = chd.Publish("logs", msg.RoutingKey, false, false, amqp.Publishing{
err = chd.PublishWithContext(ctx, "logs", msg.RoutingKey, false, false, amqp.Publishing{
// Copy all the properties
ContentType: msg.ContentType,
ContentEncoding: msg.ContentEncoding,
Expand Down Expand Up @@ -375,9 +379,12 @@ func ExampleChannel_Publish() {
Body: []byte("Go Go AMQP!"),
}

ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()

// This is not a mandatory delivery, so it will be dropped if there are no
// queues bound to the logs exchange.
err = c.Publish("logs", "info", false, false, msg)
err = c.PublishWithContext(ctx, "logs", "info", false, false, msg)
if err != nil {
// Since publish is asynchronous this can happen if the network connection
// is reset or if the server has run out of resources.
Expand Down

0 comments on commit 4e4daa9

Please sign in to comment.