Skip to content

Git designed for AI agents.

25.1 KB
Raw Blame
package catfile

import (
	"context"
	"errors"
	"fmt"
	"io"
	"os"
	"sync/atomic"
	"testing"
	"time"

	"github.com/feder-cr/git-ai/internal/git/gittest"
	"github.com/feder-cr/git-ai/internal/git/mvcc"
	"github.com/feder-cr/git-ai/internal/git/quarantine"
	"github.com/feder-cr/git-ai/internal/gitaly/config"
	"github.com/feder-cr/git-ai/internal/gitaly/storage"
	"github.com/feder-cr/git-ai/internal/helper"
	"github.com/feder-cr/git-ai/internal/testhelper"
	"github.com/feder-cr/git-ai/internal/testhelper/testcfg"
	"github.com/stretchr/testify/require"
)

func TestProcesses_add(t *testing.T) {
	ctx := testhelper.Context(t)

	const maxLen = 3
	p := &processes{maxLen: maxLen}

	cfg := testcfg.Build(t)
	repo, _ := gittest.CreateRepository(t, ctx, cfg, gittest.CreateRepositoryConfig{
		SkipCreationViaService: true,
	})

	key0 := mustCreateKey(t, "0", repo)
	value0, cancel := mustCreateCacheable(t, cfg, repo)
	p.Add(key0, value0, time.Now().Add(time.Hour), cancel)
	requireProcessesValid(t, p)

	key1 := mustCreateKey(t, "1", repo)
	value1, cancel := mustCreateCacheable(t, cfg, repo)
	p.Add(key1, value1, time.Now().Add(time.Hour), cancel)
	requireProcessesValid(t, p)

	key2 := mustCreateKey(t, "2", repo)
	value2, cancel := mustCreateCacheable(t, cfg, repo)
	p.Add(key2, value2, time.Now().Add(time.Hour), cancel)
	requireProcessesValid(t, p)

	// Because maxLen is 3, and key0 is oldest, we expect that adding key3
	// will kick out key0.
	key3 := mustCreateKey(t, "3", repo)
	value3, cancel := mustCreateCacheable(t, cfg, repo)
	p.Add(key3, value3, time.Now().Add(time.Hour), cancel)
	requireProcessesValid(t, p)

	require.Equal(t, maxLen, p.EntryCount(), "length should be maxLen")
	require.True(t, value0.isClosed(), "value0 should be closed")
	require.Equal(t, []key{key1, key2, key3}, keys(t, p))
}

func TestProcesses_addTwice(t *testing.T) {
	ctx := testhelper.Context(t)

	p := &processes{maxLen: 10}

	cfg := testcfg.Build(t)
	repo, _ := gittest.CreateRepository(t, ctx, cfg, gittest.CreateRepositoryConfig{
		SkipCreationViaService: true,
	})

	key0 := mustCreateKey(t, "0", repo)
	value0, cancel := mustCreateCacheable(t, cfg, repo)
	p.Add(key0, value0, time.Now().Add(time.Hour), cancel)
	requireProcessesValid(t, p)

	key1 := mustCreateKey(t, "1", repo)
	value1, cancel := mustCreateCacheable(t, cfg, repo)
	p.Add(key1, value1, time.Now().Add(time.Hour), cancel)
	requireProcessesValid(t, p)

	require.Equal(t, key0, p.head().key, "key0 should be oldest key")

	value2, cancel := mustCreateCacheable(t, cfg, repo)
	p.Add(key0, value2, time.Now().Add(time.Hour), cancel)
	requireProcessesValid(t, p)

	require.Equal(t, key1, p.head().key, "key1 should be oldest key")
	require.Equal(t, value1, p.head().value)

	require.True(t, value0.isClosed(), "value0 should be closed")
}

