MCPcopy
hub / github.com/labstack/echo / TestStartConfig_GracefulShutdown

Function TestStartConfig_GracefulShutdown

server_test.go:132–226  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

130}
131
132func TestStartConfig_GracefulShutdown(t *testing.T) {
133 var testCases = []struct {
134 name string
135 expectBody string
136 expectGracefulError string
137 whenHandlerTakesLonger bool
138 }{
139 {
140 name: "ok, all handlers returns before graceful shutdown deadline",
141 whenHandlerTakesLonger: false,
142 expectBody: "OK",
143 expectGracefulError: "",
144 },
145 {
146 name: "nok, handlers do not returns before graceful shutdown deadline",
147 whenHandlerTakesLonger: true,
148 expectBody: "timeout",
149 expectGracefulError: stdContext.DeadlineExceeded.Error(),
150 },
151 }
152
153 for _, tc := range testCases {
154 t.Run(tc.name, func(t *testing.T) {
155 e := New()
156
157 e.GET("/ok", func(c *Context) error {
158 msg := "OK"
159 if tc.whenHandlerTakesLonger {
160 time.Sleep(150 * time.Millisecond)
161 msg = "timeout"
162 }
163 return c.String(http.StatusOK, msg)
164 })
165
166 addrChan := make(chan string)
167 errCh := make(chan error)
168
169 ctx, shutdown := stdContext.WithTimeout(stdContext.Background(), 50*time.Millisecond)
170 defer shutdown()
171
172 shutdownErrChan := make(chan error, 1)
173 go func() {
174 errCh <- (&StartConfig{
175 Address: ":0",
176 GracefulTimeout: 50 * time.Millisecond,
177 OnShutdownError: func(err error) {
178 shutdownErrChan <- err
179 },
180 ListenerAddrFunc: func(addr net.Addr) {
181 addrChan <- addr.String()
182 },
183 }).Start(ctx, e)
184 }()
185
186 addr, err := waitForServerStart(addrChan, errCh)
187 assert.NoError(t, err)
188
189 code, body, err := doGet(fmt.Sprintf("http://%v/ok", addr))

Callers

nothing calls this directly

Calls 7

NewFunction · 0.85
waitForServerStartFunction · 0.85
doGetFunction · 0.85
ErrorMethod · 0.45
GETMethod · 0.45
StringMethod · 0.45
StartMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…