I'm working through linter complaints, these are some fixes. Broad categories: 1) Ignore errors where we can ignore errors: add "_ = ..." construct. you can argue that this is annoying noise, but apart from silencing the linter it *does* serve the purpose of highlighting that an error is being ignored. I think this is OK, because the linter highlighted some error cases I wasn't aware of (starting CPU profiles, for example). 2) Untyped constants where we though we had set the type. 3) A real bug where we ineffectually assigned to a shadowed err. 4) Some dead code removed. There'll be more of these, because not all packages are fixed, but the diff was already large enough.
89 lines
1.9 KiB
Go
89 lines
1.9 KiB
Go
// Copyright (C) 2017 The Syncthing Authors.
|
|
//
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
|
|
package main
|
|
|
|
import (
|
|
"io/ioutil"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestAutoClosedFile(t *testing.T) {
|
|
os.RemoveAll("_autoclose")
|
|
defer os.RemoveAll("_autoclose")
|
|
_ = os.Mkdir("_autoclose", 0755)
|
|
file := filepath.FromSlash("_autoclose/tmp")
|
|
data := []byte("hello, world\n")
|
|
|
|
// An autoclosed file that closes very quickly
|
|
ac := newAutoclosedFile(file, time.Millisecond, time.Millisecond)
|
|
|
|
// Write some data.
|
|
if _, err := ac.Write(data); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
// Wait for it to close
|
|
start := time.Now()
|
|
for {
|
|
time.Sleep(time.Millisecond)
|
|
ac.mut.Lock()
|
|
fd := ac.fd
|
|
ac.mut.Unlock()
|
|
if fd == nil {
|
|
break
|
|
}
|
|
if time.Since(start) > time.Second {
|
|
t.Fatal("File should have been closed after first write")
|
|
}
|
|
}
|
|
|
|
// Write more data, which should be an append.
|
|
if _, err := ac.Write(data); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
// Close.
|
|
if err := ac.Close(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
// The file should have both writes in it.
|
|
bs, err := ioutil.ReadFile(file)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(bs) != 2*len(data) {
|
|
t.Fatalf("Writes failed, expected %d bytes, not %d", 2*len(data), len(bs))
|
|
}
|
|
|
|
// Open the file again.
|
|
ac = newAutoclosedFile(file, time.Second, time.Second)
|
|
|
|
// Write something
|
|
if _, err := ac.Write(data); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
// It should now contain only one write, because the first open
|
|
// should be a truncate.
|
|
bs, err = ioutil.ReadFile(file)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(bs) != len(data) {
|
|
t.Fatalf("Write failed, expected %d bytes, not %d", len(data), len(bs))
|
|
}
|
|
|
|
// Close.
|
|
if err := ac.Close(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|