@@ -15,6 +15,24 @@ import (
1515)
1616
1717func TestConn (t * testing.T ) {
18+
19+ t .Run ("closes when context is done" , func (t * testing.T ) {
20+ ctx , cancel := context .WithCancel (context .Background ())
21+
22+ connA , connB := Pipe (ctx , noopHandler {}, noopHandler {})
23+ defer connA .Close ()
24+ defer connB .Close ()
25+
26+ cancel ()
27+ <- connA .DisconnectNotify ()
28+
29+ got := connA .Close ()
30+ want := jsonrpc2 .ErrClosed
31+ if got != want {
32+ t .Fatalf ("got %v, want %v" , got , want )
33+ }
34+ })
35+
1836 t .Run ("cancels context when closed" , func (t * testing.T ) {
1937 ctxCanceled := make (chan struct {})
2038
@@ -24,7 +42,7 @@ func TestConn(t *testing.T) {
2442 close (ctxCanceled )
2543 })
2644
27- connA , connB := Pipe (noopHandler {}, jsonrpc2 .AsyncHandler (handler ))
45+ connA , connB := Pipe (context . Background (), noopHandler {}, jsonrpc2 .AsyncHandler (handler ))
2846 defer connA .Close ()
2947 defer connB .Close ()
3048
@@ -232,7 +250,7 @@ func testParams(t *testing.T, want *json.RawMessage, fn func(c *jsonrpc2.Conn) e
232250 wg .Done ()
233251 })
234252
235- connA , connB := Pipe (noopHandler {}, handler )
253+ connA , connB := Pipe (context . Background (), noopHandler {}, handler )
236254 defer connA .Close ()
237255 defer connB .Close ()
238256
@@ -278,8 +296,7 @@ func assertRawJSONMessage(t *testing.T, got *json.RawMessage, want *json.RawMess
278296
279297// Pipe returns two jsonrpc2.Conn, connected via a synchronous, in-memory, full
280298// duplex network connection.
281- func Pipe (handlerA , handlerB jsonrpc2.Handler ) (connA * jsonrpc2.Conn , connB * jsonrpc2.Conn ) {
282- ctx := context .Background ()
299+ func Pipe (ctx context.Context , handlerA , handlerB jsonrpc2.Handler ) (connA * jsonrpc2.Conn , connB * jsonrpc2.Conn ) {
283300 a , b := net .Pipe ()
284301 connA = jsonrpc2 .NewConn (ctx , jsonrpc2 .NewPlainObjectStream (a ), handlerA )
285302 connB = jsonrpc2 .NewConn (ctx , jsonrpc2 .NewPlainObjectStream (b ), handlerB )
0 commit comments