1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
|
package grpctool
import (
"context"
"io"
"testing"
"time"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gitlab.com/gitlab-org/cluster-integration/gitlab-agent/v16/internal/tool/testing/mock_tool"
"gitlab.com/gitlab-org/cluster-integration/gitlab-agent/v16/internal/tool/tlstool"
"go.uber.org/zap/zaptest"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/credentials/insecure"
clocktesting "k8s.io/utils/clock/testing"
)
const (
t1 = "grpc://127.0.0.1:1"
t2 = "grpc://127.0.0.1:2"
)
var (
_ PoolInterface = (*Pool)(nil)
_ PoolConn = (*poolConn)(nil)
_ PoolInterface = (*PoolSelf)(nil)
_ PoolConn = (*selfPoolConn)(nil)
)
func TestKasPool_DialConnDifferentPort(t *testing.T) {
ctrl := gomock.NewController(t)
rep := mock_tool.NewMockErrReporter(ctrl)
p := NewPool(zaptest.NewLogger(t), rep, credentials.NewTLS(tlstool.DefaultClientTLSConfig()))
defer clz(t, p)
c1, err := p.Dial(context.Background(), t1)
require.NoError(t, err)
c1.Done()
c2, err := p.Dial(context.Background(), t2)
require.NoError(t, err)
assert.NotSame(t, c1, c2)
c2.Done()
}
func TestKasPool_DialConnSequentialReuse(t *testing.T) {
ctrl := gomock.NewController(t)
rep := mock_tool.NewMockErrReporter(ctrl)
p := NewPool(zaptest.NewLogger(t), rep, credentials.NewTLS(tlstool.DefaultClientTLSConfig()))
defer clz(t, p)
c1, err := p.Dial(context.Background(), t1)
require.NoError(t, err)
c1.Done()
c2, err := p.Dial(context.Background(), t1)
require.NoError(t, err)
assert.Same(t, c1.(*poolConn).ClientConn, c2.(*poolConn).ClientConn)
c2.Done()
}
func TestKasPool_DialConnConcurrentReuse(t *testing.T) {
ctrl := gomock.NewController(t)
rep := mock_tool.NewMockErrReporter(ctrl)
p := NewPool(zaptest.NewLogger(t), rep, credentials.NewTLS(tlstool.DefaultClientTLSConfig()))
defer clz(t, p)
c1, err := p.Dial(context.Background(), t1)
require.NoError(t, err)
c2, err := p.Dial(context.Background(), t1)
require.NoError(t, err)
assert.Same(t, c1.(*poolConn).ClientConn, c2.(*poolConn).ClientConn)
c1.Done()
c2.Done()
}
func TestKasPool_CloseClosesAllConnections(t *testing.T) {
ctrl := gomock.NewController(t)
rep := mock_tool.NewMockErrReporter(ctrl)
p := NewPool(zaptest.NewLogger(t), rep, credentials.NewTLS(tlstool.DefaultClientTLSConfig()))
c, err := p.Dial(context.Background(), t1)
require.NoError(t, err)
c.Done()
require.NoError(t, p.Close())
assert.Empty(t, p.conns)
}
func TestKasPool_DonePanicsOnMultipleInvocations(t *testing.T) {
ctrl := gomock.NewController(t)
rep := mock_tool.NewMockErrReporter(ctrl)
p := NewPool(zaptest.NewLogger(t), rep, credentials.NewTLS(tlstool.DefaultClientTLSConfig()))
defer clz(t, p)
c, err := p.Dial(context.Background(), t1)
require.NoError(t, err)
c.Done()
assert.PanicsWithError(t, "pool connection Done() called more than once", func() {
c.Done()
})
}
func TestKasPool_DoneEvictsExpiredIdleConnections(t *testing.T) {
start := time.Now()
tClock := clocktesting.NewFakePassiveClock(start)
p := &Pool{
log: zaptest.NewLogger(t),
dialOpts: []grpc.DialOption{grpc.WithTransportCredentials(insecure.NewCredentials())},
conns: map[string]*connHolder{},
clk: tClock,
}
defer clz(t, p)
c1, err := p.Dial(context.Background(), t1)
require.NoError(t, err)
c1.Done()
tClock.SetTime(start.Add(2 * evictIdleConnAfter))
p.runGcLocked()
assert.Empty(t, p.conns)
}
func clz(t *testing.T, c io.Closer) {
assert.NoError(t, c.Close())
}
|