restore: Fix small memory leak in filesWriter, add tests

Signed-off-by: Igor Fedorenko <igor@ifedorenko.com>
This commit is contained in:
Igor Fedorenko 2018-09-11 23:12:19 -04:00 committed by Alexander Neumann
parent 941202c119
commit e010f3b884
2 changed files with 45 additions and 0 deletions

View file

@ -66,4 +66,5 @@ func (w *filesWriter) close(file *fileInfo) {
w.lock.Lock()
defer w.lock.Unlock()
w.writers.Remove(file)
delete(w.inprogress, file)
}

View file

@ -0,0 +1,44 @@
package restorer
import (
"io/ioutil"
"testing"
rtest "github.com/restic/restic/internal/test"
)
func TestFilesWriterBasic(t *testing.T) {
dir, cleanup := rtest.TempDir(t)
defer cleanup()
w := newFilesWriter(1)
f1 := &fileInfo{path: dir + "/f1"}
f2 := &fileInfo{path: dir + "/f2"}
rtest.OK(t, w.writeToFile(f1, []byte{1}))
rtest.Equals(t, 1, w.writers.Len())
rtest.Equals(t, 1, len(w.inprogress))
rtest.OK(t, w.writeToFile(f2, []byte{2}))
rtest.Equals(t, 1, w.writers.Len())
rtest.Equals(t, 2, len(w.inprogress))
rtest.OK(t, w.writeToFile(f1, []byte{1}))
w.close(f1)
rtest.Equals(t, 0, w.writers.Len())
rtest.Equals(t, 1, len(w.inprogress))
rtest.OK(t, w.writeToFile(f2, []byte{2}))
w.close(f2)
rtest.Equals(t, 0, w.writers.Len())
rtest.Equals(t, 0, len(w.inprogress))
buf, err := ioutil.ReadFile(f1.path)
rtest.OK(t, err)
rtest.Equals(t, []byte{1, 1}, buf)
buf, err = ioutil.ReadFile(f2.path)
rtest.OK(t, err)
rtest.Equals(t, []byte{2, 2}, buf)
}