github.com/cloudwego/kitex@v0.9.0/pkg/remote/trans/nphttp2/conn_pool_test.go (about)

     1  /*
     2   * Copyright 2022 CloudWeGo Authors
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package nphttp2
    18  
    19  import (
    20  	"testing"
    21  	"time"
    22  
    23  	"github.com/cloudwego/kitex/internal/test"
    24  )
    25  
    26  func TestConnPool(t *testing.T) {
    27  	// mock init
    28  	connPool := newMockConnPool()
    29  	defer connPool.Close()
    30  	opt := newMockConnOption()
    31  	ctx := newMockCtxWithRPCInfo()
    32  
    33  	// test Get()
    34  	_, err := connPool.Get(ctx, "tcp", mockAddr0, opt)
    35  	test.Assert(t, err == nil, err)
    36  
    37  	// test connection reuse
    38  	// keep create new connection until filling the pool size,
    39  	// then Get() will reuse established connection by round-robin
    40  	for i := 0; uint32(i) < connPool.size*2; i++ {
    41  		_, err := connPool.Get(ctx, "tcp", mockAddr1, opt)
    42  		test.Assert(t, err == nil, err)
    43  	}
    44  
    45  	// test TimeoutGet()
    46  	opt.ConnectTimeout = time.Microsecond
    47  	_, err = connPool.Get(ctx, "tcp", mockAddr0, opt)
    48  	test.Assert(t, err != nil, err)
    49  
    50  	// test Clean()
    51  	connPool.Clean("tcp", mockAddr0)
    52  	_, ok := connPool.conns.Load(mockAddr0)
    53  	test.Assert(t, !ok)
    54  }