github.com/cloudwego/hertz@v0.9.3/pkg/protocol/http1/client_unix_test.go (about)

     1  // Copyright 2023 CloudWeGo 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 implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  //
    15  //go:build aix || darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris
    16  // +build aix darwin dragonfly freebsd linux netbsd openbsd solaris
    17  
    18  package http1
    19  
    20  import (
    21  	"context"
    22  	"errors"
    23  	"net/http"
    24  	"runtime"
    25  	"sync"
    26  	"sync/atomic"
    27  	"testing"
    28  	"time"
    29  
    30  	errs "github.com/cloudwego/hertz/pkg/common/errors"
    31  	"github.com/cloudwego/hertz/pkg/common/test/assert"
    32  	"github.com/cloudwego/hertz/pkg/network/netpoll"
    33  	"github.com/cloudwego/hertz/pkg/protocol"
    34  	"github.com/cloudwego/hertz/pkg/protocol/consts"
    35  )
    36  
    37  func TestGcBodyStream(t *testing.T) {
    38  	srv := &http.Server{Addr: "127.0.0.1:11001", Handler: http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
    39  		for range [1024]int{} {
    40  			w.Write([]byte("hello world\n"))
    41  		}
    42  	})}
    43  	go srv.ListenAndServe()
    44  	time.Sleep(100 * time.Millisecond)
    45  
    46  	c := &HostClient{
    47  		ClientOptions: &ClientOptions{
    48  			Dialer:             netpoll.NewDialer(),
    49  			ResponseBodyStream: true,
    50  		},
    51  		Addr: "127.0.0.1:11001",
    52  	}
    53  
    54  	for i := 0; i < 10; i++ {
    55  		req, resp := protocol.AcquireRequest(), protocol.AcquireResponse()
    56  		req.SetRequestURI("http://127.0.0.1:11001")
    57  		req.SetMethod(consts.MethodPost)
    58  		err := c.Do(context.Background(), req, resp)
    59  		if err != nil {
    60  			t.Errorf("client Do error=%v", err.Error())
    61  		}
    62  	}
    63  
    64  	runtime.GC()
    65  	// wait for gc
    66  	time.Sleep(100 * time.Millisecond)
    67  	c.CloseIdleConnections()
    68  	assert.DeepEqual(t, 0, c.ConnPoolState().TotalConnNum)
    69  }
    70  
    71  func TestMaxConn(t *testing.T) {
    72  	srv := &http.Server{Addr: "127.0.0.1:11002", Handler: http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
    73  		w.Write([]byte("hello world\n"))
    74  	})}
    75  	go srv.ListenAndServe()
    76  	time.Sleep(100 * time.Millisecond)
    77  
    78  	c := &HostClient{
    79  		ClientOptions: &ClientOptions{
    80  			Dialer:             netpoll.NewDialer(),
    81  			ResponseBodyStream: true,
    82  			MaxConnWaitTimeout: time.Millisecond * 100,
    83  			MaxConns:           5,
    84  		},
    85  		Addr: "127.0.0.1:11002",
    86  	}
    87  
    88  	var successCount int32
    89  	var noFreeCount int32
    90  	wg := sync.WaitGroup{}
    91  	for i := 0; i < 10; i++ {
    92  		wg.Add(1)
    93  		go func() {
    94  			defer wg.Done()
    95  			req, resp := protocol.AcquireRequest(), protocol.AcquireResponse()
    96  			req.SetRequestURI("http://127.0.0.1:11002")
    97  			req.SetMethod(consts.MethodPost)
    98  			err := c.Do(context.Background(), req, resp)
    99  			if err != nil {
   100  				if errors.Is(err, errs.ErrNoFreeConns) {
   101  					atomic.AddInt32(&noFreeCount, 1)
   102  					return
   103  				}
   104  				t.Errorf("client Do error=%v", err.Error())
   105  			}
   106  			atomic.AddInt32(&successCount, 1)
   107  		}()
   108  	}
   109  	wg.Wait()
   110  
   111  	assert.True(t, atomic.LoadInt32(&successCount) == 5)
   112  	assert.True(t, atomic.LoadInt32(&noFreeCount) == 5)
   113  	assert.DeepEqual(t, 0, c.ConnectionCount())
   114  	assert.DeepEqual(t, 5, c.WantConnectionCount())
   115  
   116  	runtime.GC()
   117  	// wait for gc
   118  	time.Sleep(100 * time.Millisecond)
   119  	c.CloseIdleConnections()
   120  	assert.DeepEqual(t, 0, c.WantConnectionCount())
   121  }