summaryrefslogtreecommitdiff
path: root/internal/gitaly/gitaly_test.go
blob: ae3098b10168bc1420ab74649854b66c0acf716a (plain)
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
package gitaly

import (
	"context"
	"testing"

	"github.com/prometheus/client_golang/prometheus/testutil"
	"github.com/stretchr/testify/require"

	"gitlab.com/gitlab-org/gitlab-shell/v14/internal/metrics"
)

func TestPrometheusMetrics(t *testing.T) {
	metrics.GitalyConnectionsTotal.Reset()

	c := newClient()

	cmd := Command{ServiceName: "git-upload-pack", Address: "tcp://localhost:9999"}
	c.newConnection(context.Background(), cmd)
	c.newConnection(context.Background(), cmd)

	require.Equal(t, 1, testutil.CollectAndCount(metrics.GitalyConnectionsTotal))
	require.InDelta(t, 2, testutil.ToFloat64(metrics.GitalyConnectionsTotal.WithLabelValues("ok")), 0.1)
	require.InDelta(t, 0, testutil.ToFloat64(metrics.GitalyConnectionsTotal.WithLabelValues("fail")), 0.1)

	cmd = Command{Address: ""}
	c.newConnection(context.Background(), cmd)

	require.InDelta(t, 2, testutil.ToFloat64(metrics.GitalyConnectionsTotal.WithLabelValues("ok")), 0.1)
	require.InDelta(t, 1, testutil.ToFloat64(metrics.GitalyConnectionsTotal.WithLabelValues("fail")), 0.1)
}

func TestCachedConnections(t *testing.T) {
	c := newClient()

	require.Len(t, c.cache.connections, 0)

	cmd := Command{ServiceName: "git-upload-pack", Address: "tcp://localhost:9999"}

	conn, err := c.GetConnection(context.Background(), cmd)
	require.NoError(t, err)
	require.Len(t, c.cache.connections, 1)

	newConn, err := c.GetConnection(context.Background(), cmd)
	require.NoError(t, err)
	require.Len(t, c.cache.connections, 1)
	require.Equal(t, conn, newConn)

	cmd = Command{ServiceName: "git-upload-pack", Address: "tcp://localhost:9998"}
	_, err = c.GetConnection(context.Background(), cmd)
	require.NoError(t, err)
	require.Len(t, c.cache.connections, 2)
}

func newClient() *Client {
	c := &Client{}
	c.InitSidechannelRegistry(context.Background())
	return c
}