func TestProcesses_Checkout(t *testing.T) {
	ctx := testhelper.Context(t)

	p := &processes{maxLen: 10}

	cfg := testcfg.Build(t)
	repo, _ := gittest.CreateRepository(t, ctx, cfg, gittest.CreateRepositoryConfig{
		SkipCreationViaService: true,
	})

	key0 := mustCreateKey(t, "0", repo)
	value0, cancel := mustCreateCacheable(t, cfg, repo)
	p.Add(key0, value0, time.Now().Add(time.Hour), cancel)

	entry, ok := p.Checkout(key{sessionID: "foo"})
	requireProcessesValid(t, p)
	require.Nil(t, entry, "expect nil value when key not found")
	require.False(t, ok, "ok flag")

	entry, ok = p.Checkout(key0)
	requireProcessesValid(t, p)

	require.Equal(t, value0, entry.value)
	require.True(t, ok, "ok flag")

	require.False(t, entry.value.isClosed(), "value should not be closed after checkout")

	entry, ok = p.Checkout(key0)
	require.False(t, ok, "ok flag after second checkout")
	require.Nil(t, entry, "value from second checkout")
}

func TestProcesses_EnforceTTL(t *testing.T) {
	ctx := testhelper.Context(t)

	p := &processes{maxLen: 10}

	cfg := testcfg.Build(t)
	repo, _ := gittest.CreateRepository(t, ctx, cfg, gittest.CreateRepositoryConfig{
		SkipCreationViaService: true,
	})

	cutoff := time.Now()

	key0 := mustCreateKey(t, "0", repo)
	value0, cancel := mustCreateCacheable(t, cfg, repo)
	p.Add(key0, value0, cutoff.Add(-time.Hour), cancel)

	key1 := mustCreateKey(t, "1", repo)
	value1, cancel := mustCreateCacheable(t, cfg, repo)
	p.Add(key1, value1, cutoff.Add(-time.Millisecond), cancel)

	key2 := mustCreateKey(t, "2", repo)
	value2, cancel := mustCreateCacheable(t, cfg, repo)
	p.Add(key2, value2, cutoff.Add(time.Millisecond), cancel)

	key3 := mustCreateKey(t, "3", repo)
	value3, cancel := mustCreateCacheable(t, cfg, repo)
	p.Add(key3, value3, cutoff.Add(time.Hour), cancel)

	requireProcessesValid(t, p)

	// We expect this cutoff to cause eviction of key0 and key1 but no other keys.
	p.EnforceTTL(cutoff)

	requireProcessesValid(t, p)

	for i, v := range []cacheable{value0, value1} {
		require.True(t, v.isClosed(), "value %d %v should be closed", i, v)
	}

	require.Equal(t, []key{key2, key3}, keys(t, p), "remaining keys after EnforceTTL")

	p.EnforceTTL(cutoff)

	requireProcessesValid(t, p)
	require.Equal(t, []key{key2, key3}, keys(t, p), "remaining keys after second EnforceTTL")
}

func TestCache_autoExpiry(t *testing.T) {
	ctx := testhelper.Context(t)

	monitorTicker := helper.NewManualTicker()

	c := newCache(time.Hour, 10, monitorTicker)
	defer c.Stop()

	cfg := testcfg.Build(t)
	repo, _ := gittest.CreateRepository(t, ctx, cfg, gittest.CreateRepositoryConfig{
		SkipCreationViaService: true,
	})

	// Add a process that has expired already.
	key0 := mustCreateKey(t, "0", repo)
	value0, cancel := mustCreateCacheable(t, cfg, repo)
	c.objectReaders.Add(key0, value0, time.Now().Add(-time.Millisecond), cancel)
	requireProcessesValid(t, &c.objectReaders)

	require.Contains(t, keys(t, &c.objectReaders), key0, "key should still be in map")
	require.False(t, value0.isClosed(), "value should not have been closed")

	// We need to tick thrice to get deterministic results: the first tick is discarded before
	// the monitor enters the loop, the second tick will be consumed and kicks off the eviction
	// but doesn't yet guarantee that the eviction has finished, and the third tick will then
	// start another eviction, which means that the previous eviction is done.
	monitorTicker.Tick()
	monitorTicker.Tick()
	monitorTicker.Tick()

	require.Empty(t, keys(t, &c.objectReaders), "key should no longer be in map")
	require.True(t, value0.isClosed(), "value should be closed after eviction")
}

