Welcome to mirror list, hosted at ThFree Co, Russian Federation.

cache_test.go « interceptor « internal - gitlab.com/gitlab-org/gitaly.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ae1fa45e418e07c1bb93ced14bc0a0b92b5fad54 (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
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
120
121
122
123
124
125
126
127
128
129
130
131
package interceptor_test

import (
	"context"
	"net"
	"testing"
	"time"

	"github.com/golang/protobuf/proto"
	"github.com/golang/protobuf/protoc-gen-go/descriptor"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
	"gitlab.com/gitlab-org/gitaly-proto/go/gitalypb"
	"gitlab.com/gitlab-org/gitaly/internal/interceptor"
	"gitlab.com/gitlab-org/gitaly/internal/interceptor/testdata"
	"gitlab.com/gitlab-org/gitaly/internal/praefect/protoregistry"
	"google.golang.org/grpc"
)

//go:generate make testdata/stream.pb.go
func TestStreamInvalidator(t *testing.T) {

	cache, repoQ := newMockCache()

	reg := protoregistry.New()
	require.NoError(t, reg.RegisterFiles(streamFileDesc(t)))

	srvr := grpc.NewServer(
		grpc.StreamInterceptor(
			grpc.StreamServerInterceptor(
				interceptor.StreamInvalidator(cache, reg),
			),
		),
	)

	ctx, cancel := context.WithTimeout(context.Background(), time.Second)
	defer cancel()

	svc := &testSvc{}

	cli, cleanup := newTestSvc(t, ctx, srvr, svc)
	defer cleanup()

	expectedInvalidations := []*gitalypb.Repository{
		&gitalypb.Repository{
			GitAlternateObjectDirectories: []string{"1"},
			GitObjectDirectory:            "1",
			GlProjectPath:                 "1",
			GlRepository:                  "1",
			RelativePath:                  "1",
			StorageName:                   "1",
		},
	}

	go func() {
		_, err := cli.ClientStreamRepoMutator(ctx, &testdata.Request{
			Destination: expectedInvalidations[0],
		})
		assert.NoError(t, err)
	}()

	for i := 0; i < len(expectedInvalidations); i++ {
		t.Logf("waiting for repo invalidation #%d", i)
		select {
		case repo := <-repoQ:
			require.Equal(t, expectedInvalidations[i], repo)
		case <-ctx.Done():
			break
		}

	}

}

// mockCache allows us to relay back via channel which repos are being
// invalidated in the cache
type mockCache struct {
	invalidatedRepo chan<- *gitalypb.Repository
}

func newMockCache() (interceptor.RepoCache, <-chan *gitalypb.Repository) {
	repoQ := make(chan *gitalypb.Repository)
	return &mockCache{repoQ}, repoQ
}

func (mc mockCache) InvalidateRepo(repo *gitalypb.Repository) error {
	mc.invalidatedRepo <- repo
	return nil
}

func streamFileDesc(t testing.TB) *descriptor.FileDescriptorProto {
	fdp, err := protoregistry.ExtractFileDescriptor(proto.FileDescriptor("stream.proto"))
	require.NoError(t, err)
	return fdp
}

func newTestSvc(t testing.TB, ctx context.Context, srvr *grpc.Server, svc testdata.TestServiceServer) (testdata.TestServiceClient, func()) {
	testdata.RegisterTestServiceServer(srvr, svc)

	lis, err := net.Listen("tcp", ":0")
	require.NoError(t, err)

	errQ := make(chan error)

	go func() {
		errQ <- srvr.Serve(lis)
	}()

	cleanup := func() {
		require.NoError(t, <-errQ)
	}

	cc, err := grpc.DialContext(
		ctx,
		lis.Addr().String(),
		grpc.WithBlock(),
		grpc.WithInsecure(),
	)
	require.NoError(t, err)

	return testdata.NewTestServiceClient(cc), cleanup
}

type testSvc struct{}

func (ts *testSvc) ClientStreamRepoMutator(*testdata.Request, testdata.TestService_ClientStreamRepoMutatorServer) error {
	return nil
}
func (ts *testSvc) ClientStreamRepoAccessor(*testdata.Request, testdata.TestService_ClientStreamRepoAccessorServer) error {
	return nil
}