@@ -3,6 +3,7 @@ package rpc
33import (
44 "context"
55 "net"
6+ "sync"
67 "testing"
78
89 "github.com/stretchr/testify/require"
@@ -48,13 +49,22 @@ func TestConnection_BaseContext(t *testing.T) {
4849 })
4950
5051 t .Run ("external context" , func (t * testing.T ) {
51- _ , server := net .Pipe ()
52- doneCh := make (chan struct {}, 1 )
52+ client , server := net .Pipe ()
5353
5454 ctx , cancel := context .WithCancel (context .Background ())
5555 defer cancel ()
5656
57+ clientConnectedCh := make (chan struct {}, 1 )
58+ contextCancelledCh := make (chan struct {}, 1 )
59+ wg := & sync.WaitGroup {}
60+ wg .Add (2 )
61+
5762 go func () {
63+ defer wg .Done ()
64+
65+ ctx , cancel := context .WithCancel (ctx )
66+ defer cancel ()
67+
5868 bootstrapClient := testcapnp .StreamTest_ServerToClient (slowStreamTestServer {})
5969 conn := NewConn (NewStreamTransport (server ), & Options {
6070 BootstrapClient : capnp .Client (bootstrapClient ),
@@ -64,11 +74,44 @@ func TestConnection_BaseContext(t *testing.T) {
6474 })
6575 defer conn .Close ()
6676
77+ select {
78+ case <- clientConnectedCh :
79+ cancel ()
80+ contextCancelledCh <- struct {}{}
81+ case <- conn .Done ():
82+ t .Failed ()
83+ }
84+
85+ // Connection should close when external context is cancelled
6786 <- conn .Done ()
68- close (doneCh )
6987 }()
7088
71- cancel ()
72- <- doneCh
89+ go func () {
90+ defer wg .Done ()
91+
92+ conn := NewConn (NewStreamTransport (client ), nil )
93+ defer conn .Close ()
94+
95+ ctx , cancel := context .WithCancel (context .Background ())
96+ defer cancel ()
97+
98+ client := testcapnp .StreamTest (conn .Bootstrap (ctx ))
99+ defer client .Release ()
100+
101+ if err := client .Resolve (ctx ); err != nil {
102+ require .NoError (t , err )
103+ }
104+
105+ clientConnectedCh <- struct {}{}
106+ <- contextCancelledCh
107+
108+ err := client .Push (ctx , func (st testcapnp.StreamTest_push_Params ) error {
109+ return st .SetData (make ([]byte , 1 ))
110+ })
111+
112+ require .NoError (t , err )
113+ }()
114+
115+ wg .Wait ()
73116 })
74117}
0 commit comments