func TestRoundToNearestFiveMinutes(t *testing.T) {
	testCases := []struct {
		minute           int
		expectedInterval int
	}{
		{
			minute:           2,
			expectedInterval: 5,
		},
		{
			minute:           5,
			expectedInterval: 10,
		},
		{
			minute:           6,
			expectedInterval: 10,
		},
		{
			minute:           11,
			expectedInterval: 15,
		},
		{
			minute:           22,
			expectedInterval: 25,
		},
		{
			minute:           34,
			expectedInterval: 35,
		},
		{
			minute:           49,
			expectedInterval: 50,
		},
		{
			minute:           54,
			expectedInterval: 55,
		},
		{
			minute:           58,
			expectedInterval: 60,
		},
	}

	base := time.Date(2024, 9, 25, 1, 0, 0, 0, time.UTC)
	for _, tc := range testCases {
		t.Run(fmt.Sprintf("%d minutes", tc.minute), func(t *testing.T) {
			require.Equal(t,
				tc.expectedInterval,
				roundToNearestFiveMinute(base.Add(time.Duration(tc.minute)*time.Minute)),
			)
		})
	}
}

func TestCache_ObjectReader(t *testing.T) {
	t.Parallel()

	ctx := testhelper.Context(t)
	cfg := testcfg.Build(t)

	repo, repoPath := gittest.CreateRepository(t, ctx, cfg, gittest.CreateRepositoryConfig{
		SkipCreationViaService: true,
	})
	gittest.WriteCommit(t, cfg, repoPath, gittest.WithBranch("main"))

	repoExecutor := newRepoExecutor(t, cfg, repo)

	cache := newCache(time.Hour, 10, helper.NewManualTicker())
	defer cache.Stop()

	t.Run("cached", func(t *testing.T) {
		defer cache.Evict()

		reader, cancel, err := cache.ObjectReader(ctx, repoExecutor)
		require.NoError(t, err)

		// Cancel the context such that the process will be considered for return to the
		// cache and wait for the cache to collect it.
		cancel()

		allKeys := keys(t, &cache.objectReaders)

		expectedSessionID := fmt.Sprintf("%d", roundToNearestFiveMinute(time.Now()))
		require.Equal(t, []key{{
			sessionID:   expectedSessionID,
			repoStorage: repo.GetStorageName(),
			repoRelPath: repo.GetRelativePath(),
		}}, allKeys)

		// Assert that we can still read from the cached process.
		_, err = reader.Object(ctx, "refs/heads/main")
		require.NoError(t, err)
	})

	t.Run("dirty process does not get cached", func(t *testing.T) {
		defer cache.Evict()

		reader, cancel, err := cache.ObjectReader(ctx, repoExecutor)
		require.NoError(t, err)

		// While we request object data, we do not consume it at all. The reader is thus
		// dirty and cannot be reused and shouldn't be returned to the cache.
		object, err := reader.Object(ctx, "refs/heads/main")
		require.NoError(t, err)

		// Cancel the process such that it will be considered for return to the cache.
		cancel()

		require.Empty(t, keys(t, &cache.objectReaders))

		// The process should be killed now, so reading the object must fail.
		_, err = io.ReadAll(object)
		require.True(t, errors.Is(err, os.ErrClosed))
	})

	t.Run("closed process does not get cached", func(t *testing.T) {
		defer cache.Evict()

		reader, cancel, err := cache.ObjectReader(ctx, repoExecutor)
		require.NoError(t, err)

		// Closed processes naturally cannot be reused anymore and thus shouldn't ever get
		// cached.
		reader.close()

		// Cancel the process such that it will be considered for return to the cache.
		cancel()

		require.Empty(t, keys(t, &cache.objectReaders))
	})
}

