Fix locking around deleteFile (fixes #64)

This commit is contained in:
Jakob Borg 2014-02-09 23:24:55 +01:00
parent fc6eabea28
commit b6814241cc
2 changed files with 21 additions and 1 deletions

View File

@ -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

View File

@ -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)
}
}