package vault

import (
	"context"
	"testing"
	"time"

	"github.com/stretchr/testify/require"
)

func TestMemoryPathLockHonorsContextWhileAnotherOperationOwnsPath(t *testing.T) {
	path := t.TempDir()
	unlock, err := lockMemoryPath(t.Context(), path)
	require.NoError(t, err)
	defer unlock()

	ctx, cancel := context.WithCancel(t.Context())
	cancel()
	started := time.Now()
	_, err = lockMemoryPath(ctx, path)
	require.ErrorIs(t, err, context.Canceled)
	require.Less(t, time.Since(started), 100*time.Millisecond,
		"a canceled Git operation must not wait behind the current path owner")
}