func TestCache_ObjectReaderWithoutMailmap(t *testing.T) {
	t.Parallel()

	ctx := testhelper.Context(t)
	cfg := testcfg.Build(t)

	repo, repoPath := gittest.CreateRepository(t, ctx, cfg, gittest.CreateRepositoryConfig{
		SkipCreationViaService: true,
	})
	gittest.WriteCommit(t, cfg, repoPath, gittest.WithBranch("main"))

	repoExecutor := newRepoExecutor(t, cfg, repo)

	cache := newCache(time.Hour, 10, helper.NewManualTicker())
	defer cache.Stop()

	t.Run("cached", func(t *testing.T) {
		defer cache.Evict()

		reader, cancel, err := cache.ObjectReaderWithoutMailmap(ctx, repoExecutor)
		require.NoError(t, err)

		// Cancel the context such that the process will be considered for return to the
		// cache and wait for the cache to collect it.
		cancel()

		allKeys := keys(t, &cache.objectReadersWithoutMailmap)

		expectedSessionID := fmt.Sprintf("%d", roundToNearestFiveMinute(time.Now()))

		require.Equal(t, []key{{
			sessionID:   expectedSessionID,
			repoStorage: repo.GetStorageName(),
			repoRelPath: repo.GetRelativePath(),
		}}, allKeys)

		// Assert that we can still read from the cached process.
		_, err = reader.Object(ctx, "refs/heads/main")
		require.NoError(t, err)
	})

	t.Run("dirty process does not get cached", func(t *testing.T) {
		defer cache.Evict()

		reader, cancel, err := cache.ObjectReaderWithoutMailmap(ctx, repoExecutor)
		require.NoError(t, err)

		// While we request object data, we do not consume it at all. The reader is thus
		// dirty and cannot be reused and shouldn't be returned to the cache.
		object, err := reader.Object(ctx, "refs/heads/main")
		require.NoError(t, err)

		// Cancel the process such that it will be considered for return to the cache.
		cancel()

		require.Empty(t, keys(t, &cache.objectReadersWithoutMailmap))

		// The process should be killed now, so reading the object must fail.
		_, err = io.ReadAll(object)
		require.True(t, errors.Is(err, os.ErrClosed))
	})

	t.Run("closed process does not get cached", func(t *testing.T) {
		defer cache.Evict()

		reader, cancel, err := cache.ObjectReaderWithoutMailmap(ctx, repoExecutor)
		require.NoError(t, err)

		// Closed processes naturally cannot be reused anymore and thus shouldn't ever get
		// cached.
		reader.close()

		// Cancel the process such that it will be considered for return to the cache.
		cancel()

		require.Empty(t, keys(t, &cache.objectReadersWithoutMailmap))
	})
}

func TestCache_ObjectReader_quarantine(t *testing.T) {
	t.Parallel()

	ctx := testhelper.Context(t)
	cfg := testcfg.Build(t)

	locator := config.NewLocator(cfg)
	logger := testhelper.NewLogger(t)

	cache := newCache(time.Hour, 10, helper.NewManualTicker())
	defer cache.Stop()

	t.Run("with active quarantine", func(t *testing.T) {
		defer cache.Evict()

		repo, repoPath := gittest.CreateRepository(t, ctx, cfg, gittest.CreateRepositoryConfig{
			SkipCreationViaService: true,
		})
		gittest.WriteCommit(t, cfg, repoPath, gittest.WithBranch("main"))

		quarantineDir, cleanup, err := quarantine.New(testhelper.Context(t), repo, logger, locator)
		require.NoError(t, err)
		t.Cleanup(cleanup)

		quarantineRepo := quarantineDir.QuarantinedRepo()

		reader, cancel, err := cache.ObjectReaderWithoutMailmap(ctx, newRepoExecutor(t, cfg, quarantineRepo))
		require.NoError(t, err)

		// Cancel the context such that the process will be considered for return to the
		// cache and wait for the cache to collect it.
		cancel()

		allKeys := keys(t, &cache.objectReadersWithoutMailmap)

		expectedSessionID := fmt.Sprintf("%d", roundToNearestFiveMinute(time.Now()))

		require.Equal(t, []key{{
			sessionID:   expectedSessionID,
			repoStorage: repo.GetStorageName(),
			repoRelPath: repo.GetRelativePath(),
			repoObjDir:  quarantineRepo.GetGitObjectDirectory(),
			repoAltDir:  "objects",
		}}, allKeys)

		// Assert that we can still read from the cached process.
		_, err = reader.Object(ctx, "refs/heads/main")
		require.NoError(t, err)
	})
}

// fakeMVCCCache is an mvcc.Cache pinned to a fixed base manifest pointer.
// Environment() is deliberately left empty so that the git-cat-file(1) processes
// spawned by these tests are plain ones and the tests do not require the bundled
// MVCC Git binary.
type fakeMVCCCache struct {
	baseManifestPointer string
	readOnly            bool
}

