diff options
Diffstat (limited to 'internal/git/catfile/batch_test.go')
-rw-r--r-- | internal/git/catfile/batch_test.go | 87 |
1 files changed, 49 insertions, 38 deletions
diff --git a/internal/git/catfile/batch_test.go b/internal/git/catfile/batch_test.go index 570e878d5..d0a2766d8 100644 --- a/internal/git/catfile/batch_test.go +++ b/internal/git/catfile/batch_test.go @@ -14,6 +14,8 @@ import ( "github.com/stretchr/testify/require" "gitlab.com/gitlab-org/gitaly/internal/command" "gitlab.com/gitlab-org/gitaly/internal/git" + "gitlab.com/gitlab-org/gitaly/internal/git/repository" + "gitlab.com/gitlab-org/gitaly/internal/gitaly/config" "gitlab.com/gitlab-org/gitaly/internal/helper/text" "gitlab.com/gitlab-org/gitaly/internal/testhelper" "gitlab.com/gitlab-org/gitaly/internal/testhelper/testcfg" @@ -21,22 +23,43 @@ import ( "google.golang.org/grpc/metadata" ) -func setupBatch(t *testing.T, ctx context.Context) Batch { +type repoExecutor struct { + repository.GitRepo + gitCmdFactory git.CommandFactory +} + +func (e *repoExecutor) Exec(ctx context.Context, cmd git.Cmd, opts ...git.CmdOpt) (*command.Command, error) { + return e.gitCmdFactory.New(ctx, e.GitRepo, cmd, opts...) +} + +func (e *repoExecutor) ExecAndWait(ctx context.Context, cmd git.Cmd, opts ...git.CmdOpt) error { + command, err := e.Exec(ctx, cmd, opts...) + if err != nil { + return err + } + return command.Wait() +} + +func setupBatch(t *testing.T, ctx context.Context) (config.Cfg, Batch, *gitalypb.Repository) { t.Helper() cfg, repo, _ := testcfg.BuildWithRepo(t) + repoExecutor := &repoExecutor{ + GitRepo: repo, gitCmdFactory: git.NewExecCommandFactory(cfg), + } - c, err := New(ctx, git.NewExecCommandFactory(cfg), repo) + cache := newCache(1*time.Hour, 1000, defaultEvictionInterval) + batch, err := cache.BatchProcess(ctx, repoExecutor) require.NoError(t, err) - return c + return cfg, batch, repo } func TestInfo(t *testing.T) { ctx, cancel := testhelper.Context() defer cancel() - c := setupBatch(t, ctx) + _, c, _ := setupBatch(t, ctx) testCases := []struct { desc string @@ -68,10 +91,9 @@ func TestBlob(t *testing.T) { ctx, cancel := testhelper.Context() defer cancel() - c := setupBatch(t, ctx) + _, c, _ := setupBatch(t, ctx) - gitignoreBytes, err := ioutil.ReadFile("testdata/blob-dfaa3f97ca337e20154a98ac9d0be76ddd1fcc82") - require.NoError(t, err) + gitignoreBytes := testhelper.MustReadFile(t, "testdata/blob-dfaa3f97ca337e20154a98ac9d0be76ddd1fcc82") testCases := []struct { desc string @@ -131,10 +153,9 @@ func TestCommit(t *testing.T) { ctx, cancel := testhelper.Context() defer cancel() - c := setupBatch(t, ctx) + _, c, _ := setupBatch(t, ctx) - commitBytes, err := ioutil.ReadFile("testdata/commit-e63f41fe459e62e1228fcef60d7189127aeba95a") - require.NoError(t, err) + commitBytes := testhelper.MustReadFile(t, "testdata/commit-e63f41fe459e62e1228fcef60d7189127aeba95a") testCases := []struct { desc string @@ -165,10 +186,9 @@ func TestTag(t *testing.T) { ctx, cancel := testhelper.Context() defer cancel() - c := setupBatch(t, ctx) + _, c, _ := setupBatch(t, ctx) - tagBytes, err := ioutil.ReadFile("testdata/tag-a509fa67c27202a2bc9dd5e014b4af7e6063ac76") - require.NoError(t, err) + tagBytes := testhelper.MustReadFile(t, "testdata/tag-a509fa67c27202a2bc9dd5e014b4af7e6063ac76") testCases := []struct { desc string @@ -228,10 +248,9 @@ func TestTree(t *testing.T) { ctx, cancel := testhelper.Context() defer cancel() - c := setupBatch(t, ctx) + _, c, _ := setupBatch(t, ctx) - treeBytes, err := ioutil.ReadFile("testdata/tree-7e2f26d033ee47cd0745649d1a28277c56197921") - require.NoError(t, err) + treeBytes := testhelper.MustReadFile(t, "testdata/tree-7e2f26d033ee47cd0745649d1a28277c56197921") testCases := []struct { desc string @@ -291,11 +310,10 @@ func TestRepeatedCalls(t *testing.T) { ctx, cancel := testhelper.Context() defer cancel() - c := setupBatch(t, ctx) + _, c, _ := setupBatch(t, ctx) treeOid := git.Revision("7e2f26d033ee47cd0745649d1a28277c56197921") - treeBytes, err := ioutil.ReadFile("testdata/tree-7e2f26d033ee47cd0745649d1a28277c56197921") - require.NoError(t, err) + treeBytes := testhelper.MustReadFile(t, "testdata/tree-7e2f26d033ee47cd0745649d1a28277c56197921") tree1Obj, err := c.Tree(ctx, treeOid) require.NoError(t, err) @@ -330,14 +348,12 @@ func TestRepeatedCalls(t *testing.T) { } func TestSpawnFailure(t *testing.T) { - defer func() { injectSpawnErrors = false }() - - // reset global cache - defer func(old *batchCache) { cache = old }(cache) + cfg, testRepo, _ := testcfg.BuildWithRepo(t) + testRepoExecutor := &repoExecutor{ + GitRepo: testRepo, gitCmdFactory: git.NewExecCommandFactory(cfg), + } - // Use very high values to effectively disable auto-expiry - cache = newCache(1*time.Hour, 1000) - defer cache.EvictAll() + cache := newCache(1*time.Hour, 1000, defaultEvictionInterval) require.True( t, @@ -349,12 +365,7 @@ func TestSpawnFailure(t *testing.T) { ctx1, cancel1 := testhelper.Context() defer cancel1() - cfg, testRepo, _ := testcfg.BuildWithRepo(t) - - gitCmdFactory := git.NewExecCommandFactory(cfg) - - injectSpawnErrors = false - _, err := catfileWithFreshSessionID(ctx1, gitCmdFactory, testRepo) + _, err := catfileWithFreshSessionID(ctx1, cache, testRepoExecutor) require.NoError(t, err, "catfile spawn should succeed in normal circumstances") require.Equal(t, 2, numGitChildren(t), "there should be 2 git child processes") @@ -369,7 +380,7 @@ func TestSpawnFailure(t *testing.T) { require.Equal(t, 2, numGitChildren(t), "there should still be 2 git child processes") - cache.EvictAll() + cache.Evict() require.Equal(t, 0, cacheSize(cache), "the cache should be empty now") require.True( @@ -381,8 +392,8 @@ func TestSpawnFailure(t *testing.T) { ctx2, cancel2 := testhelper.Context() defer cancel2() - injectSpawnErrors = true - _, err = catfileWithFreshSessionID(ctx2, gitCmdFactory, testRepo) + cache.injectSpawnErrors = true + _, err = catfileWithFreshSessionID(ctx2, cache, testRepoExecutor) require.Error(t, err, "expect simulated error") require.IsType(t, &simulatedBatchSpawnError{}, err) @@ -393,7 +404,7 @@ func TestSpawnFailure(t *testing.T) { ) } -func catfileWithFreshSessionID(ctx context.Context, gitCmdFactory git.CommandFactory, repo *gitalypb.Repository) (Batch, error) { +func catfileWithFreshSessionID(ctx context.Context, cache Cache, repo git.RepositoryExecutor) (Batch, error) { id, err := text.RandomHex(4) if err != nil { return nil, err @@ -403,7 +414,7 @@ func catfileWithFreshSessionID(ctx context.Context, gitCmdFactory git.CommandFac SessionIDField: id, }) - return New(metadata.NewIncomingContext(ctx, md), gitCmdFactory, repo) + return cache.BatchProcess(metadata.NewIncomingContext(ctx, md), repo) } func waitTrue(callback func() bool) bool { @@ -431,7 +442,7 @@ func numGitChildren(t *testing.T) int { return bytes.Count(out, []byte("\n")) } -func cacheSize(bc *batchCache) int { +func cacheSize(bc *BatchCache) int { bc.Lock() defer bc.Unlock() return bc.len() |