MCPcopy Create free account
hub / github.com/cloudwego/netpoll / TestConnectionTimeout

Function TestConnectionTimeout

connection_test.go:343–519  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

341}
342
343func TestConnectionTimeout(t *testing.T) {
344 ln, err := net.Listen("tcp", "127.0.0.1:0")
345 MustNil(t, err)
346 defer ln.Close()
347
348 const (
349 bufsz = 1 << 20
350 interval = 10 * time.Millisecond
351 )
352
353 calcRate := func(n int32) int32 {
354 v := n / int32(time.Second/interval)
355 if v > bufsz {
356 panic(v)
357 }
358 if v < 1 {
359 return 1
360 }
361 return v
362 }
363
364 wn := int32(1) // for each Read, must <= bufsz
365 setServerWriteRate := func(n int32) {
366 atomic.StoreInt32(&wn, calcRate(n))
367 }
368
369 rn := int32(1) // for each Write, must <= bufsz
370 setServerReadRate := func(n int32) {
371 atomic.StoreInt32(&rn, calcRate(n))
372 }
373
374 go func() {
375 for {
376 conn, err := ln.Accept()
377 if err != nil {
378 return
379 }
380 // set small SO_SNDBUF/SO_RCVBUF buffer for better control timeout test
381 tcpconn := conn.(*net.TCPConn)
382 tcpconn.SetReadBuffer(512)
383 tcpconn.SetWriteBuffer(512)
384 go func() {
385 buf := make([]byte, bufsz)
386 for {
387 n := atomic.LoadInt32(&rn)
388 _, err := conn.Read(buf[:int(n)])
389 if err != nil {
390 conn.Close()
391 return
392 }
393 time.Sleep(interval)
394 }
395 }()
396
397 go func() {
398 buf := make([]byte, bufsz)
399 for {
400 n := atomic.LoadInt32(&wn)

Callers

nothing calls this directly

Calls 15

DialConnectionFunction · 0.85
AcceptMethod · 0.80
AddrMethod · 0.80
IsMethod · 0.80
AddMethod · 0.80
CloseMethod · 0.65
FdMethod · 0.65
MallocMethod · 0.65
WriterMethod · 0.65
FlushMethod · 0.65
SetWriteTimeoutMethod · 0.65
NextMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…