func (c fakeMVCCCache) Prepare(context.Context)                     {}
func (c fakeMVCCCache) PrepareWithManifest(context.Context, string) {}
func (c fakeMVCCCache) WaitUntilReady(context.Context) error        { return nil }
func (c fakeMVCCCache) Commit(context.Context) error                { return nil }
func (c fakeMVCCCache) SetCommitConflictError(error)                {}
func (c fakeMVCCCache) AfterCommit(func()) bool                     { return false }
func (c fakeMVCCCache) Environment() []mvcc.EnvVar                  { return nil }
func (c fakeMVCCCache) BaseManifestPointer() string                 { return c.baseManifestPointer }

func (c fakeMVCCCache) ReadOnly() bool                                           { return c.readOnly }
func (c fakeMVCCCache) WalkManifest(context.Context, mvcc.ArtifactVisitor) error { return nil }

func TestCache_ObjectReader_mvcc(t *testing.T) {
	t.Parallel()

	ctx := testhelper.Context(t)
	cfg := testcfg.Build(t)

	repo, repoPath := gittest.CreateRepository(t, ctx, cfg, gittest.CreateRepositoryConfig{
		SkipCreationViaService: true,
	})
	gittest.WriteCommit(t, cfg, repoPath, gittest.WithBranch("main"))

	repoExecutor := newRepoExecutor(t, cfg, repo)

	cache := newCache(time.Hour, 10, helper.NewManualTicker())
	defer cache.Stop()

	// ctxWithManifest returns a context carrying a read-only MVCC cache pinned to the
	// given manifest, mimicking what the MVCC middleware injects for the duration of
	// an accessor RPC. Only read-only RPCs are cacheable, so this is the context the
	// caching assertions below are written against.
	ctxWithManifest := func(manifest string) context.Context {
		return mvcc.ContextWithCache(ctx, fakeMVCCCache{
			baseManifestPointer: manifest,
			readOnly:            true,
		})
	}

	expectedKey := func(manifest string) key {
		return key{
			sessionID:    fmt.Sprintf("%d", roundToNearestFiveMinute(time.Now())),
			mvccManifest: manifest,
			repoStorage:  repo.GetStorageName(),
			repoRelPath:  repo.GetRelativePath(),
		}
	}

	t.Run("manifest is part of the cache key", func(t *testing.T) {
		defer cache.Evict()

		mvccCtx := ctxWithManifest("manifest-1")

		reader, cancel, err := cache.ObjectReader(mvccCtx, repoExecutor)
		require.NoError(t, err)

		// Cancel the context such that the process will be considered for return to
		// the cache.
		cancel()

		require.Equal(t, []key{expectedKey("manifest-1")}, keys(t, &cache.objectReaders))

		// Assert that we can still read from the cached process.
		_, err = reader.Object(mvccCtx, "refs/heads/main")
		require.NoError(t, err)
	})

	t.Run("process is reused for the same manifest", func(t *testing.T) {
		defer cache.Evict()

		_, cancel, err := cache.ObjectReader(ctxWithManifest("manifest-1"), repoExecutor)
		require.NoError(t, err)
		cancel()

		require.Equal(t, []key{expectedKey("manifest-1")}, keys(t, &cache.objectReaders))

		// Requesting a reader for the same manifest must check out the cached
		// process instead of spawning a new one, which empties the cache.
		_, cancel, err = cache.ObjectReader(ctxWithManifest("manifest-1"), repoExecutor)
		require.NoError(t, err)
		require.Empty(t, keys(t, &cache.objectReaders))

		cancel()
		require.Equal(t, []key{expectedKey("manifest-1")}, keys(t, &cache.objectReaders))
	})

	t.Run("process is not reused across manifests", func(t *testing.T) {
		defer cache.Evict()

		_, cancel, err := cache.ObjectReader(ctxWithManifest("manifest-1"), repoExecutor)
		require.NoError(t, err)
		cancel()

		require.Equal(t, []key{expectedKey("manifest-1")}, keys(t, &cache.objectReaders))

		// A process spawned under a different manifest observes a different view of
		// the repository, so the cached one must be left alone and a new process
		// spawned instead.
		_, cancel, err = cache.ObjectReader(ctxWithManifest("manifest-2"), repoExecutor)
		require.NoError(t, err)
		require.Equal(t, []key{expectedKey("manifest-1")}, keys(t, &cache.objectReaders))

		cancel()
		require.ElementsMatch(t, []key{
			expectedKey("manifest-1"),
			expectedKey("manifest-2"),
		}, keys(t, &cache.objectReaders))
	})

	t.Run("processes are not cached for non-read-only RPCs", func(t *testing.T) {
		defer cache.Evict()

		// A mutator holds GIT_MVCC_MANIFEST_PATH pointing at its own in-flight
		// view of the repository. Handing that process to a later RPC would let
		// it read and write through a stale view, so such processes must never
		// enter the cache.
		mvccCtx := mvcc.ContextWithCache(ctx, fakeMVCCCache{
			baseManifestPointer: "manifest-1",
			readOnly:            false,
		})

		_, cancel, err := cache.ObjectReader(mvccCtx, repoExecutor)
		require.NoError(t, err)

		// Cancelling is what returns a cacheable process to the cache. This one
		// must be killed instead, leaving the cache untouched.
		//
		// Note that no object is read through the reader before cancelling: an
		// unconsumed read leaves the queue dirty, which suppresses caching on its
		// own and would make this assertion pass regardless of the gating.
		cancel()
		require.Empty(t, keys(t, &cache.objectReaders))
	})

	t.Run("read-only process is not reused by a non-read-only RPC", func(t *testing.T) {
		defer cache.Evict()

		_, cancel, err := cache.ObjectReader(ctxWithManifest("manifest-1"), repoExecutor)
		require.NoError(t, err)
		cancel()

		require.Equal(t, []key{expectedKey("manifest-1")}, keys(t, &cache.objectReaders))

		// Even on a matching manifest, a mutator must not check out the cached
		// read-only process: it is uncacheable, so the cached entry is left
		// untouched and a fresh process is spawned.
		_, cancel, err = cache.ObjectReader(mvcc.ContextWithCache(ctx, fakeMVCCCache{
			baseManifestPointer: "manifest-1",
			readOnly:            false,
		}), repoExecutor)
		require.NoError(t, err)
		require.Equal(t, []key{expectedKey("manifest-1")}, keys(t, &cache.objectReaders))

		cancel()
		require.Equal(t, []key{expectedKey("manifest-1")}, keys(t, &cache.objectReaders))
	})

	t.Run("processes are not cached when the manifest pointer is empty", func(t *testing.T) {
		defer cache.Evict()

		// An empty manifest pointer is what mvcc.Cache reports for a view it has
		// not resolved yet, so it identifies no repository state at all. Keying a
		// process on it would put it in the very same slot as a process spawned
		// for a repository that does not use MVCC, so such processes must not be
		// cached even though the RPC is a read-only one.
		//
		// As above, no object is read before cancelling: an unconsumed read
		// leaves the queue dirty, which suppresses caching on its own and would
		// make this assertion pass regardless of the gating.
		_, cancel, err := cache.ObjectReader(ctxWithManifest(""), repoExecutor)
		require.NoError(t, err)

		cancel()
		require.Empty(t, keys(t, &cache.objectReaders))
	})

	t.Run("empty manifest pointer does not check out a non-MVCC process", func(t *testing.T) {
		defer cache.Evict()

		// Seed the cache with a process spawned for the same repository without
		// MVCC. Its key carries no manifest, which is exactly the key an empty
		// manifest pointer would compute.
		_, cancel, err := cache.ObjectReader(ctx, repoExecutor)
		require.NoError(t, err)
		cancel()

		require.Equal(t, []key{expectedKey("")}, keys(t, &cache.objectReaders))

		// The cached process observes the repository directly rather than through
		// a manifest, so an MVCC RPC must not be handed it. The entry staying in
		// the cache is what tells us it was not checked out.
		_, cancel, err = cache.ObjectReader(ctxWithManifest(""), repoExecutor)
		require.NoError(t, err)
		require.Equal(t, []key{expectedKey("")}, keys(t, &cache.objectReaders))

		// The uncacheable process is killed rather than returned, so the seeded
		// entry remains the only one.
		cancel()
		require.Equal(t, []key{expectedKey("")}, keys(t, &cache.objectReaders))
	})

	t.Run("repositories without MVCC are unaffected", func(t *testing.T) {
		defer cache.Evict()

		_, cancel, err := cache.ObjectReader(ctx, repoExecutor)
		require.NoError(t, err)
		cancel()

		allKeys := keys(t, &cache.objectReaders)
		require.Len(t, allKeys, 1)
		require.Empty(t, allKeys[0].mvccManifest)
	})
}

