github.com/keybase/client/go@v0.0.0-20240309051027-028f7c731f8b/kbfs/data/prev_revisions_test.go (about) 1 // Copyright 2018 Keybase Inc. All rights reserved. 2 // Use of this source code is governed by a BSD 3 // license that can be found in the LICENSE file. 4 5 package data 6 7 import ( 8 "testing" 9 10 "github.com/keybase/client/go/kbfs/kbfsmd" 11 "github.com/stretchr/testify/require" 12 ) 13 14 func checkRevRevisions( 15 t *testing.T, pr PrevRevisions, minRev kbfsmd.Revision, 16 maxRev kbfsmd.Revision, numRevisions int) { 17 // Make sure the revisions are in descending order, and the counts 18 // are in increasing order. 19 maxMin := kbfsmd.Revision( 20 minPrevRevisionSlotCounts[len(minPrevRevisionSlotCounts)-1]) 21 shouldBeFull := maxRev-minRev >= maxMin 22 if shouldBeFull { 23 require.Len(t, pr, len(minPrevRevisionSlotCounts)) 24 } 25 require.Equal(t, uint8(1), pr[0].Count) 26 for i, r := range pr { 27 require.True(t, r.Revision > minRev) 28 require.True(t, r.Revision <= maxRev) 29 if i > 0 { 30 require.True(t, r.Revision < pr[i-1].Revision) 31 require.True(t, r.Count > pr[i-1].Count) 32 } 33 } 34 } 35 36 func TestPrevRevisions(t *testing.T) { 37 var pr PrevRevisions 38 t.Log("Add the first set of revisions") 39 var i int 40 for ; i < len(minPrevRevisionSlotCounts); i++ { 41 rev := kbfsmd.Revision((i + 1) * 2) 42 pr = pr.AddRevision(rev, 0) 43 require.Len(t, pr, i+1) 44 checkRevRevisions(t, pr, 0, rev, i+1) 45 } 46 t.Log("The next set will start replacing the prev ones") 47 for ; i < len(minPrevRevisionSlotCounts)*2; i++ { 48 rev := kbfsmd.Revision((i + 1) * 2) 49 pr = pr.AddRevision(rev, 0) 50 require.Len(t, pr, len(minPrevRevisionSlotCounts)) 51 checkRevRevisions(t, pr, 0, rev, i+1) 52 } 53 maxMin := int(minPrevRevisionSlotCounts[len(minPrevRevisionSlotCounts)-1]) 54 t.Log("Exceed the maximum min count") 55 for ; i < maxMin+1; i++ { 56 rev := kbfsmd.Revision((i + 1) * 2) 57 pr = pr.AddRevision(rev, 0) 58 require.Len(t, pr, len(minPrevRevisionSlotCounts)) 59 checkRevRevisions(t, pr, 0, rev, i+1) 60 } 61 t.Log("Exceed the maximum min count by even more") 62 for ; i < maxMin*2+1; i++ { 63 rev := kbfsmd.Revision((i + 1) * 2) 64 pr = pr.AddRevision(rev, 0) 65 require.Len(t, pr, len(minPrevRevisionSlotCounts)) 66 checkRevRevisions(t, pr, 0, rev, i+1) 67 } 68 t.Log("Garbage collect past the oldest revision") 69 gcRev := pr[len(pr)-1].Revision + 1 70 i++ 71 rev := kbfsmd.Revision((i + 1) * 2) 72 pr = pr.AddRevision(rev, gcRev) 73 require.Len(t, pr, len(minPrevRevisionSlotCounts)) 74 checkRevRevisions(t, pr, gcRev, rev, i+1) 75 t.Log("Garbage collect past the final two revisions") 76 gcRev = pr[len(pr)-2].Revision + 1 77 i++ 78 rev = kbfsmd.Revision((i + 1) * 2) 79 pr = pr.AddRevision(rev, gcRev) 80 require.Len(t, pr, len(minPrevRevisionSlotCounts)-1) 81 checkRevRevisions(t, pr, gcRev, rev, i+1) 82 t.Log("Garbage collect everything") 83 gcRev = pr[0].Revision 84 i++ 85 rev = kbfsmd.Revision((i + 1) * 2) 86 pr = pr.AddRevision(rev, gcRev) 87 require.Len(t, pr, 1) 88 checkRevRevisions(t, pr, gcRev, rev, i+1) 89 90 t.Log("Fill it up again") 91 for j := kbfsmd.Revision(1); j < 5; j++ { 92 pr = pr.AddRevision(rev+j, gcRev) 93 } 94 95 t.Log("A lower revision number wipes everything out") 96 rev-- 97 pr = pr.AddRevision(rev, gcRev) 98 require.Len(t, pr, 1) 99 }