MCPcopy
hub / github.com/valyala/fasthttp / TestServerConnState

Function TestServerConnState

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

Source from the content-addressed store, hash-verified

247}
248
249func TestServerConnState(t *testing.T) {
250 t.Parallel()
251
252 states := make([]string, 0)
253 s := &Server{
254 Handler: func(ctx *RequestCtx) {},
255 ConnState: func(_ net.Conn, state ConnState) {
256 states = append(states, state.String())
257 },
258 }
259
260 ln := fasthttputil.NewInmemoryListener()
261
262 serverCh := make(chan struct{})
263 go func() {
264 if err := s.Serve(ln); err != nil {
265 t.Errorf("unexpected error: %v", err)
266 }
267 close(serverCh)
268 }()
269
270 clientCh := make(chan struct{})
271 go func() {
272 c, err := ln.Dial()
273 if err != nil {
274 t.Errorf("unexpected error: %v", err)
275 }
276 br := bufio.NewReader(c)
277 // Send 2 requests on the same connection.
278 for range 2 {
279 if _, err = c.Write([]byte("GET / HTTP/1.1\r\nHost: aa\r\n\r\n")); err != nil {
280 t.Errorf("unexpected error: %v", err)
281 }
282 var resp Response
283 if err := resp.Read(br); err != nil {
284 t.Errorf("unexpected error: %v", err)
285 }
286 if resp.StatusCode() != StatusOK {
287 t.Errorf("unexpected status code: %d. Expecting %d", resp.StatusCode(), StatusOK)
288 }
289 }
290 if err := c.Close(); err != nil {
291 t.Errorf("unexpected error: %v", err)
292 }
293 // Give the server a little bit of time to transition the connection to the close state.
294 time.Sleep(time.Millisecond * 100)
295 close(clientCh)
296 }()
297
298 select {
299 case <-clientCh:
300 case <-time.After(time.Second):
301 t.Fatal("timeout")
302 }
303
304 if err := ln.Close(); err != nil {
305 t.Fatalf("unexpected error: %v", err)
306 }

Callers

nothing calls this directly

Calls 10

ServeMethod · 0.95
DialMethod · 0.95
ReadMethod · 0.95
StatusCodeMethod · 0.95
CloseMethod · 0.95
NewInmemoryListenerFunction · 0.92
NewReaderMethod · 0.80
WriteMethod · 0.65
CloseMethod · 0.65
StringMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…