func requireProcessesValid(t *testing.T, p *processes) {
	p.entriesMutex.Lock()
	defer p.entriesMutex.Unlock()

	for _, ent := range p.entries {
		v := ent.value
		require.False(t, v.isClosed(), "values in cache should not be closed: %v %v", ent, v)
	}
}

// blockingCacheable is a fake cacheable whose close blocks until release is
// closed. It lets a test hold an eviction inside close() while probing whether
// the cache lock is held during that close.
type blockingCacheable struct {
	closeStarted chan struct{}
	release      chan struct{}
	closed       atomic.Bool
}

func (b *blockingCacheable) isClosed() bool { return b.closed.Load() }
func (b *blockingCacheable) isDirty() bool  { return false }

func (b *blockingCacheable) close() {
	if b.closeStarted != nil {
		close(b.closeStarted)
	}
	if b.release != nil {
		<-b.release
	}
	b.closed.Store(true)
}

// TestProcesses_evictDoesNotHoldLock asserts that a wedged process close during
// eviction does not hold entriesMutex, so other cache operations keep making
// progress. This is the regression test for the stalled shutdown where
// ProcessCache.Stop blocked acquiring the lock held by an in-progress close. See
// https://gitlab.com/gitlab-org/gitaly/-/issues/7462.
func TestProcesses_evictDoesNotHoldLock(t *testing.T) {
	t.Parallel()

	p := &processes{maxLen: 10}

	blocker := &blockingCacheable{
		closeStarted: make(chan struct{}),
		release:      make(chan struct{}),
	}
	p.Add(key{sessionID: "blocker"}, blocker, time.Now().Add(time.Hour), func() {})

	evictReturned := make(chan struct{})
	go func() {
		p.Evict()
		close(evictReturned)
	}()

	// Wait until Evict has detached the entry under the lock and is now blocked
	// inside close(), which runs with the lock released.
	select {
	case <-blocker.closeStarted:
	case <-time.After(10 * time.Second):
		t.Fatal("close() was never entered")
	}

	// While that close is wedged, operations that take entriesMutex must still
	// make progress. If the lock were held across close(), this goroutine would
	// block until the test timed out.
	cacheOpsDone := make(chan struct{})
	go func() {
		_ = p.EntryCount()
		p.Add(key{sessionID: "other"}, &blockingCacheable{}, time.Now().Add(time.Hour), func() {})
		close(cacheOpsDone)
	}()

	select {
	case <-cacheOpsDone:
	case <-time.After(10 * time.Second):
		t.Fatal("cache operations blocked while a process close was in progress")
	}
	require.Equal(t, 1, p.EntryCount())

	// Evict must still be blocked on the wedged close.
	select {
	case <-evictReturned:
		t.Fatal("Evict returned before the close was released")
	default:
	}

	// Releasing the close lets Evict finish.
	close(blocker.release)
	select {
	case <-evictReturned:
	case <-time.After(10 * time.Second):
		t.Fatal("Evict did not return after close was released")
	}

	require.True(t, blocker.isClosed())
}

func mustCreateCacheable(t *testing.T, cfg config.Cfg, repo storage.Repository) (cacheable, func()) {
	t.Helper()

	ctx, cancel := context.WithCancel(testhelper.Context(t))

	batch, err := newObjectReader(ctx, newRepoExecutor(t, cfg, repo), nil)
	require.NoError(t, err)

	return batch, cancel
}

func mustCreateKey(t *testing.T, sessionID string, repo storage.Repository) key {
	t.Helper()

	key, cacheable := newCacheKey(sessionID, repo)
	require.True(t, cacheable)

	return key
}

func keys(t *testing.T, p *processes) []key {
	t.Helper()

	p.entriesMutex.Lock()
	defer p.entriesMutex.Unlock()

	var result []key
	for _, ent := range p.entries {
		result = append(result, ent.key)
	}

	return result
}