Added tests for the behavior of dup and dup2 with regards to flushing.

geesefs-0-30-9
Aaron Jacobs 2015-03-23 10:44:21 +11:00
commit 590e59404b
2 changed files with 246 additions and 104 deletions

View File

@ -149,6 +149,27 @@ func (fs *flushFS) OpenFile(
return
}
func (fs *flushFS) ReadFile(
ctx context.Context,
req *fuse.ReadFileRequest) (
resp *fuse.ReadFileResponse, err error) {
resp = &fuse.ReadFileResponse{}
fs.mu.Lock()
defer fs.mu.Unlock()
// Ensure the offset is in range.
if req.Offset > int64(len(fs.fooContents)) {
return
}
// Read what we can.
resp.Data = make([]byte, req.Size)
copy(resp.Data, fs.fooContents[req.Offset:])
return
}
func (fs *flushFS) WriteFile(
ctx context.Context,
req *fuse.WriteFileRequest) (

View File

@ -16,9 +16,12 @@ package flushfs_test
import (
"io"
"io/ioutil"
"os"
"path"
"runtime"
"sync"
"syscall"
"testing"
"github.com/jacobsa/fuse"
@ -37,6 +40,10 @@ func TestFlushFS(t *testing.T) { RunTests(t) }
type FlushFSTest struct {
samples.SampleTest
// File handles that are closed in TearDown if non-nil.
f1 *os.File
f2 *os.File
mu sync.Mutex
// GUARDED_BY(mu)
@ -76,6 +83,20 @@ func (t *FlushFSTest) SetUp(ti *TestInfo) {
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())
}
// Finish tearing down.
t.SampleTest.TearDown()
}
////////////////////////////////////////////////////////////////////////
// Helpers
////////////////////////////////////////////////////////////////////////
@ -120,6 +141,19 @@ func (t *FlushFSTest) setFsyncError(err error) {
t.fsyncErr = err
}
// Like syscall.Dup2, but correctly annotates the syscall as blocking. See here
// for more info: https://github.com/golang/go/issues/10202
func dup2(oldfd int, newfd int) (err error) {
_, _, e1 := syscall.Syscall(
syscall.SYS_DUP2, uintptr(oldfd), uintptr(newfd), 0)
if e1 != 0 {
err = e1
}
return
}
////////////////////////////////////////////////////////////////////////
// Tests
////////////////////////////////////////////////////////////////////////
@ -131,26 +165,20 @@ func (t *FlushFSTest) CloseReports_ReadWrite() {
buf := make([]byte, 1024)
// 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)
defer func() {
if f != nil {
ExpectEq(nil, f.Close())
}
}()
// Write some contents to the file.
n, err = f.Write([]byte("taco"))
n, err = t.f1.Write([]byte("taco"))
AssertEq(nil, err)
AssertEq(4, n)
// Seek and read them back.
off, err = f.Seek(0, 0)
off, err = t.f1.Seek(0, 0)
AssertEq(nil, err)
AssertEq(0, off)
n, err = f.Read(buf)
n, err = t.f1.Read(buf)
AssertThat(err, AnyOf(nil, io.EOF))
AssertEq("taco", string(buf[:n]))
@ -159,8 +187,8 @@ func (t *FlushFSTest) CloseReports_ReadWrite() {
AssertThat(t.getFsyncs(), ElementsAre())
// Close the file.
err = f.Close()
f = nil
err = t.f1.Close()
t.f1 = nil
AssertEq(nil, err)
// Now we should have received the flush operation (but still no fsync).
@ -172,22 +200,16 @@ func (t *FlushFSTest) CloseReports_ReadOnly() {
var err error
// 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)
defer func() {
if f != nil {
ExpectEq(nil, f.Close())
}
}()
// At this point, no flushes or fsyncs should have happened.
AssertThat(t.getFlushes(), ElementsAre())
AssertThat(t.getFsyncs(), ElementsAre())
// Close the file.
err = f.Close()
f = nil
err = t.f1.Close()
t.f1 = nil
AssertEq(nil, err)
// Now we should have received the flush operation (but still no fsync).
@ -200,17 +222,11 @@ func (t *FlushFSTest) CloseReports_WriteOnly() {
var err error
// 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)
defer func() {
if f != nil {
ExpectEq(nil, f.Close())
}
}()
// Write some contents to the file.
n, err = f.Write([]byte("taco"))
n, err = t.f1.Write([]byte("taco"))
AssertEq(nil, err)
AssertEq(4, n)
@ -219,8 +235,8 @@ func (t *FlushFSTest) CloseReports_WriteOnly() {
AssertThat(t.getFsyncs(), ElementsAre())
// Close the file.
err = f.Close()
f = nil
err = t.f1.Close()
t.f1 = nil
AssertEq(nil, err)
// Now we should have received the flush operation (but still no fsync).
@ -233,17 +249,11 @@ func (t *FlushFSTest) CloseReports_MultipleTimes_NonOverlappingFileHandles() {
var err error
// 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)
defer func() {
if f != nil {
ExpectEq(nil, f.Close())
}
}()
// Write some contents to the file.
n, err = f.Write([]byte("taco"))
n, err = t.f1.Write([]byte("taco"))
AssertEq(nil, err)
AssertEq(4, n)
@ -252,8 +262,8 @@ func (t *FlushFSTest) CloseReports_MultipleTimes_NonOverlappingFileHandles() {
AssertThat(t.getFsyncs(), ElementsAre())
// Close the file.
err = f.Close()
f = nil
err = t.f1.Close()
t.f1 = nil
AssertEq(nil, err)
// Now we should have received the flush operation (but still no fsync).
@ -261,11 +271,11 @@ func (t *FlushFSTest) CloseReports_MultipleTimes_NonOverlappingFileHandles() {
AssertThat(t.getFsyncs(), ElementsAre())
// 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)
// Write again; expect no further flushes.
n, err = f.Write([]byte("p"))
n, err = t.f1.Write([]byte("p"))
AssertEq(nil, err)
AssertEq(1, n)
@ -273,8 +283,8 @@ func (t *FlushFSTest) CloseReports_MultipleTimes_NonOverlappingFileHandles() {
AssertThat(t.getFsyncs(), ElementsAre())
// Close the file. Now the new contents should be flushed.
err = f.Close()
f = nil
err = t.f1.Close()
t.f1 = nil
AssertEq(nil, err)
AssertThat(t.getFlushes(), ElementsAre("taco", "paco"))
@ -286,28 +296,18 @@ func (t *FlushFSTest) CloseReports_MultipleTimes_OverlappingFileHandles() {
var err error
// 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)
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)
defer func() {
if f1 != nil {
ExpectEq(nil, f1.Close())
}
if f2 != nil {
ExpectEq(nil, f2.Close())
}
}()
// Write some contents with each handle.
n, err = f1.Write([]byte("taco"))
n, err = t.f1.Write([]byte("taco"))
AssertEq(nil, err)
AssertEq(4, n)
n, err = f2.Write([]byte("p"))
n, err = t.f2.Write([]byte("p"))
AssertEq(nil, err)
AssertEq(1, n)
@ -316,15 +316,15 @@ func (t *FlushFSTest) CloseReports_MultipleTimes_OverlappingFileHandles() {
AssertThat(t.getFsyncs(), ElementsAre())
// Close one handle. The current contents should be flushed.
err = f1.Close()
f1 = nil
err = t.f1.Close()
t.f1 = nil
AssertEq(nil, err)
AssertThat(t.getFlushes(), ElementsAre("paco"))
AssertThat(t.getFsyncs(), ElementsAre())
// 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(3, n)
@ -332,35 +332,27 @@ func (t *FlushFSTest) CloseReports_MultipleTimes_OverlappingFileHandles() {
AssertThat(t.getFsyncs(), ElementsAre())
// Close the handle. Now the new contents should be flushed.
err = f2.Close()
f2 = nil
err = t.f2.Close()
t.f2 = nil
AssertEq(nil, err)
AssertThat(t.getFlushes(), ElementsAre("paco", "porp"))
AssertThat(t.getFsyncs(), ElementsAre())
}
func (t *FlushFSTest) CloseReports_DuplicatedFileDescriptor() {
AssertTrue(false, "TODO")
}
func (t *FlushFSTest) CloseError() {
// Open the file.
f, err := os.OpenFile(path.Join(t.Dir, "foo"), os.O_RDWR, 0)
AssertEq(nil, err)
var err error
defer func() {
if f != nil {
ExpectEq(nil, f.Close())
}
}()
// Open the file.
t.f1, err = os.OpenFile(path.Join(t.Dir, "foo"), os.O_RDWR, 0)
AssertEq(nil, err)
// Configure a flush error.
t.setFlushError(fuse.ENOENT)
// Close the file.
err = f.Close()
f = nil
err = t.f1.Close()
t.f1 = nil
AssertNe(nil, err)
ExpectThat(err, Error(HasSubstr("no such file")))
@ -371,17 +363,11 @@ func (t *FlushFSTest) FsyncReports() {
var err error
// 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)
defer func() {
if f != nil {
ExpectEq(nil, f.Close())
}
}()
// Write some contents to the file.
n, err = f.Write([]byte("taco"))
n, err = t.f1.Write([]byte("taco"))
AssertEq(nil, err)
AssertEq(4, n)
@ -389,14 +375,14 @@ func (t *FlushFSTest) FsyncReports() {
AssertThat(t.getFsyncs(), ElementsAre())
// Fsync.
err = f.Sync()
err = t.f1.Sync()
AssertEq(nil, err)
AssertThat(t.getFlushes(), ElementsAre())
AssertThat(t.getFsyncs(), ElementsAre("taco"))
// Write some more contents.
n, err = f.Write([]byte("s"))
n, err = t.f1.Write([]byte("s"))
AssertEq(nil, err)
AssertEq(1, n)
@ -404,7 +390,7 @@ func (t *FlushFSTest) FsyncReports() {
AssertThat(t.getFsyncs(), ElementsAre("taco"))
// Fsync.
err = f.Sync()
err = t.f1.Sync()
AssertEq(nil, err)
AssertThat(t.getFlushes(), ElementsAre())
@ -412,40 +398,175 @@ func (t *FlushFSTest) FsyncReports() {
}
func (t *FlushFSTest) FsyncError() {
// Open the file.
f, err := os.OpenFile(path.Join(t.Dir, "foo"), os.O_RDWR, 0)
AssertEq(nil, err)
var err error
defer func() {
if f != nil {
ExpectEq(nil, f.Close())
}
}()
// Open the file.
t.f1, err = os.OpenFile(path.Join(t.Dir, "foo"), os.O_RDWR, 0)
AssertEq(nil, err)
// Configure an fsync error.
t.setFsyncError(fuse.ENOENT)
// Fsync.
err = f.Sync()
err = t.f1.Sync()
AssertNe(nil, err)
ExpectThat(err, Error(HasSubstr("no such file")))
}
func (t *FlushFSTest) Dup() {
AssertTrue(false, "TODO")
var n int
var err error
isDarwin := runtime.GOOS == "darwin"
var expectedFlushes []interface{}
// Open the file.
t.f1, err = os.OpenFile(path.Join(t.Dir, "foo"), os.O_WRONLY, 0)
AssertEq(nil, err)
fd1 := t.f1.Fd()
// Use dup(2) to get another copy.
fd2, err := syscall.Dup(int(fd1))
AssertEq(nil, err)
t.f2 = os.NewFile(uintptr(fd2), t.f1.Name())
// Write some contents with each handle.
n, err = t.f1.Write([]byte("taco"))
AssertEq(nil, err)
AssertEq(4, n)
n, err = t.f2.Write([]byte("s"))
AssertEq(nil, err)
AssertEq(1, n)
// At this point, no flushes or fsyncs should have happened.
AssertThat(t.getFlushes(), ElementsAre())
AssertThat(t.getFsyncs(), ElementsAre())
// Close one handle. On Linux the current contents should be flushed. On OS
// X, where the semantics of handles are different, they apparently are not.
// (Cf. https://github.com/osxfuse/osxfuse/issues/199)
err = t.f1.Close()
t.f1 = nil
AssertEq(nil, err)
if !isDarwin {
expectedFlushes = append(expectedFlushes, "tacos")
}
AssertThat(t.getFlushes(), ElementsAre(expectedFlushes...))
AssertThat(t.getFsyncs(), ElementsAre())
// Write some more contents via the other handle. Again, no further flushes.
n, err = t.f2.Write([]byte("!"))
AssertEq(nil, err)
AssertEq(1, n)
AssertThat(t.getFlushes(), ElementsAre(expectedFlushes...))
AssertThat(t.getFsyncs(), ElementsAre())
// Close the handle. Now the new contents should be flushed.
err = t.f2.Close()
t.f2 = nil
AssertEq(nil, err)
expectedFlushes = append(expectedFlushes, "tacos!")
ExpectThat(t.getFlushes(), ElementsAre(expectedFlushes...))
ExpectThat(t.getFsyncs(), ElementsAre())
}
func (t *FlushFSTest) Dup_CloseError() {
AssertTrue(false, "TODO")
func (t *FlushFSTest) Dup_FlushError() {
var err error
// Open the file.
t.f1, err = os.OpenFile(path.Join(t.Dir, "foo"), os.O_WRONLY, 0)
AssertEq(nil, err)
fd1 := t.f1.Fd()
// Use dup(2) to get another copy.
fd2, err := syscall.Dup(int(fd1))
AssertEq(nil, err)
t.f2 = os.NewFile(uintptr(fd2), t.f1.Name())
// Configure a flush error.
t.setFlushError(fuse.ENOENT)
// Close by the first handle. On OS X, where the semantics of file handles
// are different (cf. https://github.com/osxfuse/osxfuse/issues/199), this
// does not result in an error.
err = t.f1.Close()
t.f1 = nil
if runtime.GOOS == "darwin" {
AssertEq(nil, err)
} else {
AssertNe(nil, err)
ExpectThat(err, Error(HasSubstr("no such file")))
}
// Close by the second handle.
err = t.f2.Close()
t.f2 = nil
AssertNe(nil, err)
ExpectThat(err, Error(HasSubstr("no such file")))
}
func (t *FlushFSTest) Dup2() {
AssertTrue(false, "TODO")
var n int
var err error
// Open the file.
t.f1, err = os.OpenFile(path.Join(t.Dir, "foo"), os.O_WRONLY, 0)
AssertEq(nil, err)
// Write some contents to the file.
n, err = t.f1.Write([]byte("taco"))
AssertEq(nil, err)
AssertEq(4, n)
// Open and unlink some temporary file.
t.f2, err = ioutil.TempFile("", "")
AssertEq(nil, err)
err = os.Remove(t.f2.Name())
AssertEq(nil, err)
// Duplicate the temporary file descriptor on top of the file from our file
// system. We should see a flush.
err = dup2(int(t.f2.Fd()), int(t.f1.Fd()))
ExpectEq(nil, err)
ExpectThat(t.getFlushes(), ElementsAre("taco"))
ExpectThat(t.getFsyncs(), ElementsAre())
}
func (t *FlushFSTest) Dup2_CloseError() {
AssertTrue(false, "TODO")
func (t *FlushFSTest) Dup2_FlushError() {
var err error
// Open the file.
t.f1, err = os.OpenFile(path.Join(t.Dir, "foo"), os.O_WRONLY, 0)
AssertEq(nil, err)
// Open and unlink some temporary file.
t.f2, err = ioutil.TempFile("", "")
AssertEq(nil, err)
err = os.Remove(t.f2.Name())
AssertEq(nil, err)
// Configure a flush error.
t.setFlushError(fuse.ENOENT)
// Duplicate the temporary file descriptor on top of the file from our file
// system. We shouldn't see the flush error.
err = dup2(int(t.f2.Fd()), int(t.f1.Fd()))
ExpectEq(nil, err)
}
func (t *FlushFSTest) Mmap() {