|
| 1 | +using GVFS.FunctionalTests.FileSystemRunners; |
| 2 | +using GVFS.FunctionalTests.Should; |
| 3 | +using GVFS.FunctionalTests.Tools; |
| 4 | +using GVFS.Tests.Should; |
| 5 | +using NUnit.Framework; |
| 6 | +using System.IO; |
| 7 | + |
| 8 | +namespace GVFS.FunctionalTests.Tests.EnlistmentPerFixture |
| 9 | +{ |
| 10 | + [TestFixture] |
| 11 | + public class PrefetchCommitsOffloadTests : TestsWithEnlistmentPerFixture |
| 12 | + { |
| 13 | + private const string PrefetchPackPrefix = "prefetch"; |
| 14 | + |
| 15 | + private FileSystemRunner fileSystem; |
| 16 | + |
| 17 | + public PrefetchCommitsOffloadTests() |
| 18 | + : base(forcePerRepoObjectCache: true, skipPrefetchDuringClone: true) |
| 19 | + { |
| 20 | + this.fileSystem = new SystemIORunner(); |
| 21 | + } |
| 22 | + |
| 23 | + private string PackRoot |
| 24 | + { |
| 25 | + get |
| 26 | + { |
| 27 | + return this.Enlistment.GetPackRoot(this.fileSystem); |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + [TestCase, Order(1)] |
| 32 | + public void PrefetchCommitsMountedUsesOffload() |
| 33 | + { |
| 34 | + // With the enlistment mounted, prefetch --commits should succeed |
| 35 | + // by offloading to the mount process (using its warm auth). |
| 36 | + this.Enlistment.Prefetch("--commits"); |
| 37 | + this.PostFetchJobShouldComplete(); |
| 38 | + |
| 39 | + string[] prefetchPacks = this.ReadPrefetchPackFileNames(); |
| 40 | + prefetchPacks.Length.ShouldBeAtLeast(1, "There should be at least one prefetch pack after mounted prefetch"); |
| 41 | + this.AllPrefetchPacksShouldHaveIdx(prefetchPacks); |
| 42 | + } |
| 43 | + |
| 44 | + [TestCase, Order(2)] |
| 45 | + public void PrefetchCommitsMountedIsIdempotent() |
| 46 | + { |
| 47 | + // Running prefetch --commits again while mounted should succeed |
| 48 | + // (may be a no-op if packs are already up to date). |
| 49 | + string[] packsBefore = this.ReadPrefetchPackFileNames(); |
| 50 | + |
| 51 | + this.Enlistment.Prefetch("--commits"); |
| 52 | + this.PostFetchJobShouldComplete(); |
| 53 | + |
| 54 | + string[] packsAfter = this.ReadPrefetchPackFileNames(); |
| 55 | + packsAfter.Length.ShouldBeAtLeast(packsBefore.Length, "Pack count should not decrease after idempotent prefetch"); |
| 56 | + this.AllPrefetchPacksShouldHaveIdx(packsAfter); |
| 57 | + } |
| 58 | + |
| 59 | + [TestCase, Order(3)] |
| 60 | + public void PrefetchCommitsUnmountedFallsBackToDirectAuth() |
| 61 | + { |
| 62 | + // Unmount, then prefetch --commits should fall back to direct auth |
| 63 | + // and still succeed. |
| 64 | + this.Enlistment.UnmountGVFS(); |
| 65 | + |
| 66 | + try |
| 67 | + { |
| 68 | + this.Enlistment.Prefetch("--commits"); |
| 69 | + |
| 70 | + string[] prefetchPacks = this.ReadPrefetchPackFileNames(); |
| 71 | + prefetchPacks.Length.ShouldBeAtLeast(1, "There should be at least one prefetch pack after unmounted prefetch"); |
| 72 | + this.AllPrefetchPacksShouldHaveIdx(prefetchPacks); |
| 73 | + } |
| 74 | + finally |
| 75 | + { |
| 76 | + this.Enlistment.MountGVFS(); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + [TestCase, Order(4)] |
| 81 | + public void PrefetchCommitsMountedAfterRemount() |
| 82 | + { |
| 83 | + // After unmount + remount, prefetch --commits should work via |
| 84 | + // the mount process again. |
| 85 | + this.Enlistment.UnmountGVFS(); |
| 86 | + this.Enlistment.MountGVFS(); |
| 87 | + |
| 88 | + this.Enlistment.Prefetch("--commits"); |
| 89 | + this.PostFetchJobShouldComplete(); |
| 90 | + |
| 91 | + string[] prefetchPacks = this.ReadPrefetchPackFileNames(); |
| 92 | + prefetchPacks.Length.ShouldBeAtLeast(1, "There should be at least one prefetch pack after remount prefetch"); |
| 93 | + this.AllPrefetchPacksShouldHaveIdx(prefetchPacks); |
| 94 | + } |
| 95 | + |
| 96 | + private string[] ReadPrefetchPackFileNames() |
| 97 | + { |
| 98 | + return Directory.GetFiles(this.PackRoot, $"{PrefetchPackPrefix}*.pack"); |
| 99 | + } |
| 100 | + |
| 101 | + private void AllPrefetchPacksShouldHaveIdx(string[] prefetchPacks) |
| 102 | + { |
| 103 | + foreach (string prefetchPack in prefetchPacks) |
| 104 | + { |
| 105 | + string idxPath = Path.ChangeExtension(prefetchPack, ".idx"); |
| 106 | + idxPath.ShouldBeAFile(this.fileSystem); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + private void PostFetchJobShouldComplete() |
| 111 | + { |
| 112 | + string objectDir = this.Enlistment.GetObjectRoot(this.fileSystem); |
| 113 | + string postFetchLock = Path.Combine(objectDir, "git-maintenance-step.lock"); |
| 114 | + |
| 115 | + System.Diagnostics.Stopwatch timeout = System.Diagnostics.Stopwatch.StartNew(); |
| 116 | + while (this.fileSystem.FileExists(postFetchLock)) |
| 117 | + { |
| 118 | + timeout.Elapsed.TotalSeconds.ShouldBeAtMost(60, "Post-fetch lock file was not released within 60 seconds"); |
| 119 | + System.Threading.Thread.Sleep(500); |
| 120 | + } |
| 121 | + |
| 122 | + ProcessResult graphResult = GitProcess.InvokeProcess( |
| 123 | + this.Enlistment.RepoRoot, |
| 124 | + "commit-graph verify --shallow --object-dir=\"" + objectDir + "\""); |
| 125 | + graphResult.ExitCode.ShouldEqual(0); |
| 126 | + } |
| 127 | + } |
| 128 | +} |
0 commit comments