diff --git a/jsonrpc2/context.go b/jsonrpc2/context.go index 9d844d0..1ec8286 100644 --- a/jsonrpc2/context.go +++ b/jsonrpc2/context.go @@ -19,8 +19,12 @@ type Ctx struct { ctx context.Context } -// Context returns ctx given to preceding SetContext call. +// Context returns ctx given to preceding SetContext call or +// context.Background() otherwise. func (c *Ctx) Context() context.Context { + if c.ctx == nil { + return context.Background() + } return c.ctx } diff --git a/jsonrpc2/context_test.go b/jsonrpc2/context_test.go index 9d77de8..b17d07f 100644 --- a/jsonrpc2/context_test.go +++ b/jsonrpc2/context_test.go @@ -62,6 +62,13 @@ func init() { _ = rpc.Register(&CtxSvc{}) } +func TestNoContext(t *testing.T) { + var c jsonrpc2.Ctx + if c.Context() != context.Background() { + t.Fatal("empty context") + } +} + // - for each of these servers: // * TCP server without context // * TCP server with context