Skip to content

Commit 6f07c8e

Browse files
authored
In timeout error handling, consider the case where the context is canceled (#300)
* in timeout error handling, consider the case where the context is canceled. * fix test
1 parent 5a0b4a8 commit 6f07c8e

File tree

8 files changed

+342
-67
lines changed

8 files changed

+342
-67
lines changed

_examples/04_timeout/federation/federation.pb.go

Lines changed: 34 additions & 31 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

_examples/04_timeout/federation/federation_grpc_federation.pb.go

Lines changed: 81 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

_examples/04_timeout/main_test.go

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ import (
2929
const bufSize = 1024
3030

3131
var (
32-
listener *bufconn.Listener
33-
postClient post.PostServiceClient
34-
calledUpdatePost bool
35-
blockCh = make(chan struct{})
32+
listener *bufconn.Listener
33+
postClient post.PostServiceClient
34+
updateDone = make(chan struct{})
35+
blockCh = make(chan struct{})
3636
)
3737

3838
type clientConfig struct{}
@@ -58,10 +58,16 @@ func (s *PostServer) GetPost(ctx context.Context, req *post.GetPostRequest) (*po
5858
}
5959

6060
func (s *PostServer) UpdatePost(ctx context.Context, req *post.UpdatePostRequest) (*post.UpdatePostResponse, error) {
61-
calledUpdatePost = true
61+
time.Sleep(2 * time.Second)
62+
updateDone <- struct{}{}
6263
return nil, nil
6364
}
6465

66+
func (s *PostServer) DeletePost(ctx context.Context, req *post.DeletePostRequest) (*post.DeletePostResponse, error) {
67+
time.Sleep(1 * time.Second)
68+
return nil, status.New(codes.Internal, "failed to delete").Err()
69+
}
70+
6571
func dialer(ctx context.Context, address string) (net.Conn, error) {
6672
return listener.Dial()
6773
}
@@ -140,15 +146,21 @@ func TestFederation(t *testing.T) {
140146
}
141147
})
142148
t.Run("UpdatePost", func(t *testing.T) {
143-
if _, err := client.UpdatePost(ctx, &federation.UpdatePostRequest{
149+
_, err := client.UpdatePost(ctx, &federation.UpdatePostRequest{
144150
Id: "foo",
145-
}); err != nil {
146-
t.Fatal(err)
151+
})
152+
if err == nil {
153+
t.Fatal("expected error")
154+
}
155+
st, ok := status.FromError(err)
156+
if !ok {
157+
t.Fatal("failed to get gRPC status error")
147158
}
148-
if !calledUpdatePost {
149-
t.Fatal("failed to call UpdatePost method")
159+
if st.Code() != codes.Internal {
160+
t.Fatalf("failed to get status code: %s", st.Code())
150161
}
151162
})
152163
blockCh <- struct{}{}
164+
<-updateDone
153165
time.Sleep(100 * time.Millisecond)
154166
}

0 commit comments

Comments
 (0)