Remove file closing boilerplate.

geesefs-0-30-9
Aaron Jacobs 2015-03-23 10:32:30 +11:00
parent 6a25ef9666
commit 3a9a00d6f3
1 changed files with 87 additions and 168 deletions

View File

@ -39,6 +39,10 @@ func TestFlushFS(t *testing.T) { RunTests(t) }
type FlushFSTest struct { type FlushFSTest struct {
samples.SampleTest samples.SampleTest
// File handles that are closed in TearDown if non-nil.
f1 *os.File
f2 *os.File
mu sync.Mutex mu sync.Mutex
// GUARDED_BY(mu) // GUARDED_BY(mu)
@ -78,6 +82,17 @@ func (t *FlushFSTest) SetUp(ti *TestInfo) {
t.SampleTest.SetUp(ti) t.SampleTest.SetUp(ti)
} }
func (t *FlushFSTest) TearDown() {
// Close files if non-nil.
if t.f1 != nil {
ExpectEq(nil, t.f1.Close())
}
if t.f2 != nil {
ExpectEq(nil, t.f2.Close())
}
}
//////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////
// Helpers // Helpers
//////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////
@ -146,26 +161,20 @@ func (t *FlushFSTest) CloseReports_ReadWrite() {
buf := make([]byte, 1024) buf := make([]byte, 1024)
// Open the file. // Open the file.
f, err := os.OpenFile(path.Join(t.Dir, "foo"), os.O_RDWR, 0) t.f1, err = os.OpenFile(path.Join(t.Dir, "foo"), os.O_RDWR, 0)
AssertEq(nil, err) AssertEq(nil, err)
defer func() {
if f != nil {
ExpectEq(nil, f.Close())
}
}()
// Write some contents to the file. // Write some contents to the file.
n, err = f.Write([]byte("taco")) n, err = t.f1.Write([]byte("taco"))
AssertEq(nil, err) AssertEq(nil, err)
AssertEq(4, n) AssertEq(4, n)
// Seek and read them back. // Seek and read them back.
off, err = f.Seek(0, 0) off, err = t.f1.Seek(0, 0)
AssertEq(nil, err) AssertEq(nil, err)
AssertEq(0, off) AssertEq(0, off)
n, err = f.Read(buf) n, err = t.f1.Read(buf)
AssertThat(err, AnyOf(nil, io.EOF)) AssertThat(err, AnyOf(nil, io.EOF))
AssertEq("taco", string(buf[:n])) AssertEq("taco", string(buf[:n]))
@ -174,8 +183,8 @@ func (t *FlushFSTest) CloseReports_ReadWrite() {
AssertThat(t.getFsyncs(), ElementsAre()) AssertThat(t.getFsyncs(), ElementsAre())
// Close the file. // Close the file.
err = f.Close() err = t.f1.Close()
f = nil t.f1 = nil
AssertEq(nil, err) AssertEq(nil, err)
// Now we should have received the flush operation (but still no fsync). // Now we should have received the flush operation (but still no fsync).
@ -187,22 +196,16 @@ func (t *FlushFSTest) CloseReports_ReadOnly() {
var err error var err error
// Open the file. // Open the file.
f, err := os.OpenFile(path.Join(t.Dir, "foo"), os.O_RDONLY, 0) t.f1, err = os.OpenFile(path.Join(t.Dir, "foo"), os.O_RDONLY, 0)
AssertEq(nil, err) AssertEq(nil, err)
defer func() {
if f != nil {
ExpectEq(nil, f.Close())
}
}()
// At this point, no flushes or fsyncs should have happened. // At this point, no flushes or fsyncs should have happened.
AssertThat(t.getFlushes(), ElementsAre()) AssertThat(t.getFlushes(), ElementsAre())
AssertThat(t.getFsyncs(), ElementsAre()) AssertThat(t.getFsyncs(), ElementsAre())
// Close the file. // Close the file.
err = f.Close() err = t.f1.Close()
f = nil t.f1 = nil
AssertEq(nil, err) AssertEq(nil, err)
// Now we should have received the flush operation (but still no fsync). // Now we should have received the flush operation (but still no fsync).
@ -215,17 +218,11 @@ func (t *FlushFSTest) CloseReports_WriteOnly() {
var err error var err error
// Open the file. // Open the file.
f, err := os.OpenFile(path.Join(t.Dir, "foo"), os.O_WRONLY, 0) t.f1, err = os.OpenFile(path.Join(t.Dir, "foo"), os.O_WRONLY, 0)
AssertEq(nil, err) AssertEq(nil, err)
defer func() {
if f != nil {
ExpectEq(nil, f.Close())
}
}()
// Write some contents to the file. // Write some contents to the file.
n, err = f.Write([]byte("taco")) n, err = t.f1.Write([]byte("taco"))
AssertEq(nil, err) AssertEq(nil, err)
AssertEq(4, n) AssertEq(4, n)
@ -234,8 +231,8 @@ func (t *FlushFSTest) CloseReports_WriteOnly() {
AssertThat(t.getFsyncs(), ElementsAre()) AssertThat(t.getFsyncs(), ElementsAre())
// Close the file. // Close the file.
err = f.Close() err = t.f1.Close()
f = nil t.f1 = nil
AssertEq(nil, err) AssertEq(nil, err)
// Now we should have received the flush operation (but still no fsync). // Now we should have received the flush operation (but still no fsync).
@ -248,17 +245,11 @@ func (t *FlushFSTest) CloseReports_MultipleTimes_NonOverlappingFileHandles() {
var err error var err error
// Open the file. // Open the file.
f, err := os.OpenFile(path.Join(t.Dir, "foo"), os.O_WRONLY, 0) t.f1, err = os.OpenFile(path.Join(t.Dir, "foo"), os.O_WRONLY, 0)
AssertEq(nil, err) AssertEq(nil, err)
defer func() {
if f != nil {
ExpectEq(nil, f.Close())
}
}()
// Write some contents to the file. // Write some contents to the file.
n, err = f.Write([]byte("taco")) n, err = t.f1.Write([]byte("taco"))
AssertEq(nil, err) AssertEq(nil, err)
AssertEq(4, n) AssertEq(4, n)
@ -267,8 +258,8 @@ func (t *FlushFSTest) CloseReports_MultipleTimes_NonOverlappingFileHandles() {
AssertThat(t.getFsyncs(), ElementsAre()) AssertThat(t.getFsyncs(), ElementsAre())
// Close the file. // Close the file.
err = f.Close() err = t.f1.Close()
f = nil t.f1 = nil
AssertEq(nil, err) AssertEq(nil, err)
// Now we should have received the flush operation (but still no fsync). // Now we should have received the flush operation (but still no fsync).
@ -276,11 +267,11 @@ func (t *FlushFSTest) CloseReports_MultipleTimes_NonOverlappingFileHandles() {
AssertThat(t.getFsyncs(), ElementsAre()) AssertThat(t.getFsyncs(), ElementsAre())
// Open the file again. // Open the file again.
f, err = os.OpenFile(path.Join(t.Dir, "foo"), os.O_WRONLY, 0) t.f1, err = os.OpenFile(path.Join(t.Dir, "foo"), os.O_WRONLY, 0)
AssertEq(nil, err) AssertEq(nil, err)
// Write again; expect no further flushes. // Write again; expect no further flushes.
n, err = f.Write([]byte("p")) n, err = t.f1.Write([]byte("p"))
AssertEq(nil, err) AssertEq(nil, err)
AssertEq(1, n) AssertEq(1, n)
@ -288,8 +279,8 @@ func (t *FlushFSTest) CloseReports_MultipleTimes_NonOverlappingFileHandles() {
AssertThat(t.getFsyncs(), ElementsAre()) AssertThat(t.getFsyncs(), ElementsAre())
// Close the file. Now the new contents should be flushed. // Close the file. Now the new contents should be flushed.
err = f.Close() err = t.f1.Close()
f = nil t.f1 = nil
AssertEq(nil, err) AssertEq(nil, err)
AssertThat(t.getFlushes(), ElementsAre("taco", "paco")) AssertThat(t.getFlushes(), ElementsAre("taco", "paco"))
@ -301,28 +292,18 @@ func (t *FlushFSTest) CloseReports_MultipleTimes_OverlappingFileHandles() {
var err error var err error
// Open the file with two handles. // Open the file with two handles.
f1, err := os.OpenFile(path.Join(t.Dir, "foo"), os.O_WRONLY, 0) t.f1, err = os.OpenFile(path.Join(t.Dir, "foo"), os.O_WRONLY, 0)
AssertEq(nil, err) AssertEq(nil, err)
f2, err := os.OpenFile(path.Join(t.Dir, "foo"), os.O_WRONLY, 0) t.f2, err = os.OpenFile(path.Join(t.Dir, "foo"), os.O_WRONLY, 0)
AssertEq(nil, err) AssertEq(nil, err)
defer func() {
if f1 != nil {
ExpectEq(nil, f1.Close())
}
if f2 != nil {
ExpectEq(nil, f2.Close())
}
}()
// Write some contents with each handle. // Write some contents with each handle.
n, err = f1.Write([]byte("taco")) n, err = t.f1.Write([]byte("taco"))
AssertEq(nil, err) AssertEq(nil, err)
AssertEq(4, n) AssertEq(4, n)
n, err = f2.Write([]byte("p")) n, err = t.f2.Write([]byte("p"))
AssertEq(nil, err) AssertEq(nil, err)
AssertEq(1, n) AssertEq(1, n)
@ -331,15 +312,15 @@ func (t *FlushFSTest) CloseReports_MultipleTimes_OverlappingFileHandles() {
AssertThat(t.getFsyncs(), ElementsAre()) AssertThat(t.getFsyncs(), ElementsAre())
// Close one handle. The current contents should be flushed. // Close one handle. The current contents should be flushed.
err = f1.Close() err = t.f1.Close()
f1 = nil t.f1 = nil
AssertEq(nil, err) AssertEq(nil, err)
AssertThat(t.getFlushes(), ElementsAre("paco")) AssertThat(t.getFlushes(), ElementsAre("paco"))
AssertThat(t.getFsyncs(), ElementsAre()) AssertThat(t.getFsyncs(), ElementsAre())
// Write some more contents via the other handle. Again, no further flushes. // Write some more contents via the other handle. Again, no further flushes.
n, err = f2.Write([]byte("orp")) n, err = t.f2.Write([]byte("orp"))
AssertEq(nil, err) AssertEq(nil, err)
AssertEq(3, n) AssertEq(3, n)
@ -347,8 +328,8 @@ func (t *FlushFSTest) CloseReports_MultipleTimes_OverlappingFileHandles() {
AssertThat(t.getFsyncs(), ElementsAre()) AssertThat(t.getFsyncs(), ElementsAre())
// Close the handle. Now the new contents should be flushed. // Close the handle. Now the new contents should be flushed.
err = f2.Close() err = t.f2.Close()
f2 = nil t.f2 = nil
AssertEq(nil, err) AssertEq(nil, err)
AssertThat(t.getFlushes(), ElementsAre("paco", "porp")) AssertThat(t.getFlushes(), ElementsAre("paco", "porp"))
@ -356,22 +337,18 @@ func (t *FlushFSTest) CloseReports_MultipleTimes_OverlappingFileHandles() {
} }
func (t *FlushFSTest) CloseError() { func (t *FlushFSTest) CloseError() {
// Open the file. var err error
f, err := os.OpenFile(path.Join(t.Dir, "foo"), os.O_RDWR, 0)
AssertEq(nil, err)
defer func() { // Open the file.
if f != nil { t.f1, err = os.OpenFile(path.Join(t.Dir, "foo"), os.O_RDWR, 0)
ExpectEq(nil, f.Close()) AssertEq(nil, err)
}
}()
// Configure a flush error. // Configure a flush error.
t.setFlushError(fuse.ENOENT) t.setFlushError(fuse.ENOENT)
// Close the file. // Close the file.
err = f.Close() err = t.f1.Close()
f = nil t.f1 = nil
AssertNe(nil, err) AssertNe(nil, err)
ExpectThat(err, Error(HasSubstr("no such file"))) ExpectThat(err, Error(HasSubstr("no such file")))
@ -382,17 +359,11 @@ func (t *FlushFSTest) FsyncReports() {
var err error var err error
// Open the file. // Open the file.
f, err := os.OpenFile(path.Join(t.Dir, "foo"), os.O_WRONLY, 0) t.f1, err = os.OpenFile(path.Join(t.Dir, "foo"), os.O_WRONLY, 0)
AssertEq(nil, err) AssertEq(nil, err)
defer func() {
if f != nil {
ExpectEq(nil, f.Close())
}
}()
// Write some contents to the file. // Write some contents to the file.
n, err = f.Write([]byte("taco")) n, err = t.f1.Write([]byte("taco"))
AssertEq(nil, err) AssertEq(nil, err)
AssertEq(4, n) AssertEq(4, n)
@ -400,14 +371,14 @@ func (t *FlushFSTest) FsyncReports() {
AssertThat(t.getFsyncs(), ElementsAre()) AssertThat(t.getFsyncs(), ElementsAre())
// Fsync. // Fsync.
err = f.Sync() err = t.f1.Sync()
AssertEq(nil, err) AssertEq(nil, err)
AssertThat(t.getFlushes(), ElementsAre()) AssertThat(t.getFlushes(), ElementsAre())
AssertThat(t.getFsyncs(), ElementsAre("taco")) AssertThat(t.getFsyncs(), ElementsAre("taco"))
// Write some more contents. // Write some more contents.
n, err = f.Write([]byte("s")) n, err = t.f1.Write([]byte("s"))
AssertEq(nil, err) AssertEq(nil, err)
AssertEq(1, n) AssertEq(1, n)
@ -415,7 +386,7 @@ func (t *FlushFSTest) FsyncReports() {
AssertThat(t.getFsyncs(), ElementsAre("taco")) AssertThat(t.getFsyncs(), ElementsAre("taco"))
// Fsync. // Fsync.
err = f.Sync() err = t.f1.Sync()
AssertEq(nil, err) AssertEq(nil, err)
AssertThat(t.getFlushes(), ElementsAre()) AssertThat(t.getFlushes(), ElementsAre())
@ -423,21 +394,17 @@ func (t *FlushFSTest) FsyncReports() {
} }
func (t *FlushFSTest) FsyncError() { func (t *FlushFSTest) FsyncError() {
// Open the file. var err error
f, err := os.OpenFile(path.Join(t.Dir, "foo"), os.O_RDWR, 0)
AssertEq(nil, err)
defer func() { // Open the file.
if f != nil { t.f1, err = os.OpenFile(path.Join(t.Dir, "foo"), os.O_RDWR, 0)
ExpectEq(nil, f.Close()) AssertEq(nil, err)
}
}()
// Configure an fsync error. // Configure an fsync error.
t.setFsyncError(fuse.ENOENT) t.setFsyncError(fuse.ENOENT)
// Fsync. // Fsync.
err = f.Sync() err = t.f1.Sync()
AssertNe(nil, err) AssertNe(nil, err)
ExpectThat(err, Error(HasSubstr("no such file"))) ExpectThat(err, Error(HasSubstr("no such file")))
@ -447,36 +414,24 @@ func (t *FlushFSTest) Dup() {
var n int var n int
var err error var err error
var f1 *os.File
var f2 *os.File
defer func() {
if f1 != nil {
ExpectEq(nil, f1.Close())
}
if f2 != nil {
ExpectEq(nil, f2.Close())
}
}()
// Open the file. // Open the file.
f1, err = os.OpenFile(path.Join(t.Dir, "foo"), os.O_WRONLY, 0) t.f1, err = os.OpenFile(path.Join(t.Dir, "foo"), os.O_WRONLY, 0)
AssertEq(nil, err) AssertEq(nil, err)
fd1 := f1.Fd() fd1 := t.f1.Fd()
// Use dup(2) to get another copy. // Use dup(2) to get another copy.
fd2, err := syscall.Dup(int(fd1)) fd2, err := syscall.Dup(int(fd1))
AssertEq(nil, err) AssertEq(nil, err)
f2 = os.NewFile(uintptr(fd2), f1.Name()) t.f2 = os.NewFile(uintptr(fd2), t.f1.Name())
// Write some contents with each handle. // Write some contents with each handle.
n, err = f1.Write([]byte("taco")) n, err = t.f1.Write([]byte("taco"))
AssertEq(nil, err) AssertEq(nil, err)
AssertEq(4, n) AssertEq(4, n)
n, err = f2.Write([]byte("s")) n, err = t.f2.Write([]byte("s"))
AssertEq(nil, err) AssertEq(nil, err)
AssertEq(1, n) AssertEq(1, n)
@ -485,15 +440,15 @@ func (t *FlushFSTest) Dup() {
AssertThat(t.getFsyncs(), ElementsAre()) AssertThat(t.getFsyncs(), ElementsAre())
// Close one handle. The current contents should be flushed. // Close one handle. The current contents should be flushed.
err = f1.Close() err = t.f1.Close()
f1 = nil t.f1 = nil
AssertEq(nil, err) AssertEq(nil, err)
AssertThat(t.getFlushes(), ElementsAre("tacos")) AssertThat(t.getFlushes(), ElementsAre("tacos"))
AssertThat(t.getFsyncs(), ElementsAre()) AssertThat(t.getFsyncs(), ElementsAre())
// Write some more contents via the other handle. Again, no further flushes. // Write some more contents via the other handle. Again, no further flushes.
n, err = f2.Write([]byte("!")) n, err = t.f2.Write([]byte("!"))
AssertEq(nil, err) AssertEq(nil, err)
AssertEq(1, n) AssertEq(1, n)
@ -501,8 +456,8 @@ func (t *FlushFSTest) Dup() {
AssertThat(t.getFsyncs(), ElementsAre()) AssertThat(t.getFsyncs(), ElementsAre())
// Close the handle. Now the new contents should be flushed. // Close the handle. Now the new contents should be flushed.
err = f2.Close() err = t.f2.Close()
f2 = nil t.f2 = nil
AssertEq(nil, err) AssertEq(nil, err)
AssertThat(t.getFlushes(), ElementsAre("tacos", "tacos!")) AssertThat(t.getFlushes(), ElementsAre("tacos", "tacos!"))
@ -512,43 +467,31 @@ func (t *FlushFSTest) Dup() {
func (t *FlushFSTest) Dup_FlushError() { func (t *FlushFSTest) Dup_FlushError() {
var err error var err error
var f1 *os.File
var f2 *os.File
defer func() {
if f1 != nil {
ExpectEq(nil, f1.Close())
}
if f2 != nil {
ExpectEq(nil, f2.Close())
}
}()
// Open the file. // Open the file.
f1, err = os.OpenFile(path.Join(t.Dir, "foo"), os.O_WRONLY, 0) t.f1, err = os.OpenFile(path.Join(t.Dir, "foo"), os.O_WRONLY, 0)
AssertEq(nil, err) AssertEq(nil, err)
fd1 := f1.Fd() fd1 := t.f1.Fd()
// Use dup(2) to get another copy. // Use dup(2) to get another copy.
fd2, err := syscall.Dup(int(fd1)) fd2, err := syscall.Dup(int(fd1))
AssertEq(nil, err) AssertEq(nil, err)
f2 = os.NewFile(uintptr(fd2), f1.Name()) t.f2 = os.NewFile(uintptr(fd2), t.f1.Name())
// Configure a flush error. // Configure a flush error.
t.setFlushError(fuse.ENOENT) t.setFlushError(fuse.ENOENT)
// Close by the first handle. // Close by the first handle.
err = f1.Close() err = t.f1.Close()
f1 = nil t.f1 = nil
AssertNe(nil, err) AssertNe(nil, err)
ExpectThat(err, Error(HasSubstr("no such file"))) ExpectThat(err, Error(HasSubstr("no such file")))
// Close by the second handle. // Close by the second handle.
err = f2.Close() err = t.f2.Close()
f2 = nil t.f2 = nil
AssertNe(nil, err) AssertNe(nil, err)
ExpectThat(err, Error(HasSubstr("no such file"))) ExpectThat(err, Error(HasSubstr("no such file")))
@ -558,37 +501,25 @@ func (t *FlushFSTest) Dup2() {
var n int var n int
var err error var err error
var f1 *os.File
var f2 *os.File
defer func() {
if f1 != nil {
ExpectEq(nil, f1.Close())
}
if f2 != nil {
ExpectEq(nil, f2.Close())
}
}()
// Open the file. // Open the file.
f1, err = os.OpenFile(path.Join(t.Dir, "foo"), os.O_WRONLY, 0) t.f1, err = os.OpenFile(path.Join(t.Dir, "foo"), os.O_WRONLY, 0)
AssertEq(nil, err) AssertEq(nil, err)
// Write some contents to the file. // Write some contents to the file.
n, err = f1.Write([]byte("taco")) n, err = t.f1.Write([]byte("taco"))
AssertEq(nil, err) AssertEq(nil, err)
AssertEq(4, n) AssertEq(4, n)
// Open and unlink some temporary file. // Open and unlink some temporary file.
f2, err = ioutil.TempFile("", "") t.f2, err = ioutil.TempFile("", "")
AssertEq(nil, err) AssertEq(nil, err)
err = os.Remove(f2.Name()) err = os.Remove(t.f2.Name())
AssertEq(nil, err) AssertEq(nil, err)
// Duplicate the temporary file descriptor on top of the file from our file // Duplicate the temporary file descriptor on top of the file from our file
// system. We should see a flush. // system. We should see a flush.
err = dup2(int(f2.Fd()), int(f1.Fd())) err = dup2(int(t.f2.Fd()), int(t.f1.Fd()))
ExpectEq(nil, err) ExpectEq(nil, err)
ExpectThat(t.getFlushes(), ElementsAre("taco")) ExpectThat(t.getFlushes(), ElementsAre("taco"))
@ -598,27 +529,15 @@ func (t *FlushFSTest) Dup2() {
func (t *FlushFSTest) Dup2_FlushError() { func (t *FlushFSTest) Dup2_FlushError() {
var err error var err error
var f1 *os.File
var f2 *os.File
defer func() {
if f1 != nil {
ExpectEq(nil, f1.Close())
}
if f2 != nil {
ExpectEq(nil, f2.Close())
}
}()
// Open the file. // Open the file.
f1, err = os.OpenFile(path.Join(t.Dir, "foo"), os.O_WRONLY, 0) t.f1, err = os.OpenFile(path.Join(t.Dir, "foo"), os.O_WRONLY, 0)
AssertEq(nil, err) AssertEq(nil, err)
// Open and unlink some temporary file. // Open and unlink some temporary file.
f2, err = ioutil.TempFile("", "") t.f2, err = ioutil.TempFile("", "")
AssertEq(nil, err) AssertEq(nil, err)
err = os.Remove(f2.Name()) err = os.Remove(t.f2.Name())
AssertEq(nil, err) AssertEq(nil, err)
// Configure a flush error. // Configure a flush error.
@ -626,7 +545,7 @@ func (t *FlushFSTest) Dup2_FlushError() {
// Duplicate the temporary file descriptor on top of the file from our file // Duplicate the temporary file descriptor on top of the file from our file
// system. We shouldn't see the flush error. // system. We shouldn't see the flush error.
err = dup2(int(f2.Fd()), int(f1.Fd())) err = dup2(int(t.f2.Fd()), int(t.f1.Fd()))
ExpectEq(nil, err) ExpectEq(nil, err)
} }