1
0
Эх сурвалжийг харах

Add test for LocalSize/GlobalSize results

Jakob Borg 10 жил өмнө
parent
commit
1eca4170f7
2 өөрчлөгдсөн 61 нэмэгдсэн , 6 устгасан
  1. 15 6
      lib/db/leveldb.go
  2. 46 0
      lib/db/set_test.go

+ 15 - 6
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)
 			}
 		}

+ 46 - 0
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)