cubefs

Форк
0
/
recovery_test.go 
102 строки · 2.4 Кб
1
// Copyright 2022 The CubeFS Authors.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//     http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12
// implied. See the License for the specific language governing
13
// permissions and limitations under the License.
14

15
package rpc
16

17
import (
18
	"errors"
19
	"net"
20
	"net/http"
21
	"os"
22
	"testing"
23

24
	"github.com/stretchr/testify/require"
25
)
26

27
func TestServerRecovery(t *testing.T) {
28
	{
29
		router := New()
30
		router.Router.PanicHandler = defaultRecovery
31
		router.Use(func(c *Context) {
32
			panic("in interceptor")
33
		})
34
		router.Handle(http.MethodGet, "/", func(c *Context) {
35
			// do nothing
36
		})
37

38
		w := new(mockResponseWriter)
39
		req, _ := http.NewRequest(http.MethodGet, "/", nil)
40
		require.Panics(t, func() {
41
			router.ServeHTTP(w, req)
42
		})
43
	}
44
	{
45
		router := New()
46
		router.Router.PanicHandler = defaultRecovery
47
		router.Use(func(c *Context) {
48
			// do nothing
49
		})
50
		router.Handle(http.MethodGet, "/", func(c *Context) {
51
			panic("in app")
52
		})
53

54
		w := new(mockResponseWriter)
55
		req, _ := http.NewRequest(http.MethodGet, "/", nil)
56
		require.Panics(t, func() {
57
			router.ServeHTTP(w, req)
58
		})
59
	}
60
	{
61
		router := New()
62
		router.Router.PanicHandler = defaultRecovery
63
		router.Use(func(c *Context) {
64
			// do nothing
65
		})
66
		router.Handle(http.MethodGet, "/", func(c *Context) {
67
			panic(&net.OpError{
68
				Err: &os.SyscallError{
69
					Syscall: "App",
70
					Err:     errors.New("broken pipe"),
71
				},
72
			})
73
		})
74

75
		w := new(mockResponseWriter)
76
		req, _ := http.NewRequest(http.MethodGet, "/", nil)
77
		require.NotPanics(t, func() {
78
			router.ServeHTTP(w, req)
79
		})
80
	}
81
	{
82
		router := New()
83
		router.Router.PanicHandler = defaultRecovery
84
		router.Use(func(c *Context) {
85
			panic(&net.OpError{
86
				Err: &os.SyscallError{
87
					Syscall: "Middleware",
88
					Err:     errors.New("connection reset by peer"),
89
				},
90
			})
91
		})
92
		router.Handle(http.MethodGet, "/", func(c *Context) {
93
			// do nothing
94
		})
95

96
		w := new(mockResponseWriter)
97
		req, _ := http.NewRequest(http.MethodGet, "/", nil)
98
		require.NotPanics(t, func() {
99
			router.ServeHTTP(w, req)
100
		})
101
	}
102
}
103

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.