1
0
Fork 0
mirror of https://github.com/sourcegraph/jsonrpc2.git synced 2026-07-04 08:13:40 +02:00

Fix TestConn_Close

The changes in this commit arrange TestConn_Close to execute as
intended by closing the connection after sending the request.
This commit is contained in:
Sam Herrmann 2023-02-07 13:52:21 -05:00
parent 0c9de81282
commit 85075f0782
2 changed files with 22 additions and 7 deletions

View file

@ -430,7 +430,6 @@ func (c *Conn) close(cause error) error {
} }
for _, call := range c.pending { for _, call := range c.pending {
call.done <- cause
close(call.done) close(call.done)
} }

View file

@ -359,19 +359,35 @@ func TestConn_DisconnectNotify(t *testing.T) {
func TestConn_Close(t *testing.T) { func TestConn_Close(t *testing.T) {
t.Run("waiting for response", func(t *testing.T) { t.Run("waiting for response", func(t *testing.T) {
_, connB := net.Pipe() connA, connB := net.Pipe()
c := jsonrpc2.NewConn(context.Background(), jsonrpc2.NewPlainObjectStream(connB), nil) 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{}) done := make(chan struct{})
go func() { 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) t.Errorf("got error %v, want %v", err, jsonrpc2.ErrClosed)
} }
close(done) 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) t.Error(err)
} }
assertDisconnect(t, c, connB) assertDisconnect(t, nodeB, connB)
<-done <-done
}) })
} }
@ -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 { select {
case <-c.DisconnectNotify(): case <-c.DisconnectNotify():
case <-time.After(200 * time.Millisecond): case <-time.After(200 * time.Millisecond):