examples: Replace context.Background with context.WithTimeout (#1877)

This commit is contained in:
Menghan Li
2018-02-28 15:01:36 -08:00
committed by dfawley
parent a1de3b2bd6
commit f0a1202acd
5 changed files with 28 additions and 11 deletions

View File

@ -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 {
...
}
@ -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 {
...
}
@ -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)
}
@ -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 {

View File

@ -21,6 +21,7 @@ package main
import (
"log"
"os"
"time"
"golang.org/x/net/context"
"google.golang.org/grpc"
@ -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)
}

View File

@ -21,6 +21,7 @@ package mock_helloworld_test
import (
"fmt"
"testing"
"time"
"github.com/golang/mock/gomock"
"github.com/golang/protobuf/proto"
@ -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")
}

View File

@ -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)
}
@ -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)
}
@ -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)
}
@ -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)
}

View File

@ -21,6 +21,7 @@ package mock_routeguide_test
import (
"fmt"
"testing"
"time"
"github.com/golang/mock/gomock"
"github.com/golang/protobuf/proto"
@ -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
}