Skip to content

Commit

Permalink
examples: Replace context.Background with context.WithTimeout (#1877)
Browse files Browse the repository at this point in the history
  • Loading branch information
menghanl authored and dfawley committed Feb 28, 2018
1 parent a1de3b2 commit f0a1202
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 11 deletions.
8 changes: 4 additions & 4 deletions examples/gotutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ Now let's look at how we call our service methods. Note that in gRPC-Go, RPCs op
Calling the simple RPC `GetFeature` is nearly as straightforward as calling a local method.

```go
feature, err := client.GetFeature(context.Background(), &pb.Point{409146138, -746188906})
feature, err := client.GetFeature(ctx, &pb.Point{409146138, -746188906})
if err != nil {
...
}
Expand All @@ -331,7 +331,7 @@ Here's where we call the server-side streaming method `ListFeatures`, which retu

```go
rect := &pb.Rectangle{ ... } // initialize a pb.Rectangle
stream, err := client.ListFeatures(context.Background(), rect)
stream, err := client.ListFeatures(ctx, rect)
if err != nil {
...
}
Expand Down Expand Up @@ -364,7 +364,7 @@ for i := 0; i < pointCount; i++ {
points = append(points, randomPoint(r))
}
log.Printf("Traversing %d points.", len(points))
stream, err := client.RecordRoute(context.Background())
stream, err := client.RecordRoute(ctx)
if err != nil {
log.Fatalf("%v.RecordRoute(_) = _, %v", client, err)
}
Expand All @@ -387,7 +387,7 @@ The `RouteGuide_RecordRouteClient` has a `Send()` method that we can use to send
Finally, let's look at our bidirectional streaming RPC `RouteChat()`. As in the case of `RecordRoute`, we only pass the method a context object and get back a stream that we can use to both write and read messages. However, this time we return values via our method's stream while the server is still writing messages to *their* message stream.

```go
stream, err := client.RouteChat(context.Background())
stream, err := client.RouteChat(ctx)
waitc := make(chan struct{})
go func() {
for {
Expand Down
5 changes: 4 additions & 1 deletion examples/helloworld/greeter_client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package main
import (
"log"
"os"
"time"

"golang.org/x/net/context"
"google.golang.org/grpc"
Expand All @@ -46,7 +47,9 @@ func main() {
if len(os.Args) > 1 {
name = os.Args[1]
}
r, err := c.SayHello(context.Background(), &pb.HelloRequest{Name: name})
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
r, err := c.SayHello(ctx, &pb.HelloRequest{Name: name})
if err != nil {
log.Fatalf("could not greet: %v", err)
}
Expand Down
5 changes: 4 additions & 1 deletion examples/helloworld/mock_helloworld/hw_mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package mock_helloworld_test
import (
"fmt"
"testing"
"time"

"github.com/golang/mock/gomock"
"github.com/golang/protobuf/proto"
Expand Down Expand Up @@ -59,7 +60,9 @@ func TestSayHello(t *testing.T) {
}

func testSayHello(t *testing.T, client helloworld.GreeterClient) {
r, err := client.SayHello(context.Background(), &helloworld.HelloRequest{Name: "unit_test"})
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
r, err := client.SayHello(ctx, &helloworld.HelloRequest{Name: "unit_test"})
if err != nil || r.Message != "Mocked Interface" {
t.Errorf("mocking failed")
}
Expand Down
16 changes: 12 additions & 4 deletions examples/route_guide/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ var (
// printFeature gets the feature for the given point.
func printFeature(client pb.RouteGuideClient, point *pb.Point) {
log.Printf("Getting feature for point (%d, %d)", point.Latitude, point.Longitude)
feature, err := client.GetFeature(context.Background(), point)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
feature, err := client.GetFeature(ctx, point)
if err != nil {
log.Fatalf("%v.GetFeatures(_) = _, %v: ", client, err)
}
Expand All @@ -56,7 +58,9 @@ func printFeature(client pb.RouteGuideClient, point *pb.Point) {
// printFeatures lists all the features within the given bounding Rectangle.
func printFeatures(client pb.RouteGuideClient, rect *pb.Rectangle) {
log.Printf("Looking for features within %v", rect)
stream, err := client.ListFeatures(context.Background(), rect)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
stream, err := client.ListFeatures(ctx, rect)
if err != nil {
log.Fatalf("%v.ListFeatures(_) = _, %v", client, err)
}
Expand All @@ -82,7 +86,9 @@ func runRecordRoute(client pb.RouteGuideClient) {
points = append(points, randomPoint(r))
}
log.Printf("Traversing %d points.", len(points))
stream, err := client.RecordRoute(context.Background())
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
stream, err := client.RecordRoute(ctx)
if err != nil {
log.Fatalf("%v.RecordRoute(_) = _, %v", client, err)
}
Expand All @@ -108,7 +114,9 @@ func runRouteChat(client pb.RouteGuideClient) {
{Location: &pb.Point{Latitude: 0, Longitude: 2}, Message: "Fifth message"},
{Location: &pb.Point{Latitude: 0, Longitude: 3}, Message: "Sixth message"},
}
stream, err := client.RouteChat(context.Background())
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
stream, err := client.RouteChat(ctx)
if err != nil {
log.Fatalf("%v.RouteChat(_) = _, %v", client, err)
}
Expand Down
5 changes: 4 additions & 1 deletion examples/route_guide/mock_routeguide/rg_mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package mock_routeguide_test
import (
"fmt"
"testing"
"time"

"github.com/golang/mock/gomock"
"github.com/golang/protobuf/proto"
Expand Down Expand Up @@ -59,7 +60,9 @@ func TestRouteChat(t *testing.T) {
}

func testRouteChat(client rgpb.RouteGuideClient) error {
stream, err := client.RouteChat(context.Background())
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
stream, err := client.RouteChat(ctx)
if err != nil {
return err
}
Expand Down

0 comments on commit f0a1202

Please sign in to comment.