@@ -24,6 +24,7 @@ import (
2424 "strings"
2525 "sync"
2626 "testing"
27+ "time"
2728
2829 "github.com/gogo/protobuf/proto"
2930 "github.com/pkg/errors"
@@ -57,7 +58,8 @@ func (tc *testingClient) Test(ctx context.Context, req *testPayload) (*testPaylo
5758}
5859
5960type testPayload struct {
60- Foo string `protobuf:"bytes,1,opt,name=foo,proto3"`
61+ Foo string `protobuf:"bytes,1,opt,name=foo,proto3"`
62+ Deadline int64 `protobuf:"varint,2,opt,name=deadline,proto3"`
6163}
6264
6365func (r * testPayload ) Reset () { * r = testPayload {} }
@@ -68,7 +70,11 @@ func (r *testPayload) ProtoMessage() {}
6870type testingServer struct {}
6971
7072func (s * testingServer ) Test (ctx context.Context , req * testPayload ) (* testPayload , error ) {
71- return & testPayload {Foo : strings .Repeat (req .Foo , 2 )}, nil
73+ tp := & testPayload {Foo : strings .Repeat (req .Foo , 2 )}
74+ if dl , ok := ctx .Deadline (); ok {
75+ tp .Deadline = dl .UnixNano ()
76+ }
77+ return tp , nil
7278}
7379
7480// registerTestingService mocks more of what is generated code. Unlike grpc, we
@@ -376,6 +382,34 @@ func TestUnixSocketHandshake(t *testing.T) {
376382 }
377383}
378384
385+ func TestServerRequestTimeout (t * testing.T ) {
386+ var (
387+ ctx , cancel = context .WithDeadline (context .Background (), time .Now ().Add (10 * time .Minute ))
388+ server = mustServer (t )(NewServer ())
389+ addr , listener = newTestListener (t )
390+ testImpl = & testingServer {}
391+ client , cleanup = newTestClient (t , addr )
392+ result testPayload
393+ )
394+ defer cancel ()
395+ defer cleanup ()
396+ defer listener .Close ()
397+
398+ registerTestingService (server , testImpl )
399+
400+ go server .Serve (ctx , listener )
401+ defer server .Shutdown (ctx )
402+
403+ if err := client .Call (ctx , serviceName , "Test" , & testPayload {}, & result ); err != nil {
404+ t .Fatalf ("unexpected error making call: %v" , err )
405+ }
406+
407+ dl , _ := ctx .Deadline ()
408+ if result .Deadline != dl .UnixNano () {
409+ t .Fatalf ("expected deadline %v, actual: %v" , dl , result .Deadline )
410+ }
411+ }
412+
379413func BenchmarkRoundTrip (b * testing.B ) {
380414 var (
381415 ctx = context .Background ()
0 commit comments