Skip to content

Commit

Permalink
fix: always return non-nil context
Browse files Browse the repository at this point in the history
  • Loading branch information
powerman committed Apr 7, 2020
1 parent d125779 commit 6776687
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
6 changes: 5 additions & 1 deletion jsonrpc2/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
7 changes: 7 additions & 0 deletions jsonrpc2/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 6776687

Please sign in to comment.