kuma

Форк
0
/
pool_test.go 
116 строк · 2.8 Кб
1
package client_test
2

3
import (
4
	"context"
5
	"time"
6

7
	. "github.com/onsi/ginkgo/v2"
8
	. "github.com/onsi/gomega"
9
	"google.golang.org/grpc"
10
	"google.golang.org/grpc/connectivity"
11

12
	"github.com/kumahq/kuma/pkg/core"
13
	"github.com/kumahq/kuma/pkg/intercp/client"
14
	"github.com/kumahq/kuma/pkg/test"
15
)
16

17
var _ = Describe("Pool", func() {
18
	It("should not create a client when TLS is not configured", func() {
19
		// given
20
		pool := client.NewPool(nil, 1*time.Second, core.Now)
21

22
		// when
23
		_, err := pool.Client("http://192.168.0.1")
24

25
		// then
26
		Expect(err).To(Equal(client.TLSNotConfigured))
27
	})
28

29
	Context("configured with TLS", func() {
30
		var pool *client.Pool
31
		const idleDeadline = 100 * time.Millisecond
32
		var ticks chan time.Time
33
		var clock *test.Clock
34
		var cancelFn func()
35

36
		BeforeEach(func() {
37
			clock = test.NewClock(time.Now())
38
			ticks = make(chan time.Time)
39
			pool = client.NewPool(func(s string, config *client.TLSConfig) (client.Conn, error) {
40
				return &testConn{
41
					state: connectivity.Ready,
42
				}, nil
43
			}, idleDeadline, clock.Now)
44
			pool.SetTLSConfig(&client.TLSConfig{})
45
			ctx, c := context.WithCancel(context.Background())
46
			cancelFn = c
47
			go pool.StartCleanup(ctx, &time.Ticker{C: ticks})
48
		})
49

50
		AfterEach(func() {
51
			cancelFn()
52
		})
53

54
		It("should keep the connection open", func() {
55
			// when
56
			c, err := pool.Client("http://192.168.0.1")
57
			Expect(err).ToNot(HaveOccurred())
58
			c2, err := pool.Client("http://192.168.0.1")
59
			Expect(err).ToNot(HaveOccurred())
60

61
			// then
62
			Expect(c).To(BeIdenticalTo(c2))
63
		})
64

65
		It("should create a new connection after the previous expired", func() {
66
			// given
67
			c, err := pool.Client("http://192.168.0.1")
68
			Expect(err).ToNot(HaveOccurred())
69

70
			// when
71
			clock.Add(idleDeadline + 1*time.Millisecond)
72
			ticks <- clock.Now()
73
			ticks <- clock.Now() // send a second tick to make sure that the cleanup triggered by the first one is done
74
			c2, err := pool.Client("http://192.168.0.1")
75
			Expect(err).ToNot(HaveOccurred())
76

77
			// then
78
			Expect(c).NotTo(BeIdenticalTo(c2))
79
			Expect(c.(*testConn).closed).To(BeTrue())
80
		})
81

82
		It("should close broken connection and create a new one", func() {
83
			// given
84
			c, err := pool.Client("http://192.168.0.1")
85
			Expect(err).ToNot(HaveOccurred())
86
			c.(*testConn).state = connectivity.TransientFailure
87

88
			// when
89
			ticks <- clock.Now()
90
			ticks <- clock.Now() // send a second tick to make sure that the cleanup triggered by the first one is done
91
			c2, err := pool.Client("http://192.168.0.1")
92
			Expect(err).ToNot(HaveOccurred())
93

94
			// then
95
			Expect(c).NotTo(BeIdenticalTo(c2))
96
			Expect(c.(*testConn).closed).To(BeTrue())
97
		})
98
	})
99
})
100

101
type testConn struct {
102
	*grpc.ClientConn
103
	closed bool
104
	state  connectivity.State
105
}
106

107
var _ client.Conn = &testConn{}
108

109
func (t *testConn) Close() error {
110
	t.closed = true
111
	return nil
112
}
113

114
func (t *testConn) GetState() connectivity.State {
115
	return t.state
116
}
117

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.