lib: Return error from db.FileSet.Snapshot (fixes #7419, ref #5907) (#7424)

This commit is contained in:
Simon Frei
2021-03-07 13:43:22 +01:00
committed by GitHub
parent c1d06d9501
commit 310fba4c12
25 changed files with 601 additions and 344 deletions
+8 -8
View File
@@ -185,7 +185,7 @@ func BenchmarkNeedHalf(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
count := 0
snap := benchS.Snapshot()
snap := snapshot(b, benchS)
snap.WithNeed(protocol.LocalDeviceID, func(fi protocol.FileIntf) bool {
count++
return true
@@ -209,7 +209,7 @@ func BenchmarkNeedHalfRemote(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
count := 0
snap := fset.Snapshot()
snap := snapshot(b, fset)
snap.WithNeed(remoteDevice0, func(fi protocol.FileIntf) bool {
count++
return true
@@ -230,7 +230,7 @@ func BenchmarkHave(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
count := 0
snap := benchS.Snapshot()
snap := snapshot(b, benchS)
snap.WithHave(protocol.LocalDeviceID, func(fi protocol.FileIntf) bool {
count++
return true
@@ -251,7 +251,7 @@ func BenchmarkGlobal(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
count := 0
snap := benchS.Snapshot()
snap := snapshot(b, benchS)
snap.WithGlobal(func(fi protocol.FileIntf) bool {
count++
return true
@@ -272,7 +272,7 @@ func BenchmarkNeedHalfTruncated(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
count := 0
snap := benchS.Snapshot()
snap := snapshot(b, benchS)
snap.WithNeedTruncated(protocol.LocalDeviceID, func(fi protocol.FileIntf) bool {
count++
return true
@@ -293,7 +293,7 @@ func BenchmarkHaveTruncated(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
count := 0
snap := benchS.Snapshot()
snap := snapshot(b, benchS)
snap.WithHaveTruncated(protocol.LocalDeviceID, func(fi protocol.FileIntf) bool {
count++
return true
@@ -314,7 +314,7 @@ func BenchmarkGlobalTruncated(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
count := 0
snap := benchS.Snapshot()
snap := snapshot(b, benchS)
snap.WithGlobalTruncated(func(fi protocol.FileIntf) bool {
count++
return true
@@ -336,7 +336,7 @@ func BenchmarkNeedCount(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
snap := benchS.Snapshot()
snap := snapshot(b, benchS)
_ = snap.NeedSize(protocol.LocalDeviceID)
snap.Release()
}
+4 -4
View File
@@ -77,7 +77,7 @@ func TestIgnoredFiles(t *testing.T) {
// Local files should have the "ignored" bit in addition to just being
// generally invalid if we want to look at the simulation of that bit.
snap := fs.Snapshot()
snap := snapshot(t, fs)
defer snap.Release()
fi, ok := snap.Get(protocol.LocalDeviceID, "foo")
if !ok {
@@ -866,7 +866,7 @@ func TestCheckLocalNeed(t *testing.T) {
fs.Update(remoteDevice0, files)
checkNeed := func() {
snap := fs.Snapshot()
snap := snapshot(t, fs)
defer snap.Release()
c := snap.NeedSize(protocol.LocalDeviceID)
if c.Files != 2 {
@@ -974,7 +974,7 @@ func TestNeedAfterDropGlobal(t *testing.T) {
fs.Update(remoteDevice1, files[1:])
// remoteDevice1 needs one file: test
snap := fs.Snapshot()
snap := snapshot(t, fs)
c := snap.NeedSize(remoteDevice1)
if c.Files != 1 {
t.Errorf("Expected 1 needed files initially, got %v", c.Files)
@@ -986,7 +986,7 @@ func TestNeedAfterDropGlobal(t *testing.T) {
fs.Drop(remoteDevice0)
// remoteDevice1 still needs test.
snap = fs.Snapshot()
snap = snapshot(t, fs)
c = snap.NeedSize(remoteDevice1)
if c.Files != 1 {
t.Errorf("Expected still 1 needed files, got %v", c.Files)
+3 -3
View File
@@ -117,7 +117,7 @@ func TestRecalcMeta(t *testing.T) {
s1.Update(protocol.LocalDeviceID, files)
// Verify local/global size
snap := s1.Snapshot()
snap := snapshot(t, s1)
ls := snap.LocalSize()
gs := snap.GlobalSize()
snap.Release()
@@ -149,7 +149,7 @@ func TestRecalcMeta(t *testing.T) {
}
// Verify that our bad data "took"
snap = s1.Snapshot()
snap = snapshot(t, s1)
ls = snap.LocalSize()
gs = snap.GlobalSize()
snap.Release()
@@ -164,7 +164,7 @@ func TestRecalcMeta(t *testing.T) {
s2 := newFileSet(t, "test", fs.NewFilesystem(fs.FilesystemTypeFake, "fake"), ldb)
// Verify local/global size
snap = s2.Snapshot()
snap = snapshot(t, s2)
ls = snap.LocalSize()
gs = snap.GlobalSize()
snap.Release()
+4 -3
View File
@@ -151,12 +151,13 @@ type Snapshot struct {
fatalError func(error, string)
}
func (s *FileSet) Snapshot() *Snapshot {
func (s *FileSet) Snapshot() (*Snapshot, error) {
opStr := fmt.Sprintf("%s Snapshot()", s.folder)
l.Debugf(opStr)
t, err := s.db.newReadOnlyTransaction()
if err != nil {
fatalError(err, opStr, s.db)
s.db.handleFailure(err)
return nil, err
}
return &Snapshot{
folder: s.folder,
@@ -165,7 +166,7 @@ func (s *FileSet) Snapshot() *Snapshot {
fatalError: func(err error, opStr string) {
fatalError(err, opStr, s.db)
},
}
}, nil
}
func (s *Snapshot) Release() {
+117 -108
View File
@@ -45,9 +45,9 @@ func genBlocks(n int) []protocol.BlockInfo {
return b
}
func globalList(s *db.FileSet) []protocol.FileInfo {
func globalList(t testing.TB, s *db.FileSet) []protocol.FileInfo {
var fs []protocol.FileInfo
snap := s.Snapshot()
snap := snapshot(t, s)
defer snap.Release()
snap.WithGlobal(func(fi protocol.FileIntf) bool {
f := fi.(protocol.FileInfo)
@@ -56,9 +56,9 @@ func globalList(s *db.FileSet) []protocol.FileInfo {
})
return fs
}
func globalListPrefixed(s *db.FileSet, prefix string) []db.FileInfoTruncated {
func globalListPrefixed(t testing.TB, s *db.FileSet, prefix string) []db.FileInfoTruncated {
var fs []db.FileInfoTruncated
snap := s.Snapshot()
snap := snapshot(t, s)
defer snap.Release()
snap.WithPrefixedGlobalTruncated(prefix, func(fi protocol.FileIntf) bool {
f := fi.(db.FileInfoTruncated)
@@ -68,9 +68,9 @@ func globalListPrefixed(s *db.FileSet, prefix string) []db.FileInfoTruncated {
return fs
}
func haveList(s *db.FileSet, n protocol.DeviceID) []protocol.FileInfo {
func haveList(t testing.TB, s *db.FileSet, n protocol.DeviceID) []protocol.FileInfo {
var fs []protocol.FileInfo
snap := s.Snapshot()
snap := snapshot(t, s)
defer snap.Release()
snap.WithHave(n, func(fi protocol.FileIntf) bool {
f := fi.(protocol.FileInfo)
@@ -80,9 +80,9 @@ func haveList(s *db.FileSet, n protocol.DeviceID) []protocol.FileInfo {
return fs
}
func haveListPrefixed(s *db.FileSet, n protocol.DeviceID, prefix string) []db.FileInfoTruncated {
func haveListPrefixed(t testing.TB, s *db.FileSet, n protocol.DeviceID, prefix string) []db.FileInfoTruncated {
var fs []db.FileInfoTruncated
snap := s.Snapshot()
snap := snapshot(t, s)
defer snap.Release()
snap.WithPrefixedHaveTruncated(n, prefix, func(fi protocol.FileIntf) bool {
f := fi.(db.FileInfoTruncated)
@@ -92,9 +92,9 @@ func haveListPrefixed(s *db.FileSet, n protocol.DeviceID, prefix string) []db.Fi
return fs
}
func needList(s *db.FileSet, n protocol.DeviceID) []protocol.FileInfo {
func needList(t testing.TB, s *db.FileSet, n protocol.DeviceID) []protocol.FileInfo {
var fs []protocol.FileInfo
snap := s.Snapshot()
snap := snapshot(t, s)
defer snap.Release()
snap.WithNeed(n, func(fi protocol.FileIntf) bool {
f := fi.(protocol.FileInfo)
@@ -221,7 +221,7 @@ func TestGlobalSet(t *testing.T) {
check := func() {
t.Helper()
g := fileList(globalList(m))
g := fileList(globalList(t, m))
sort.Sort(g)
if fmt.Sprint(g) != fmt.Sprint(expectedGlobal) {
@@ -244,7 +244,7 @@ func TestGlobalSet(t *testing.T) {
}
globalBytes += f.FileSize()
}
gs := globalSize(m)
gs := globalSize(t, m)
if gs.Files != globalFiles {
t.Errorf("Incorrect GlobalSize files; %d != %d", gs.Files, globalFiles)
}
@@ -258,7 +258,7 @@ func TestGlobalSet(t *testing.T) {
t.Errorf("Incorrect GlobalSize bytes; %d != %d", gs.Bytes, globalBytes)
}
h := fileList(haveList(m, protocol.LocalDeviceID))
h := fileList(haveList(t, m, protocol.LocalDeviceID))
sort.Sort(h)
if fmt.Sprint(h) != fmt.Sprint(localTot) {
@@ -281,7 +281,7 @@ func TestGlobalSet(t *testing.T) {
}
haveBytes += f.FileSize()
}
ls := localSize(m)
ls := localSize(t, m)
if ls.Files != haveFiles {
t.Errorf("Incorrect LocalSize files; %d != %d", ls.Files, haveFiles)
}
@@ -295,14 +295,14 @@ func TestGlobalSet(t *testing.T) {
t.Errorf("Incorrect LocalSize bytes; %d != %d", ls.Bytes, haveBytes)
}
h = fileList(haveList(m, remoteDevice0))
h = fileList(haveList(t, m, remoteDevice0))
sort.Sort(h)
if fmt.Sprint(h) != fmt.Sprint(remoteTot) {
t.Errorf("Have incorrect (remote);\n A: %v !=\n E: %v", h, remoteTot)
}
n := fileList(needList(m, protocol.LocalDeviceID))
n := fileList(needList(t, m, protocol.LocalDeviceID))
sort.Sort(n)
if fmt.Sprint(n) != fmt.Sprint(expectedLocalNeed) {
@@ -311,7 +311,7 @@ func TestGlobalSet(t *testing.T) {
checkNeed(t, m, protocol.LocalDeviceID, expectedLocalNeed)
n = fileList(needList(m, remoteDevice0))
n = fileList(needList(t, m, remoteDevice0))
sort.Sort(n)
if fmt.Sprint(n) != fmt.Sprint(expectedRemoteNeed) {
@@ -320,7 +320,7 @@ func TestGlobalSet(t *testing.T) {
checkNeed(t, m, remoteDevice0, expectedRemoteNeed)
snap := m.Snapshot()
snap := snapshot(t, m)
defer snap.Release()
f, ok := snap.Get(protocol.LocalDeviceID, "b")
if !ok {
@@ -365,7 +365,7 @@ func TestGlobalSet(t *testing.T) {
check()
snap := m.Snapshot()
snap := snapshot(t, m)
av := []protocol.DeviceID{protocol.LocalDeviceID, remoteDevice0}
a := snap.Availability("a")
@@ -431,14 +431,14 @@ func TestGlobalSet(t *testing.T) {
check()
h := fileList(haveList(m, remoteDevice1))
h := fileList(haveList(t, m, remoteDevice1))
sort.Sort(h)
if fmt.Sprint(h) != fmt.Sprint(secRemote) {
t.Errorf("Have incorrect (secRemote);\n A: %v !=\n E: %v", h, secRemote)
}
n := fileList(needList(m, remoteDevice1))
n := fileList(needList(t, m, remoteDevice1))
sort.Sort(n)
if fmt.Sprint(n) != fmt.Sprint(expectedSecRemoteNeed) {
@@ -478,7 +478,7 @@ func TestNeedWithInvalid(t *testing.T) {
replace(s, remoteDevice0, remote0Have)
replace(s, remoteDevice1, remote1Have)
need := fileList(needList(s, protocol.LocalDeviceID))
need := fileList(needList(t, s, protocol.LocalDeviceID))
sort.Sort(need)
if fmt.Sprint(need) != fmt.Sprint(expectedNeed) {
@@ -506,7 +506,7 @@ func TestUpdateToInvalid(t *testing.T) {
replace(s, protocol.LocalDeviceID, localHave)
have := fileList(haveList(s, protocol.LocalDeviceID))
have := fileList(haveList(t, s, protocol.LocalDeviceID))
sort.Sort(have)
if fmt.Sprint(have) != fmt.Sprint(localHave) {
@@ -523,7 +523,7 @@ func TestUpdateToInvalid(t *testing.T) {
s.Update(protocol.LocalDeviceID, append(fileList{}, localHave[1], localHave[4]))
have = fileList(haveList(s, protocol.LocalDeviceID))
have = fileList(haveList(t, s, protocol.LocalDeviceID))
sort.Sort(have)
if fmt.Sprint(have) != fmt.Sprint(localHave) {
@@ -567,7 +567,7 @@ func TestInvalidAvailability(t *testing.T) {
replace(s, remoteDevice0, remote0Have)
replace(s, remoteDevice1, remote1Have)
snap := s.Snapshot()
snap := snapshot(t, s)
defer snap.Release()
if av := snap.Availability("both"); len(av) != 2 {
@@ -608,7 +608,7 @@ func TestGlobalReset(t *testing.T) {
}
replace(m, protocol.LocalDeviceID, local)
g := globalList(m)
g := globalList(t, m)
sort.Sort(fileList(g))
if diff, equal := messagediff.PrettyDiff(local, g); !equal {
@@ -618,7 +618,7 @@ func TestGlobalReset(t *testing.T) {
replace(m, remoteDevice0, remote)
replace(m, remoteDevice0, nil)
g = globalList(m)
g = globalList(t, m)
sort.Sort(fileList(g))
if diff, equal := messagediff.PrettyDiff(local, g); !equal {
@@ -655,7 +655,7 @@ func TestNeed(t *testing.T) {
replace(m, protocol.LocalDeviceID, local)
replace(m, remoteDevice0, remote)
need := needList(m, protocol.LocalDeviceID)
need := needList(t, m, protocol.LocalDeviceID)
sort.Sort(fileList(need))
sort.Sort(fileList(shouldNeed))
@@ -725,10 +725,10 @@ func TestListDropFolder(t *testing.T) {
if diff, equal := messagediff.PrettyDiff(expectedFolderList, actualFolderList); !equal {
t.Fatalf("FolderList mismatch. Diff:\n%s", diff)
}
if l := len(globalList(s0)); l != 3 {
if l := len(globalList(t, s0)); l != 3 {
t.Errorf("Incorrect global length %d != 3 for s0", l)
}
if l := len(globalList(s1)); l != 3 {
if l := len(globalList(t, s1)); l != 3 {
t.Errorf("Incorrect global length %d != 3 for s1", l)
}
@@ -741,10 +741,10 @@ func TestListDropFolder(t *testing.T) {
if diff, equal := messagediff.PrettyDiff(expectedFolderList, actualFolderList); !equal {
t.Fatalf("FolderList mismatch. Diff:\n%s", diff)
}
if l := len(globalList(s0)); l != 3 {
if l := len(globalList(t, s0)); l != 3 {
t.Errorf("Incorrect global length %d != 3 for s0", l)
}
if l := len(globalList(s1)); l != 0 {
if l := len(globalList(t, s1)); l != 0 {
t.Errorf("Incorrect global length %d != 0 for s1", l)
}
}
@@ -780,13 +780,13 @@ func TestGlobalNeedWithInvalid(t *testing.T) {
protocol.FileInfo{Name: "d", Version: protocol.Vector{Counters: []protocol.Counter{{ID: remoteDevice0.Short(), Value: 1002}}}},
}
need := fileList(needList(s, protocol.LocalDeviceID))
need := fileList(needList(t, s, protocol.LocalDeviceID))
if fmt.Sprint(need) != fmt.Sprint(total) {
t.Errorf("Need incorrect;\n A: %v !=\n E: %v", need, total)
}
checkNeed(t, s, protocol.LocalDeviceID, total)
global := fileList(globalList(s))
global := fileList(globalList(t, s))
if fmt.Sprint(global) != fmt.Sprint(total) {
t.Errorf("Global incorrect;\n A: %v !=\n E: %v", global, total)
}
@@ -810,7 +810,7 @@ func TestLongPath(t *testing.T) {
replace(s, protocol.LocalDeviceID, local)
gf := globalList(s)
gf := globalList(t, s)
if l := len(gf); l != 1 {
t.Fatalf("Incorrect len %d != 1 for global list", l)
}
@@ -911,17 +911,17 @@ func TestDropFiles(t *testing.T) {
// Check that they're there
h := haveList(m, protocol.LocalDeviceID)
h := haveList(t, m, protocol.LocalDeviceID)
if len(h) != len(local0) {
t.Errorf("Incorrect number of files after update, %d != %d", len(h), len(local0))
}
h = haveList(m, remoteDevice0)
h = haveList(t, m, remoteDevice0)
if len(h) != len(remote0) {
t.Errorf("Incorrect number of files after update, %d != %d", len(h), len(local0))
}
g := globalList(m)
g := globalList(t, m)
if len(g) != len(local0) {
// local0 covers all files
t.Errorf("Incorrect global files after update, %d != %d", len(g), len(local0))
@@ -931,17 +931,17 @@ func TestDropFiles(t *testing.T) {
m.Drop(protocol.LocalDeviceID)
h = haveList(m, protocol.LocalDeviceID)
h = haveList(t, m, protocol.LocalDeviceID)
if len(h) != 0 {
t.Errorf("Incorrect number of files after drop, %d != %d", len(h), 0)
}
h = haveList(m, remoteDevice0)
h = haveList(t, m, remoteDevice0)
if len(h) != len(remote0) {
t.Errorf("Incorrect number of files after update, %d != %d", len(h), len(local0))
}
g = globalList(m)
g = globalList(t, m)
if len(g) != len(remote0) {
// the ones in remote0 remain
t.Errorf("Incorrect global files after update, %d != %d", len(g), len(remote0))
@@ -961,20 +961,20 @@ func TestIssue4701(t *testing.T) {
s.Update(protocol.LocalDeviceID, localHave)
if c := localSize(s); c.Files != 1 {
if c := localSize(t, s); c.Files != 1 {
t.Errorf("Expected 1 local file, got %v", c.Files)
}
if c := globalSize(s); c.Files != 1 {
if c := globalSize(t, s); c.Files != 1 {
t.Errorf("Expected 1 global file, got %v", c.Files)
}
localHave[1].LocalFlags = 0
s.Update(protocol.LocalDeviceID, localHave)
if c := localSize(s); c.Files != 2 {
if c := localSize(t, s); c.Files != 2 {
t.Errorf("Expected 2 local files, got %v", c.Files)
}
if c := globalSize(s); c.Files != 2 {
if c := globalSize(t, s); c.Files != 2 {
t.Errorf("Expected 2 global files, got %v", c.Files)
}
@@ -982,10 +982,10 @@ func TestIssue4701(t *testing.T) {
localHave[1].LocalFlags = protocol.FlagLocalIgnored
s.Update(protocol.LocalDeviceID, localHave)
if c := localSize(s); c.Files != 0 {
if c := localSize(t, s); c.Files != 0 {
t.Errorf("Expected 0 local files, got %v", c.Files)
}
if c := globalSize(s); c.Files != 0 {
if c := globalSize(t, s); c.Files != 0 {
t.Errorf("Expected 0 global files, got %v", c.Files)
}
}
@@ -1009,7 +1009,7 @@ func TestWithHaveSequence(t *testing.T) {
replace(s, protocol.LocalDeviceID, localHave)
i := 2
snap := s.Snapshot()
snap := snapshot(t, s)
defer snap.Release()
snap.WithHaveSequence(int64(i), func(fi protocol.FileIntf) bool {
if f := fi.(protocol.FileInfo); !f.IsEquivalent(localHave[i-1], 0) {
@@ -1061,7 +1061,7 @@ loop:
break loop
default:
}
snap := s.Snapshot()
snap := snapshot(t, s)
snap.WithHaveSequence(prevSeq+1, func(fi protocol.FileIntf) bool {
if fi.SequenceNo() < prevSeq+1 {
t.Fatal("Skipped ", prevSeq+1, fi.SequenceNo())
@@ -1089,11 +1089,11 @@ func TestIssue4925(t *testing.T) {
replace(s, protocol.LocalDeviceID, localHave)
for _, prefix := range []string{"dir", "dir/"} {
pl := haveListPrefixed(s, protocol.LocalDeviceID, prefix)
pl := haveListPrefixed(t, s, protocol.LocalDeviceID, prefix)
if l := len(pl); l != 2 {
t.Errorf("Expected 2, got %v local items below %v", l, prefix)
}
pl = globalListPrefixed(s, prefix)
pl = globalListPrefixed(t, s, prefix)
if l := len(pl); l != 2 {
t.Errorf("Expected 2, got %v global items below %v", l, prefix)
}
@@ -1114,24 +1114,24 @@ func TestMoveGlobalBack(t *testing.T) {
s.Update(protocol.LocalDeviceID, localHave)
s.Update(remoteDevice0, remote0Have)
if need := needList(s, protocol.LocalDeviceID); len(need) != 1 {
if need := needList(t, s, protocol.LocalDeviceID); len(need) != 1 {
t.Error("Expected 1 local need, got", need)
} else if !need[0].IsEquivalent(remote0Have[0], 0) {
t.Errorf("Local need incorrect;\n A: %v !=\n E: %v", need[0], remote0Have[0])
}
checkNeed(t, s, protocol.LocalDeviceID, remote0Have[:1])
if need := needList(s, remoteDevice0); len(need) != 0 {
if need := needList(t, s, remoteDevice0); len(need) != 0 {
t.Error("Expected no need for remote 0, got", need)
}
checkNeed(t, s, remoteDevice0, nil)
ls := localSize(s)
ls := localSize(t, s)
if haveBytes := localHave[0].Size; ls.Bytes != haveBytes {
t.Errorf("Incorrect LocalSize bytes; %d != %d", ls.Bytes, haveBytes)
}
gs := globalSize(s)
gs := globalSize(t, s)
if globalBytes := remote0Have[0].Size; gs.Bytes != globalBytes {
t.Errorf("Incorrect GlobalSize bytes; %d != %d", gs.Bytes, globalBytes)
}
@@ -1142,24 +1142,24 @@ func TestMoveGlobalBack(t *testing.T) {
remote0Have[0].Version = remote0Have[0].Version.Update(remoteDevice0.Short()).DropOthers(remoteDevice0.Short())
s.Update(remoteDevice0, remote0Have)
if need := needList(s, remoteDevice0); len(need) != 1 {
if need := needList(t, s, remoteDevice0); len(need) != 1 {
t.Error("Expected 1 need for remote 0, got", need)
} else if !need[0].IsEquivalent(localHave[0], 0) {
t.Errorf("Need for remote 0 incorrect;\n A: %v !=\n E: %v", need[0], localHave[0])
}
checkNeed(t, s, remoteDevice0, localHave[:1])
if need := needList(s, protocol.LocalDeviceID); len(need) != 0 {
if need := needList(t, s, protocol.LocalDeviceID); len(need) != 0 {
t.Error("Expected no local need, got", need)
}
checkNeed(t, s, protocol.LocalDeviceID, nil)
ls = localSize(s)
ls = localSize(t, s)
if haveBytes := localHave[0].Size; ls.Bytes != haveBytes {
t.Errorf("Incorrect LocalSize bytes; %d != %d", ls.Bytes, haveBytes)
}
gs = globalSize(s)
gs = globalSize(t, s)
if globalBytes := localHave[0].Size; gs.Bytes != globalBytes {
t.Errorf("Incorrect GlobalSize bytes; %d != %d", gs.Bytes, globalBytes)
}
@@ -1181,7 +1181,7 @@ func TestIssue5007(t *testing.T) {
s.Update(remoteDevice0, fs)
if need := needList(s, protocol.LocalDeviceID); len(need) != 1 {
if need := needList(t, s, protocol.LocalDeviceID); len(need) != 1 {
t.Fatal("Expected 1 local need, got", need)
} else if !need[0].IsEquivalent(fs[0], 0) {
t.Fatalf("Local need incorrect;\n A: %v !=\n E: %v", need[0], fs[0])
@@ -1191,7 +1191,7 @@ func TestIssue5007(t *testing.T) {
fs[0].LocalFlags = protocol.FlagLocalIgnored
s.Update(protocol.LocalDeviceID, fs)
if need := needList(s, protocol.LocalDeviceID); len(need) != 0 {
if need := needList(t, s, protocol.LocalDeviceID); len(need) != 0 {
t.Fatal("Expected no local need, got", need)
}
checkNeed(t, s, protocol.LocalDeviceID, nil)
@@ -1211,7 +1211,7 @@ func TestNeedDeleted(t *testing.T) {
s.Update(remoteDevice0, fs)
if need := needList(s, protocol.LocalDeviceID); len(need) != 0 {
if need := needList(t, s, protocol.LocalDeviceID); len(need) != 0 {
t.Fatal("Expected no local need, got", need)
}
checkNeed(t, s, protocol.LocalDeviceID, nil)
@@ -1220,7 +1220,7 @@ func TestNeedDeleted(t *testing.T) {
fs[0].Version = fs[0].Version.Update(remoteDevice0.Short())
s.Update(remoteDevice0, fs)
if need := needList(s, protocol.LocalDeviceID); len(need) != 1 {
if need := needList(t, s, protocol.LocalDeviceID); len(need) != 1 {
t.Fatal("Expected 1 local need, got", need)
} else if !need[0].IsEquivalent(fs[0], 0) {
t.Fatalf("Local need incorrect;\n A: %v !=\n E: %v", need[0], fs[0])
@@ -1231,7 +1231,7 @@ func TestNeedDeleted(t *testing.T) {
fs[0].Version = fs[0].Version.Update(remoteDevice0.Short())
s.Update(remoteDevice0, fs)
if need := needList(s, protocol.LocalDeviceID); len(need) != 0 {
if need := needList(t, s, protocol.LocalDeviceID); len(need) != 0 {
t.Fatal("Expected no local need, got", need)
}
checkNeed(t, s, protocol.LocalDeviceID, nil)
@@ -1261,22 +1261,22 @@ func TestReceiveOnlyAccounting(t *testing.T) {
replace(s, protocol.LocalDeviceID, files)
replace(s, remote, files)
if n := localSize(s).Files; n != 3 {
if n := localSize(t, s).Files; n != 3 {
t.Fatal("expected 3 local files initially, not", n)
}
if n := localSize(s).Bytes; n != 30 {
if n := localSize(t, s).Bytes; n != 30 {
t.Fatal("expected 30 local bytes initially, not", n)
}
if n := globalSize(s).Files; n != 3 {
if n := globalSize(t, s).Files; n != 3 {
t.Fatal("expected 3 global files initially, not", n)
}
if n := globalSize(s).Bytes; n != 30 {
if n := globalSize(t, s).Bytes; n != 30 {
t.Fatal("expected 30 global bytes initially, not", n)
}
if n := receiveOnlyChangedSize(s).Files; n != 0 {
if n := receiveOnlyChangedSize(t, s).Files; n != 0 {
t.Fatal("expected 0 receive only changed files initially, not", n)
}
if n := receiveOnlyChangedSize(s).Bytes; n != 0 {
if n := receiveOnlyChangedSize(t, s).Bytes; n != 0 {
t.Fatal("expected 0 receive only changed bytes initially, not", n)
}
@@ -1291,22 +1291,22 @@ func TestReceiveOnlyAccounting(t *testing.T) {
// Check that we see the files
if n := localSize(s).Files; n != 3 {
if n := localSize(t, s).Files; n != 3 {
t.Fatal("expected 3 local files after local change, not", n)
}
if n := localSize(s).Bytes; n != 120 {
if n := localSize(t, s).Bytes; n != 120 {
t.Fatal("expected 120 local bytes after local change, not", n)
}
if n := globalSize(s).Files; n != 3 {
if n := globalSize(t, s).Files; n != 3 {
t.Fatal("expected 3 global files after local change, not", n)
}
if n := globalSize(s).Bytes; n != 30 {
if n := globalSize(t, s).Bytes; n != 30 {
t.Fatal("expected 30 global files after local change, not", n)
}
if n := receiveOnlyChangedSize(s).Files; n != 1 {
if n := receiveOnlyChangedSize(t, s).Files; n != 1 {
t.Fatal("expected 1 receive only changed file after local change, not", n)
}
if n := receiveOnlyChangedSize(s).Bytes; n != 100 {
if n := receiveOnlyChangedSize(t, s).Bytes; n != 100 {
t.Fatal("expected 100 receive only changed btyes after local change, not", n)
}
@@ -1322,22 +1322,22 @@ func TestReceiveOnlyAccounting(t *testing.T) {
// Check that we see the files, same data as initially
if n := localSize(s).Files; n != 3 {
if n := localSize(t, s).Files; n != 3 {
t.Fatal("expected 3 local files after revert, not", n)
}
if n := localSize(s).Bytes; n != 30 {
if n := localSize(t, s).Bytes; n != 30 {
t.Fatal("expected 30 local bytes after revert, not", n)
}
if n := globalSize(s).Files; n != 3 {
if n := globalSize(t, s).Files; n != 3 {
t.Fatal("expected 3 global files after revert, not", n)
}
if n := globalSize(s).Bytes; n != 30 {
if n := globalSize(t, s).Bytes; n != 30 {
t.Fatal("expected 30 global bytes after revert, not", n)
}
if n := receiveOnlyChangedSize(s).Files; n != 0 {
if n := receiveOnlyChangedSize(t, s).Files; n != 0 {
t.Fatal("expected 0 receive only changed files after revert, not", n)
}
if n := receiveOnlyChangedSize(s).Bytes; n != 0 {
if n := receiveOnlyChangedSize(t, s).Bytes; n != 0 {
t.Fatal("expected 0 receive only changed bytes after revert, not", n)
}
}
@@ -1366,7 +1366,7 @@ func TestNeedAfterUnignore(t *testing.T) {
local.ModifiedS = 0
s.Update(protocol.LocalDeviceID, fileList{local})
if need := needList(s, protocol.LocalDeviceID); len(need) != 1 {
if need := needList(t, s, protocol.LocalDeviceID); len(need) != 1 {
t.Fatal("Expected one local need, got", need)
} else if !need[0].IsEquivalent(remote, 0) {
t.Fatalf("Got %v, expected %v", need[0], remote)
@@ -1387,7 +1387,7 @@ func TestRemoteInvalidNotAccounted(t *testing.T) {
}
s.Update(remoteDevice0, files)
global := globalSize(s)
global := globalSize(t, s)
if global.Files != 1 {
t.Error("Expected one file in global size, not", global.Files)
}
@@ -1411,7 +1411,7 @@ func TestNeedWithNewerInvalid(t *testing.T) {
s.Update(remoteDevice0, fileList{file})
s.Update(remoteDevice1, fileList{file})
need := needList(s, protocol.LocalDeviceID)
need := needList(t, s, protocol.LocalDeviceID)
if len(need) != 1 {
t.Fatal("Locally missing file should be needed")
}
@@ -1427,7 +1427,7 @@ func TestNeedWithNewerInvalid(t *testing.T) {
s.Update(remoteDevice1, fileList{inv})
// We still have an old file, we need the newest valid file
need = needList(s, protocol.LocalDeviceID)
need = needList(t, s, protocol.LocalDeviceID)
if len(need) != 1 {
t.Fatal("Locally missing file should be needed regardless of invalid files")
}
@@ -1452,13 +1452,13 @@ func TestNeedAfterDeviceRemove(t *testing.T) {
s.Update(remoteDevice0, fs)
if need := needList(s, protocol.LocalDeviceID); len(need) != 1 {
if need := needList(t, s, protocol.LocalDeviceID); len(need) != 1 {
t.Fatal("Expected one local need, got", need)
}
s.Drop(remoteDevice0)
if need := needList(s, protocol.LocalDeviceID); len(need) != 0 {
if need := needList(t, s, protocol.LocalDeviceID); len(need) != 0 {
t.Fatal("Expected no local need, got", need)
}
checkNeed(t, s, protocol.LocalDeviceID, nil)
@@ -1481,7 +1481,7 @@ func TestCaseSensitive(t *testing.T) {
replace(s, protocol.LocalDeviceID, local)
gf := globalList(s)
gf := globalList(t, s)
if l := len(gf); l != len(local) {
t.Fatalf("Incorrect len %d != %d for global list", l, len(local))
}
@@ -1551,7 +1551,7 @@ func TestSequenceIndex(t *testing.T) {
// a subset of those files if we manage to run before a complete
// update has happened since our last iteration.
latest = latest[:0]
snap := s.Snapshot()
snap := snapshot(t, s)
snap.WithHaveSequence(seq+1, func(f protocol.FileIntf) bool {
seen[f.FileName()] = f
latest = append(latest, f)
@@ -1617,7 +1617,7 @@ func TestIgnoreAfterReceiveOnly(t *testing.T) {
s.Update(protocol.LocalDeviceID, fs)
snap := s.Snapshot()
snap := snapshot(t, s)
defer snap.Release()
if f, ok := snap.Get(protocol.LocalDeviceID, file); !ok {
t.Error("File missing in db")
@@ -1654,7 +1654,7 @@ func TestUpdateWithOneFileTwice(t *testing.T) {
s.Update(protocol.LocalDeviceID, fs)
snap := s.Snapshot()
snap := snapshot(t, s)
defer snap.Release()
count := 0
snap.WithHaveSequence(0, func(f protocol.FileIntf) bool {
@@ -1678,7 +1678,7 @@ func TestNeedRemoteOnly(t *testing.T) {
}
s.Update(remoteDevice0, remote0Have)
need := needSize(s, remoteDevice0)
need := needSize(t, s, remoteDevice0)
if !need.Equal(db.Counts{}) {
t.Error("Expected nothing needed, got", need)
}
@@ -1697,14 +1697,14 @@ func TestNeedRemoteAfterReset(t *testing.T) {
s.Update(protocol.LocalDeviceID, files)
s.Update(remoteDevice0, files)
need := needSize(s, remoteDevice0)
need := needSize(t, s, remoteDevice0)
if !need.Equal(db.Counts{}) {
t.Error("Expected nothing needed, got", need)
}
s.Drop(remoteDevice0)
need = needSize(s, remoteDevice0)
need = needSize(t, s, remoteDevice0)
if exp := (db.Counts{Files: 1}); !need.Equal(exp) {
t.Errorf("Expected %v, got %v", exp, need)
}
@@ -1723,10 +1723,10 @@ func TestIgnoreLocalChanged(t *testing.T) {
}
s.Update(protocol.LocalDeviceID, files)
if c := globalSize(s).Files; c != 0 {
if c := globalSize(t, s).Files; c != 0 {
t.Error("Expected no global file, got", c)
}
if c := localSize(s).Files; c != 1 {
if c := localSize(t, s).Files; c != 1 {
t.Error("Expected one local file, got", c)
}
@@ -1734,10 +1734,10 @@ func TestIgnoreLocalChanged(t *testing.T) {
files[0].LocalFlags = protocol.FlagLocalIgnored
s.Update(protocol.LocalDeviceID, files)
if c := globalSize(s).Files; c != 0 {
if c := globalSize(t, s).Files; c != 0 {
t.Error("Expected no global file, got", c)
}
if c := localSize(s).Files; c != 0 {
if c := localSize(t, s).Files; c != 0 {
t.Error("Expected no local file, got", c)
}
}
@@ -1789,26 +1789,26 @@ func replace(fs *db.FileSet, device protocol.DeviceID, files []protocol.FileInfo
fs.Update(device, files)
}
func localSize(fs *db.FileSet) db.Counts {
snap := fs.Snapshot()
func localSize(t testing.TB, fs *db.FileSet) db.Counts {
snap := snapshot(t, fs)
defer snap.Release()
return snap.LocalSize()
}
func globalSize(fs *db.FileSet) db.Counts {
snap := fs.Snapshot()
func globalSize(t testing.TB, fs *db.FileSet) db.Counts {
snap := snapshot(t, fs)
defer snap.Release()
return snap.GlobalSize()
}
func needSize(fs *db.FileSet, id protocol.DeviceID) db.Counts {
snap := fs.Snapshot()
func needSize(t testing.TB, fs *db.FileSet, id protocol.DeviceID) db.Counts {
snap := snapshot(t, fs)
defer snap.Release()
return snap.NeedSize(id)
}
func receiveOnlyChangedSize(fs *db.FileSet) db.Counts {
snap := fs.Snapshot()
func receiveOnlyChangedSize(t testing.TB, fs *db.FileSet) db.Counts {
snap := snapshot(t, fs)
defer snap.Release()
return snap.ReceiveOnlyChangedSize()
}
@@ -1833,7 +1833,7 @@ func filesToCounts(files []protocol.FileInfo) db.Counts {
func checkNeed(t testing.TB, s *db.FileSet, dev protocol.DeviceID, expected []protocol.FileInfo) {
t.Helper()
counts := needSize(s, dev)
counts := needSize(t, s, dev)
if exp := filesToCounts(expected); !exp.Equal(counts) {
t.Errorf("Count incorrect (%v): expected %v, got %v", dev, exp, counts)
}
@@ -1860,3 +1860,12 @@ func newFileSet(t testing.TB, folder string, fs fs.Filesystem, ll *db.Lowlevel)
}
return fset
}
func snapshot(t testing.TB, fset *db.FileSet) *db.Snapshot {
t.Helper()
snap, err := fset.Snapshot()
if err != nil {
t.Fatal(err)
}
return snap
}
+9
View File
@@ -92,6 +92,15 @@ func newFileSet(t testing.TB, folder string, fs fs.Filesystem, db *Lowlevel) *Fi
return fset
}
func snapshot(t testing.TB, fset *FileSet) *Snapshot {
t.Helper()
snap, err := fset.Snapshot()
if err != nil {
t.Fatal(err)
}
return snap
}
// The following commented tests were used to generate jsons files to stdout for
// future tests and are kept here for reference (reuse).