github.com/TeaOSLab/EdgeNode@v1.3.8/internal/nodes/http_client_pool_test.go (about)

     1  package nodes
     2  
     3  import (
     4  	"context"
     5  	"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
     6  	"github.com/TeaOSLab/EdgeNode/internal/utils/testutils"
     7  	"net/http"
     8  	"runtime"
     9  	"testing"
    10  	"time"
    11  )
    12  
    13  func TestHTTPClientPool_Client(t *testing.T) {
    14  	var pool = NewHTTPClientPool()
    15  
    16  	rawReq, err := http.NewRequest(http.MethodGet, "https://example.com/", nil)
    17  	if err != nil {
    18  		t.Fatal(err)
    19  	}
    20  
    21  	var req = &HTTPRequest{RawReq: rawReq}
    22  
    23  	{
    24  		var origin = &serverconfigs.OriginConfig{
    25  			Id:      1,
    26  			Version: 2,
    27  			Addr:    &serverconfigs.NetworkAddressConfig{Host: "127.0.0.1", PortRange: "1234"},
    28  		}
    29  		err := origin.Init(context.Background())
    30  		if err != nil {
    31  			t.Fatal(err)
    32  		}
    33  		{
    34  			client, err := pool.Client(req, origin, origin.Addr.PickAddress(), nil, false)
    35  			if err != nil {
    36  				t.Fatal(err)
    37  			}
    38  			t.Log("client:", client)
    39  		}
    40  		for i := 0; i < 10; i++ {
    41  			client, err := pool.Client(req, origin, origin.Addr.PickAddress(), nil, false)
    42  			if err != nil {
    43  				t.Fatal(err)
    44  			}
    45  			t.Log("client:", client)
    46  		}
    47  	}
    48  }
    49  
    50  func TestHTTPClientPool_cleanClients(t *testing.T) {
    51  	var origin = &serverconfigs.OriginConfig{
    52  		Id:      1,
    53  		Version: 2,
    54  		Addr:    &serverconfigs.NetworkAddressConfig{Host: "127.0.0.1", PortRange: "1234"},
    55  	}
    56  	err := origin.Init(context.Background())
    57  	if err != nil {
    58  		t.Fatal(err)
    59  	}
    60  
    61  	var pool = NewHTTPClientPool()
    62  
    63  	for i := 0; i < 10; i++ {
    64  		t.Log("get", i)
    65  		_, _ = pool.Client(nil, origin, origin.Addr.PickAddress(), nil, false)
    66  
    67  		if testutils.IsSingleTesting() {
    68  			time.Sleep(1 * time.Second)
    69  		}
    70  	}
    71  }
    72  
    73  func BenchmarkHTTPClientPool_Client(b *testing.B) {
    74  	runtime.GOMAXPROCS(1)
    75  
    76  	var origin = &serverconfigs.OriginConfig{
    77  		Id:      1,
    78  		Version: 2,
    79  		Addr:    &serverconfigs.NetworkAddressConfig{Host: "127.0.0.1", PortRange: "1234"},
    80  	}
    81  	err := origin.Init(context.Background())
    82  	if err != nil {
    83  		b.Fatal(err)
    84  	}
    85  
    86  	b.ResetTimer()
    87  
    88  	var pool = NewHTTPClientPool()
    89  	for i := 0; i < b.N; i++ {
    90  		_, _ = pool.Client(nil, origin, origin.Addr.PickAddress(), nil, false)
    91  	}
    92  }