Skip to content

Commit

Permalink
Fix TestConn_Close
Browse files Browse the repository at this point in the history
The changes in this commit arrange TestConn_Close to execute as
intended by closing the connection after sending the request.
  • Loading branch information
samherrmann committed Feb 7, 2023
1 parent 0c9de81 commit 85075f0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
1 change: 0 additions & 1 deletion jsonrpc2.go
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,6 @@ func (c *Conn) close(cause error) error {
}

for _, call := range c.pending {
call.done <- cause
close(call.done)
}

Expand Down
28 changes: 22 additions & 6 deletions jsonrpc2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,19 +359,35 @@ func TestConn_DisconnectNotify(t *testing.T) {

func TestConn_Close(t *testing.T) {
t.Run("waiting for response", func(t *testing.T) {
_, connB := net.Pipe()
c := jsonrpc2.NewConn(context.Background(), jsonrpc2.NewPlainObjectStream(connB), nil)
connA, connB := net.Pipe()
nodeA := jsonrpc2.NewConn(
context.Background(),
jsonrpc2.NewPlainObjectStream(connA), noopHandler{},
)
defer nodeA.Close()
nodeB := jsonrpc2.NewConn(
context.Background(),
jsonrpc2.NewPlainObjectStream(connB),
noopHandler{},
)
defer nodeB.Close()

ready := make(chan struct{})
done := make(chan struct{})
go func() {
if err := c.Call(context.Background(), "m", nil, nil); err != jsonrpc2.ErrClosed {
close(ready)
err := nodeB.Call(context.Background(), "m", nil, nil)
if err != jsonrpc2.ErrClosed {
t.Errorf("got error %v, want %v", err, jsonrpc2.ErrClosed)
}
close(done)
}()
if err := c.Close(); err != nil && err != jsonrpc2.ErrClosed {
// Wait for the request to be sent before we close the connection.
<-ready
if err := nodeB.Close(); err != nil && err != jsonrpc2.ErrClosed {
t.Error(err)
}
assertDisconnect(t, c, connB)
assertDisconnect(t, nodeB, connB)
<-done
})
}
Expand All @@ -386,7 +402,7 @@ func serve(ctx context.Context, lis net.Listener, h jsonrpc2.Handler, streamMake
}
}

func assertDisconnect(t *testing.T, c *jsonrpc2.Conn, conn net.Conn) {
func assertDisconnect(t *testing.T, c *jsonrpc2.Conn, conn io.Writer) {
select {
case <-c.DisconnectNotify():
case <-time.After(200 * time.Millisecond):
Expand Down

0 comments on commit 85075f0

Please sign in to comment.