Git designed for AI agents.
test(storagecore): make the import deadline tests independent of load
Federico Elia8bd39471d86f
diff --git a/internal/gitaly/service/storagecore/import_deadline_test.go b/internal/gitaly/service/storagecore/import_deadline_test.goindex 3a37ebe..575d880 100644--- a/internal/gitaly/service/storagecore/import_deadline_test.go+++ b/internal/gitaly/service/storagecore/import_deadline_test.go@@ -4,7 +4,6 @@ import ("context""fmt""io"- "sync/atomic""testing""time"@@ -18,56 +17,56 @@ import (// The tests of this file check the write deadline of the import's pack// (doc/graft/PLAN7.md, PACK OWNERSHIP RULE 4): an upload, or the commit of the-// changeset that references it, that would end after the deadline fails like-// a storage failure, no changeset references the pack, and the import is-// resumable as any failed import (IMPORT RULE).+// changeset that references it, that would end after the deadline fails like a+// storage failure and no changeset references the pack. The import is then+// incomplete (head sequence 0), which the IMPORT RULE tests show is resumed by+// the next ImportRepository; they run with the production deadline, while these+// tests use a short one that the server must keep for its whole life.++// slowMode is how a slowBlobs bucket misses the write deadline.+type slowMode intconst (- slowNone int32 = iota- slowBlock // Put waits for the end of its context and stores nothing- slowLate // Put stores the object, then returns only after the deadline+ // slowBlock: Put waits for the end of its context and stores nothing.+ slowBlock slowMode = iota+ // slowLate: Put stores the object, whatever the deadline, and returns only+ // once the deadline has passed.+ slowLate)-// slowBlobs is a bucket that is slower than the write deadline while its mode-// is not slowNone.+// slowBlobs is a bucket slower than the write deadline.type slowBlobs struct {corestore.BlobStore- mode *atomic.Int32+ mode slowMode}func (b slowBlobs) Put(ctx context.Context, key string, r io.Reader) error {- switch b.mode.Load() {- case slowBlock:+ if b.mode == slowBlock {<-ctx.Done()return ctx.Err()- case slowLate:- if err := b.BlobStore.Put(ctx, key, r); err != nil {- return err- }- <-ctx.Done()- return nil- default:- return b.BlobStore.Put(ctx, key, r)}++ if err := b.BlobStore.Put(context.WithoutCancel(ctx), key, r); err != nil {+ return err+ }+ <-ctx.Done()+ return nil}-// testWriteDeadline is the write deadline of these tests: long enough for a-// normal upload and commit of the test repository, short enough to wait out.-// The core is never changed while the server runs: the RPCs order the test and-// the server only through a socket, which the race detector does not see.+// testWriteDeadline is the write deadline of these tests, short enough to+// wait out. Nothing in them needs a normal upload to fit into it.const testWriteDeadline = 2 * time.Second-// setupSlow is setup with a slowBlobs bucket and a write deadline of w.-func setupSlow(t *testing.T, mode corestore.Mode, dialect sqlstore.Dialect, w time.Duration) (*harness, *atomic.Int32) {+// setupSlow is setup with a slowBlobs bucket in mode and the write deadline+// testWriteDeadline.+func setupSlow(t *testing.T, mode corestore.Mode, dialect sqlstore.Dialect, slow slowMode) *harness {t.Helper()- slow := &atomic.Int32{}- h := setup(t, mode, dialect, func(core *corestore.Core) {+ return setup(t, mode, dialect, func(core *corestore.Core) {core.Blobs = slowBlobs{BlobStore: core.Blobs, mode: slow}- core.GCMargin = w- core.GCGrace = 2 * w+ core.GCMargin = testWriteDeadline+ core.GCGrace = 2 * testWriteDeadline})- return h, slow}// bucketKeys lists the objects of the repository in the bucket.@@ -82,60 +81,41 @@ func (h *harness) bucketKeys(repoID int64) []string {return keys}+// failedImport runs an import that misses the write deadline and returns the+// ID of its registration, which is left incomplete.+func (h *harness) failedImport() int64 {+ h.t.Helper()++ _, err := h.client.ImportRepository(h.ctx, &gitalypb.ImportRepositoryRequest{Repository: h.repo})+ requireResumeHint(h.t, err, codes.Unavailable)+ require.ErrorContains(h.t, err, corestore.ErrPackDeadline.Error())++ state, err := h.client.GetStorageState(h.ctx, &gitalypb.GetStorageStateRequest{Repository: h.repo})+ require.NoError(h.t, err)+ repoID := state.GetRepositoryId()+ h.requireIncomplete(repoID)+ return repoID+}+func TestImportRepositoryUploadPastTheWriteDeadline(t *testing.T) {forEachCore(t, func(t *testing.T, mode corestore.Mode, dialect sqlstore.Dialect) {- h, slow := setupSlow(t, mode, dialect, testWriteDeadline)+ h := setupSlow(t, mode, dialect, slowBlock)h.seed()- localBefore := h.localRefs()- slow.Store(slowBlock)- _, err := h.client.ImportRepository(h.ctx, &gitalypb.ImportRepositoryRequest{Repository: h.repo})- requireResumeHint(t, err, codes.Unavailable)- require.ErrorContains(t, err, corestore.ErrPackDeadline.Error())-- state, err := h.client.GetStorageState(h.ctx, &gitalypb.GetStorageStateRequest{Repository: h.repo})- require.NoError(t, err)- repoID := state.GetRepositoryId()- h.requireIncomplete(repoID)+ repoID := h.failedImport()require.Empty(t, h.bucketKeys(repoID), "the cancelled upload stored nothing")-- slow.Store(slowNone)- _, err = h.client.ImportRepository(h.ctx, &gitalypb.ImportRepositoryRequest{Repository: h.repo})- require.NoError(t, err)- h.requireImported(repoID, localBefore)})}func TestImportRepositoryCommitPastTheWriteDeadline(t *testing.T) {forEachCore(t, func(t *testing.T, mode corestore.Mode, dialect sqlstore.Dialect) {- h, slow := setupSlow(t, mode, dialect, testWriteDeadline)+ h := setupSlow(t, mode, dialect, slowLate)h.seed()- localBefore := h.localRefs()-- // The upload completes, but only returns after the deadline: the changeset- // that would reference the pack is not committed.- slow.Store(slowLate)- _, err := h.client.ImportRepository(h.ctx, &gitalypb.ImportRepositoryRequest{Repository: h.repo})- requireResumeHint(t, err, codes.Unavailable)- require.ErrorContains(t, err, corestore.ErrPackDeadline.Error())-- state, err := h.client.GetStorageState(h.ctx, &gitalypb.GetStorageStateRequest{Repository: h.repo})- require.NoError(t, err)- repoID := state.GetRepositoryId()- h.requireIncomplete(repoID)- orphans := h.bucketKeys(repoID)- require.Len(t, orphans, 1, "the pack was stored")-- // The resumed import uploads its own pack; the late one stays an orphan- // that no changeset references.- slow.Store(slowNone)- _, err = h.client.ImportRepository(h.ctx, &gitalypb.ImportRepositoryRequest{Repository: h.repo})- require.NoError(t, err)- h.requireImported(repoID, localBefore)-- changesets, err := h.core.Meta.ListChangesets(h.ctx, repoID, 0, 1)- require.NoError(t, err)- require.NotEqual(t, orphans[0], changesets[0].PackKey)- require.ElementsMatch(t, []string{orphans[0], changesets[0].PackKey}, h.bucketKeys(repoID))++ // The upload completes, but returns only after the deadline: the+ // changeset that would reference the pack is not committed, and the pack+ // is an orphan that no changeset references.+ repoID := h.failedImport()+ require.Len(t, h.bucketKeys(repoID), 1, "the pack was stored")})}