Browse Source

Fix locking around deleteFile (fixes #64)

Jakob Borg 11 years ago
parent
commit
b6814241cc
2 changed files with 21 additions and 1 deletions
  1. 3 1
      model/filequeue.go
  2. 18 0
      model/filequeue_test.go

+ 3 - 1
model/filequeue.go

@@ -199,7 +199,7 @@ func (q *FileQueue) QueuedFiles() (files []string) {
 }
 
 func (q *FileQueue) deleteAt(i int) {
-	q.files = q.files[:i+copy(q.files[i:], q.files[i+1:])]
+	q.files = append(q.files[:i], q.files[i+1:]...)
 }
 
 func (q *FileQueue) deleteFile(n string) {
@@ -228,7 +228,9 @@ func (q *FileQueue) RemoveAvailable(toRemove string) {
 			if node == toRemove {
 				q.availability[file] = nodes[:i+copy(nodes[i:], nodes[i+1:])]
 				if len(q.availability[file]) == 0 {
+					q.fmut.Lock()
 					q.deleteFile(file)
+					q.fmut.Unlock()
 				}
 			}
 			break

+ 18 - 0
model/filequeue_test.go

@@ -275,3 +275,21 @@ func TestFileQueueThreadHandling(t *testing.T) {
 		t.Error("Total mismatch; %d != %d", gotTot, total)
 	}
 }
+
+func TestDeleteAt(t *testing.T) {
+	q := FileQueue{}
+
+	for i := 0; i < 4; i++ {
+		q.files = queuedFileList{{name: "a"}, {name: "b"}, {name: "c"}, {name: "d"}}
+		q.deleteAt(i)
+		if l := len(q.files); l != 3 {
+			t.Fatal("deleteAt(%d) failed; %d != 3", i, l)
+		}
+	}
+
+	q.files = queuedFileList{{name: "a"}}
+	q.deleteAt(0)
+	if l := len(q.files); l != 0 {
+		t.Fatal("deleteAt(only) failed; %d != 0", l)
+	}
+}