github.com/msales/pkg/v3@v3.24.0/grpcx/middleware/recovery_test.go (about) 1 package middleware_test 2 3 import ( 4 "context" 5 "errors" 6 "testing" 7 8 "github.com/msales/pkg/v3/grpcx/middleware" 9 "github.com/msales/pkg/v3/log" 10 "github.com/msales/pkg/v3/mocks" 11 "github.com/stretchr/testify/assert" 12 "github.com/stretchr/testify/mock" 13 "google.golang.org/grpc" 14 ) 15 16 func TestWithUnaryServerRecovery(t *testing.T) { 17 defer func() { 18 if err := recover(); err != nil { 19 t.Fatal("Expected the panic to be handled.") 20 } 21 }() 22 23 ctx := context.Background() 24 logger := new(mocks.Logger) 25 logger.On("Error", "test", "stack", mock.AnythingOfType("string")) 26 27 interceptor := middleware.WithUnaryServerRecovery() 28 29 ret, err := interceptor(log.WithLogger(ctx, logger), nil, nil, func(ctx context.Context, req interface{}) (interface{}, error) { 30 panic("test") 31 }) 32 33 assert.Nil(t, ret) 34 assert.NoError(t, err) 35 logger.AssertExpectations(t) 36 } 37 38 func TestWithUnaryServerRecovery_WithoutStack(t *testing.T) { 39 defer func() { 40 if err := recover(); err != nil { 41 t.Fatal("Expected the panic to be handled.") 42 } 43 }() 44 45 ctx := context.Background() 46 logger := new(mocks.Logger) 47 logger.On("Error", "test") 48 49 interceptor := middleware.WithUnaryServerRecovery(middleware.WithoutStack()) 50 51 ret, err := interceptor(log.WithLogger(ctx, logger), nil, nil, func(ctx context.Context, req interface{}) (interface{}, error) { 52 panic("test") 53 }) 54 55 assert.Nil(t, ret) 56 assert.NoError(t, err) 57 logger.AssertExpectations(t) 58 } 59 60 func TestWithUnaryServerRecovery_WithError(t *testing.T) { 61 defer func() { 62 if err := recover(); err != nil { 63 t.Fatal("Expected the panic to be handled.") 64 } 65 }() 66 67 interceptor := middleware.WithUnaryServerRecovery() 68 69 ret, err := interceptor(context.Background(), nil, nil, func(ctx context.Context, req interface{}) (interface{}, error) { 70 panic(errors.New("test: error")) 71 }) 72 73 assert.Nil(t, ret) 74 assert.NoError(t, err) 75 } 76 77 func TestWithStreamServerRecovery(t *testing.T) { 78 defer func() { 79 if err := recover(); err != nil { 80 t.Fatal("Expected the panic to be handled.") 81 } 82 }() 83 84 ctx := context.Background() 85 logger := new(mocks.Logger) 86 logger.On("Error", "test", "stack", mock.AnythingOfType("string")) 87 88 interceptor := middleware.WithStreamServerRecovery() 89 stream := &serverStreamMock{ctx: log.WithLogger(ctx, logger)} 90 91 err := interceptor(nil, stream, nil, func(srv interface{}, stream grpc.ServerStream) error { 92 panic("test") 93 }) 94 95 assert.NoError(t, err) 96 logger.AssertExpectations(t) 97 } 98 99 func TestWithStreamServerRecovery_WithoutStack(t *testing.T) { 100 defer func() { 101 if err := recover(); err != nil { 102 t.Fatal("Expected the panic to be handled.") 103 } 104 }() 105 106 ctx := context.Background() 107 logger := new(mocks.Logger) 108 logger.On("Error", "test") 109 110 interceptor := middleware.WithStreamServerRecovery(middleware.WithoutStack()) 111 stream := &serverStreamMock{ctx: log.WithLogger(ctx, logger)} 112 113 err := interceptor(nil, stream, nil, func(srv interface{}, stream grpc.ServerStream) error { 114 panic("test") 115 }) 116 117 assert.NoError(t, err) 118 logger.AssertExpectations(t) 119 } 120 121 func TestWithStreamServerRecovery_WithError(t *testing.T) { 122 defer func() { 123 if err := recover(); err != nil { 124 t.Fatal("Expected the panic to be handled.") 125 } 126 }() 127 128 interceptor := middleware.WithStreamServerRecovery() 129 stream := &serverStreamMock{ctx: context.Background()} 130 131 err := interceptor(nil, stream, nil, func(srv interface{}, stream grpc.ServerStream) error { 132 panic(errors.New("test: error")) 133 }) 134 135 assert.NoError(t, err) 136 } 137 138 func TestWithUnaryClientRecovery(t *testing.T) { 139 defer func() { 140 if err := recover(); err != nil { 141 t.Fatal("Expected the panic to be handled.") 142 } 143 }() 144 145 ctx := context.Background() 146 logger := new(mocks.Logger) 147 logger.On("Error", "test", "stack", mock.AnythingOfType("string")) 148 149 interceptor := middleware.WithUnaryClientRecovery() 150 151 err := interceptor(log.WithLogger(ctx, logger), "method", nil, nil, nil, func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, opts ...grpc.CallOption) error { 152 panic("test") 153 }) 154 155 assert.NoError(t, err) 156 logger.AssertExpectations(t) 157 } 158 159 func TestWithUnaryClientRecovery_WithoutStack(t *testing.T) { 160 defer func() { 161 if err := recover(); err != nil { 162 t.Fatal("Expected the panic to be handled.") 163 } 164 }() 165 166 ctx := context.Background() 167 logger := new(mocks.Logger) 168 logger.On("Error", "test") 169 170 interceptor := middleware.WithUnaryClientRecovery(middleware.WithoutStack()) 171 172 err := interceptor(log.WithLogger(ctx, logger), "method", nil, nil, nil, func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, opts ...grpc.CallOption) error { 173 panic("test") 174 }) 175 176 assert.NoError(t, err) 177 logger.AssertExpectations(t) 178 } 179 180 func TestWithUnaryClientRecovery_WithError(t *testing.T) { 181 defer func() { 182 if err := recover(); err != nil { 183 t.Fatal("Expected the panic to be handled.") 184 } 185 }() 186 187 interceptor := middleware.WithUnaryClientRecovery() 188 err := interceptor(context.Background(), "method", nil, nil, nil, func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, opts ...grpc.CallOption) error { 189 panic(errors.New("test: error")) 190 }) 191 192 assert.NoError(t, err) 193 }