From 1eca4170f70afdf96b9878ea4ad64bc148b9c90f Mon Sep 17 00:00:00 2001 From: Jakob Borg Date: Wed, 21 Oct 2015 13:59:07 +0200 Subject: [PATCH] Add test for LocalSize/GlobalSize results --- lib/db/leveldb.go | 21 +++++++++++++++------ lib/db/set_test.go | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 6 deletions(-) diff --git a/lib/db/leveldb.go b/lib/db/leveldb.go index 20a2e3e0..151f3ff3 100644 --- a/lib/db/leveldb.go +++ b/lib/db/leveldb.go @@ -427,18 +427,19 @@ func ldbUpdateGlobal(db dbReader, batch dbWriter, folder, device []byte, file pr panic(err) } - if len(fl.versions) > 0 { - // Keep the current neweset file around so we can subtract it from - // the globalSize if we replace it. - oldFile, hasOldFile = ldbGet(db, folder, fl.versions[0].device, name) - } - for i := range fl.versions { if bytes.Compare(fl.versions[i].device, device) == 0 { if fl.versions[i].version.Equal(file.Version) { // No need to do anything return false } + + if i == 0 { + // Keep the current newest file around so we can subtract it from + // the globalSize if we replace it. + oldFile, hasOldFile = ldbGet(db, folder, fl.versions[0].device, name) + } + fl.versions = append(fl.versions[:i], fl.versions[i+1:]...) break } @@ -492,6 +493,14 @@ done: if !file.Version.Equal(oldFile.Version) { globalSize.addFile(file) if hasOldFile { + // We have the old file that was removed at the head of the list. + globalSize.removeFile(oldFile) + } else if len(fl.versions) > 1 { + // The previous newest version is now at index 1, grab it from there. + oldFile, ok := ldbGet(db, folder, fl.versions[1].device, name) + if !ok { + panic("file referenced in version list does not exist") + } globalSize.removeFile(oldFile) } } diff --git a/lib/db/set_test.go b/lib/db/set_test.go index abe355c4..64e12603 100644 --- a/lib/db/set_test.go +++ b/lib/db/set_test.go @@ -173,6 +173,29 @@ func TestGlobalSet(t *testing.T) { t.Errorf("Global incorrect;\n A: %v !=\n E: %v", g, expectedGlobal) } + globalFiles, globalDeleted, globalBytes := 0, 0, int64(0) + for _, f := range g { + if f.IsInvalid() { + continue + } + if f.IsDeleted() { + globalDeleted++ + } else { + globalFiles++ + } + globalBytes += f.Size() + } + gsFiles, gsDeleted, gsBytes := m.GlobalSize() + if gsFiles != globalFiles { + t.Errorf("Incorrect GlobalSize files; %d != %d", gsFiles, globalFiles) + } + if gsDeleted != globalDeleted { + t.Errorf("Incorrect GlobalSize deleted; %d != %d", gsDeleted, globalDeleted) + } + if gsBytes != globalBytes { + t.Errorf("Incorrect GlobalSize bytes; %d != %d", gsBytes, globalBytes) + } + h := fileList(haveList(m, protocol.LocalDeviceID)) sort.Sort(h) @@ -180,6 +203,29 @@ func TestGlobalSet(t *testing.T) { t.Errorf("Have incorrect;\n A: %v !=\n E: %v", h, localTot) } + haveFiles, haveDeleted, haveBytes := 0, 0, int64(0) + for _, f := range h { + if f.IsInvalid() { + continue + } + if f.IsDeleted() { + haveDeleted++ + } else { + haveFiles++ + } + haveBytes += f.Size() + } + lsFiles, lsDeleted, lsBytes := m.LocalSize() + if lsFiles != haveFiles { + t.Errorf("Incorrect LocalSize files; %d != %d", lsFiles, haveFiles) + } + if lsDeleted != haveDeleted { + t.Errorf("Incorrect LocalSize deleted; %d != %d", lsDeleted, haveDeleted) + } + if lsBytes != haveBytes { + t.Errorf("Incorrect LocalSize bytes; %d != %d", lsBytes, haveBytes) + } + h = fileList(haveList(m, remoteDevice0)) sort.Sort(h)