MCPcopy
hub / github.com/ory/keto / TestPanicRecovery

Function TestPanicRecovery

internal/driver/daemon_test.go:84–139  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

82}
83
84func TestPanicRecovery(t *testing.T) {
85 t.Parallel()
86
87 ctx, cancel := context.WithCancel(context.Background())
88 defer cancel()
89
90 unaryPanicInterceptor := func(context.Context, interface{}, *grpc.UnaryServerInfo, grpc.UnaryHandler) (interface{}, error) {
91 panic("test panic")
92 }
93 streamPanicInterceptor := func(context.Context, interface{}, *grpc.UnaryServerInfo, grpc.UnaryHandler) (interface{}, error) {
94 panic("test panic")
95 }
96
97 r := NewSqliteTestRegistry(t, false, WithGRPCUnaryInterceptors(unaryPanicInterceptor), WithGRPCUnaryInterceptors(streamPanicInterceptor))
98 getAddr := UseDynamicPorts(ctx, t, r)
99
100 eg := errgroup.Group{}
101 doneShutdown := make(chan struct{})
102 eg.Go(r.serveWrite(ctx, doneShutdown))
103
104 _, port, _ := getAddr(t, "write")
105
106 conn, err := grpc.NewClient(fmt.Sprintf("127.0.0.1:%s", port), grpc.WithTransportCredentials(insecure.NewCredentials()))
107 require.NoError(t, err)
108 defer func() { require.NoError(t, conn.Close()) }()
109
110 assert.EventuallyWithT(t, func(t *assert.CollectT) {
111 cl := grpcHealthV1.NewHealthClient(conn)
112
113 watcher, err := cl.Watch(ctx, &grpcHealthV1.HealthCheckRequest{})
114 require.NoError(t, err)
115 require.NoError(t, watcher.CloseSend())
116 for err := status.Error(codes.Unavailable, "init"); status.Code(err) != codes.Unavailable; _, err = watcher.Recv() {
117 }
118 }, 2*time.Second, 10*time.Millisecond)
119
120 cl := grpcHealthV1.NewHealthClient(conn)
121 // we want to ensure the server is still running after the panic
122 for range 10 {
123 // Unary call
124 resp, err := cl.Check(ctx, &grpcHealthV1.HealthCheckRequest{})
125 require.Error(t, err, "%+v", resp)
126 assert.Equal(t, codes.Internal, status.Code(err))
127
128 // Streaming call
129 wResp, err := cl.Watch(ctx, &grpcHealthV1.HealthCheckRequest{})
130 require.NoError(t, err)
131 err = wResp.RecvMsg(nil)
132 require.Error(t, err)
133 assert.Equal(t, codes.Internal, status.Code(err))
134 }
135
136 cancel()
137 <-doneShutdown
138 require.NoError(t, eg.Wait())
139}
140
141func getLabelValue(name string, metric []*ioprometheusclient.Metric) string {

Callers

nothing calls this directly

Calls 8

NewSqliteTestRegistryFunction · 0.85
UseDynamicPortsFunction · 0.85
serveWriteMethod · 0.80
WatchMethod · 0.80
CloseMethod · 0.65
CheckMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected