-
-
Notifications
You must be signed in to change notification settings - Fork 112
/
Copy pathshutdown_test.go
60 lines (49 loc) · 1.6 KB
/
shutdown_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package rpc_test
import (
"context"
"net"
"testing"
"capnproto.org/go/capnp/v3"
"capnproto.org/go/capnp/v3/rpc"
"capnproto.org/go/capnp/v3/rpc/internal/testcapnp"
"capnproto.org/go/capnp/v3/rpc/transport"
)
// TestRejectOnDisconnect verifies that, when a connection is dropped, outstanding calls
// fail with a "disconnected" exception.
func TestRejectOnDisconnect(t *testing.T) {
t.Parallel()
ctx := context.Background()
serverNetConn, clientNetConn := net.Pipe()
// The remote server will close this to acknowledge that the call has reached the other
// side; this makes sure we are testing the right logic, not the scenario where the
// connection is dropped before the message is sent, which could fail with an IO error,
// as opposed to being rejected on shutdown.
readyCh := make(chan struct{})
serverRpcConn := rpc.NewConn(transport.NewStream(serverNetConn), &rpc.Options{
BootstrapClient: capnp.Client(testcapnp.PingPong_ServerToClient(dropPingServer{
readyCh: readyCh,
})),
})
clientRpcConn := rpc.NewConn(transport.NewStream(clientNetConn), nil)
client := testcapnp.PingPong(clientRpcConn.Bootstrap(ctx))
defer client.Release()
future, release := client.EchoNum(ctx, nil)
defer release()
<-readyCh
serverNetConn.Close()
_, err := future.Struct()
if !capnp.IsDisconnected(err) {
t.Fatalf("Wanted disconnected error but got %v", err)
}
<-serverRpcConn.Done()
<-clientRpcConn.Done()
}
type dropPingServer struct {
readyCh chan<- struct{}
}
func (s dropPingServer) EchoNum(ctx context.Context, p testcapnp.PingPong_echoNum) error {
p.Go()
close(s.readyCh)
<-ctx.Done()
return nil
}