MCPcopy Index your code
hub / github.com/cloudwego/netpoll / TestParallelShortConnection

Function TestParallelShortConnection

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

Source from the content-addressed store, hash-verified

706}
707
708func TestParallelShortConnection(t *testing.T) {
709 ln := createTestTCPListener(t)
710 defer ln.Close()
711 address := ln.Addr().String()
712
713 var received int64
714 el, err := NewEventLoop(func(ctx context.Context, connection Connection) error {
715 data, err := connection.Reader().Next(connection.Reader().Len())
716 atomic.AddInt64(&received, int64(len(data)))
717 if err != nil {
718 return err
719 }
720 // t.Logf("conn[%s] received: %d, active: %v", connection.RemoteAddr(), len(data), connection.IsActive())
721 return nil
722 })
723 MustNil(t, err)
724 go func() {
725 el.Serve(ln)
726 }()
727 defer el.Shutdown(context.Background())
728
729 conns := 100
730 sizePerConn := 1024
731 totalSize := conns * sizePerConn
732 var wg sync.WaitGroup
733 for i := 0; i < conns; i++ {
734 wg.Add(1)
735 go func() {
736 defer wg.Done()
737 conn, err := DialConnection("tcp", address, time.Second)
738 MustNil(t, err)
739 n, err := conn.Writer().WriteBinary(make([]byte, sizePerConn))
740 MustNil(t, err)
741 MustTrue(t, n == sizePerConn)
742 err = conn.Writer().Flush()
743 MustNil(t, err)
744 err = conn.Close()
745 MustNil(t, err)
746 }()
747 }
748 wg.Wait()
749
750 t0 := time.Now()
751 for atomic.LoadInt64(&received) < int64(totalSize) {
752 time.Sleep(time.Millisecond)
753 if time.Since(t0) > 100*time.Millisecond { // max wait 100ms
754 break
755 }
756 }
757 Equal(t, atomic.LoadInt64(&received), int64(totalSize))
758}
759
760func TestConnectionServerClose(t *testing.T) {
761 ln := createTestTCPListener(t)

Callers

nothing calls this directly

Calls 15

ServeMethod · 0.95
ShutdownMethod · 0.95
createTestTCPListenerFunction · 0.85
DialConnectionFunction · 0.85
AddrMethod · 0.80
AddMethod · 0.80
NewEventLoopFunction · 0.70
CloseMethod · 0.65
NextMethod · 0.65
ReaderMethod · 0.65
LenMethod · 0.65
WriteBinaryMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…