fusego/samples/flushfs/flush_fs_test.go

723 lines
17 KiB
Go
Raw Normal View History

2015-03-20 03:17:05 +03:00
// Copyright 2015 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package flushfs_test
import (
2015-03-20 03:33:15 +03:00
"io"
2015-03-20 06:48:47 +03:00
"io/ioutil"
2015-03-20 03:33:15 +03:00
"os"
"path"
2015-03-23 02:41:59 +03:00
"runtime"
2015-03-20 03:20:29 +03:00
"sync"
2015-03-20 05:44:34 +03:00
"syscall"
2015-03-20 03:17:05 +03:00
"testing"
2015-03-20 04:01:59 +03:00
"github.com/jacobsa/fuse"
2015-03-20 03:17:05 +03:00
"github.com/jacobsa/fuse/samples"
2015-03-20 03:20:29 +03:00
"github.com/jacobsa/fuse/samples/flushfs"
2015-03-20 03:33:15 +03:00
. "github.com/jacobsa/oglematchers"
2015-03-20 03:17:05 +03:00
. "github.com/jacobsa/ogletest"
)
func TestFlushFS(t *testing.T) { RunTests(t) }
////////////////////////////////////////////////////////////////////////
// Boilerplate
////////////////////////////////////////////////////////////////////////
type FlushFSTest struct {
samples.SampleTest
2015-03-23 02:32:30 +03:00
// File handles that are closed in TearDown if non-nil.
f1 *os.File
f2 *os.File
2015-03-20 03:20:29 +03:00
mu sync.Mutex
// GUARDED_BY(mu)
2015-03-20 03:52:54 +03:00
flushes []string
flushErr error
2015-03-20 03:20:29 +03:00
// GUARDED_BY(mu)
2015-03-20 03:52:54 +03:00
fsyncs []string
fsyncErr error
2015-03-20 03:17:05 +03:00
}
func init() { RegisterTestSuite(&FlushFSTest{}) }
2015-03-20 03:20:29 +03:00
func (t *FlushFSTest) SetUp(ti *TestInfo) {
2015-03-20 03:21:38 +03:00
var err error
2015-03-20 03:20:29 +03:00
// Set up a file system.
2015-03-20 03:52:54 +03:00
reportTo := func(slice *[]string, err *error) func(string) error {
return func(s string) error {
2015-03-20 03:20:29 +03:00
t.mu.Lock()
defer t.mu.Unlock()
2015-03-20 03:52:54 +03:00
2015-03-20 03:20:29 +03:00
*slice = append(*slice, s)
2015-03-20 03:52:54 +03:00
return *err
2015-03-20 03:20:29 +03:00
}
}
2015-03-20 03:21:38 +03:00
t.FileSystem, err = flushfs.NewFileSystem(
2015-03-20 03:52:54 +03:00
reportTo(&t.flushes, &t.flushErr),
reportTo(&t.fsyncs, &t.fsyncErr))
2015-03-20 03:21:38 +03:00
if err != nil {
panic(err)
}
// Mount it.
t.SampleTest.SetUp(ti)
2015-03-20 03:20:29 +03:00
}
2015-03-20 03:17:05 +03:00
2015-03-23 02:32:30 +03:00
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())
}
2015-03-23 02:37:32 +03:00
// Finish tearing down.
t.SampleTest.TearDown()
2015-03-23 02:32:30 +03:00
}
2015-03-20 03:17:05 +03:00
////////////////////////////////////////////////////////////////////////
2015-03-20 03:25:15 +03:00
// Helpers
2015-03-20 03:17:05 +03:00
////////////////////////////////////////////////////////////////////////
2015-03-20 03:25:15 +03:00
// Return a copy of the current contents of t.flushes.
//
// LOCKS_EXCLUDED(t.mu)
2015-03-20 03:26:32 +03:00
func (t *FlushFSTest) getFlushes() (p []string) {
t.mu.Lock()
defer t.mu.Unlock()
p = make([]string, len(t.flushes))
copy(p, t.flushes)
return
}
2015-03-20 03:25:15 +03:00
// Return a copy of the current contents of t.fsyncs.
//
// LOCKS_EXCLUDED(t.mu)
2015-03-20 03:26:32 +03:00
func (t *FlushFSTest) getFsyncs() (p []string) {
t.mu.Lock()
defer t.mu.Unlock()
p = make([]string, len(t.fsyncs))
copy(p, t.fsyncs)
return
}
2015-03-20 03:25:15 +03:00
2015-03-20 04:01:59 +03:00
// LOCKS_EXCLUDED(t.mu)
func (t *FlushFSTest) setFlushError(err error) {
t.mu.Lock()
defer t.mu.Unlock()
t.flushErr = err
}
// LOCKS_EXCLUDED(t.mu)
func (t *FlushFSTest) setFsyncError(err error) {
t.mu.Lock()
defer t.mu.Unlock()
t.fsyncErr = err
}
2015-03-23 02:27:22 +03:00
// 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
}
2015-03-20 03:25:15 +03:00
////////////////////////////////////////////////////////////////////////
// Tests
////////////////////////////////////////////////////////////////////////
2015-03-20 03:33:15 +03:00
func (t *FlushFSTest) CloseReports_ReadWrite() {
var n int
var off int64
var err error
buf := make([]byte, 1024)
// Open the file.
2015-03-23 02:32:30 +03:00
t.f1, err = os.OpenFile(path.Join(t.Dir, "foo"), os.O_RDWR, 0)
2015-03-20 03:33:15 +03:00
AssertEq(nil, err)
// Write some contents to the file.
2015-03-23 02:32:30 +03:00
n, err = t.f1.Write([]byte("taco"))
2015-03-20 03:33:15 +03:00
AssertEq(nil, err)
AssertEq(4, n)
// Seek and read them back.
2015-03-23 02:32:30 +03:00
off, err = t.f1.Seek(0, 0)
2015-03-20 03:33:15 +03:00
AssertEq(nil, err)
2015-03-20 03:49:45 +03:00
AssertEq(0, off)
2015-03-20 03:33:15 +03:00
2015-03-23 02:32:30 +03:00
n, err = t.f1.Read(buf)
2015-03-20 03:33:15 +03:00
AssertThat(err, AnyOf(nil, io.EOF))
2015-03-20 03:49:45 +03:00
AssertEq("taco", string(buf[:n]))
2015-03-20 03:33:15 +03:00
// At this point, no flushes or fsyncs should have happened.
AssertThat(t.getFlushes(), ElementsAre())
AssertThat(t.getFsyncs(), ElementsAre())
// Close the file.
2015-03-23 02:32:30 +03:00
err = t.f1.Close()
t.f1 = nil
2015-03-20 03:33:15 +03:00
AssertEq(nil, err)
// Now we should have received the flush operation (but still no fsync).
2015-03-20 03:57:21 +03:00
ExpectThat(t.getFlushes(), ElementsAre("taco"))
2015-03-20 03:33:15 +03:00
ExpectThat(t.getFsyncs(), ElementsAre())
}
func (t *FlushFSTest) CloseReports_ReadOnly() {
2015-03-20 03:54:09 +03:00
var err error
// Open the file.
2015-03-23 02:32:30 +03:00
t.f1, err = os.OpenFile(path.Join(t.Dir, "foo"), os.O_RDONLY, 0)
2015-03-20 03:54:09 +03:00
AssertEq(nil, err)
// At this point, no flushes or fsyncs should have happened.
AssertThat(t.getFlushes(), ElementsAre())
AssertThat(t.getFsyncs(), ElementsAre())
// Close the file.
2015-03-23 02:32:30 +03:00
err = t.f1.Close()
t.f1 = nil
2015-03-20 03:54:09 +03:00
AssertEq(nil, err)
// Now we should have received the flush operation (but still no fsync).
2015-03-20 03:57:21 +03:00
ExpectThat(t.getFlushes(), ElementsAre(""))
2015-03-20 03:54:09 +03:00
ExpectThat(t.getFsyncs(), ElementsAre())
2015-03-20 03:33:15 +03:00
}
func (t *FlushFSTest) CloseReports_WriteOnly() {
2015-03-20 03:54:43 +03:00
var n int
var err error
// Open the file.
2015-03-23 02:32:30 +03:00
t.f1, err = os.OpenFile(path.Join(t.Dir, "foo"), os.O_WRONLY, 0)
2015-03-20 03:54:43 +03:00
AssertEq(nil, err)
// Write some contents to the file.
2015-03-23 02:32:30 +03:00
n, err = t.f1.Write([]byte("taco"))
2015-03-20 03:54:43 +03:00
AssertEq(nil, err)
AssertEq(4, n)
// At this point, no flushes or fsyncs should have happened.
AssertThat(t.getFlushes(), ElementsAre())
AssertThat(t.getFsyncs(), ElementsAre())
// Close the file.
2015-03-23 02:32:30 +03:00
err = t.f1.Close()
t.f1 = nil
2015-03-20 03:54:43 +03:00
AssertEq(nil, err)
// Now we should have received the flush operation (but still no fsync).
2015-03-20 03:57:21 +03:00
ExpectThat(t.getFlushes(), ElementsAre("taco"))
2015-03-20 03:54:43 +03:00
ExpectThat(t.getFsyncs(), ElementsAre())
2015-03-20 03:33:15 +03:00
}
func (t *FlushFSTest) CloseReports_MultipleTimes_NonOverlappingFileHandles() {
var n int
var err error
// Open the file.
2015-03-23 02:32:30 +03:00
t.f1, err = os.OpenFile(path.Join(t.Dir, "foo"), os.O_WRONLY, 0)
AssertEq(nil, err)
// Write some contents to the file.
2015-03-23 02:32:30 +03:00
n, err = t.f1.Write([]byte("taco"))
AssertEq(nil, err)
AssertEq(4, n)
// At this point, no flushes or fsyncs should have happened.
AssertThat(t.getFlushes(), ElementsAre())
AssertThat(t.getFsyncs(), ElementsAre())
// Close the file.
2015-03-23 02:32:30 +03:00
err = t.f1.Close()
t.f1 = nil
AssertEq(nil, err)
// Now we should have received the flush operation (but still no fsync).
2015-03-20 03:57:21 +03:00
AssertThat(t.getFlushes(), ElementsAre("taco"))
AssertThat(t.getFsyncs(), ElementsAre())
// Open the file again.
2015-03-23 02:32:30 +03:00
t.f1, err = os.OpenFile(path.Join(t.Dir, "foo"), os.O_WRONLY, 0)
AssertEq(nil, err)
// Write again; expect no further flushes.
2015-03-23 02:32:30 +03:00
n, err = t.f1.Write([]byte("p"))
AssertEq(nil, err)
AssertEq(1, n)
2015-03-20 03:57:21 +03:00
AssertThat(t.getFlushes(), ElementsAre("taco"))
AssertThat(t.getFsyncs(), ElementsAre())
// Close the file. Now the new contents should be flushed.
2015-03-23 02:32:30 +03:00
err = t.f1.Close()
t.f1 = nil
AssertEq(nil, err)
2015-03-20 03:57:21 +03:00
AssertThat(t.getFlushes(), ElementsAre("taco", "paco"))
AssertThat(t.getFsyncs(), ElementsAre())
2015-03-20 03:25:15 +03:00
}
2015-03-20 03:33:15 +03:00
func (t *FlushFSTest) CloseReports_MultipleTimes_OverlappingFileHandles() {
var n int
var err error
// Open the file with two handles.
2015-03-23 02:32:30 +03:00
t.f1, err = os.OpenFile(path.Join(t.Dir, "foo"), os.O_WRONLY, 0)
AssertEq(nil, err)
2015-03-23 02:32:30 +03:00
t.f2, err = os.OpenFile(path.Join(t.Dir, "foo"), os.O_WRONLY, 0)
AssertEq(nil, err)
// Write some contents with each handle.
2015-03-23 02:32:30 +03:00
n, err = t.f1.Write([]byte("taco"))
AssertEq(nil, err)
AssertEq(4, n)
2015-03-23 02:32:30 +03:00
n, err = t.f2.Write([]byte("p"))
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. The current contents should be flushed.
2015-03-23 02:32:30 +03:00
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.
2015-03-23 02:32:30 +03:00
n, err = t.f2.Write([]byte("orp"))
AssertEq(nil, err)
AssertEq(3, n)
AssertThat(t.getFlushes(), ElementsAre("paco"))
AssertThat(t.getFsyncs(), ElementsAre())
// Close the handle. Now the new contents should be flushed.
2015-03-23 02:32:30 +03:00
err = t.f2.Close()
t.f2 = nil
AssertEq(nil, err)
AssertThat(t.getFlushes(), ElementsAre("paco", "porp"))
AssertThat(t.getFsyncs(), ElementsAre())
2015-03-20 03:25:15 +03:00
}
2015-03-20 03:33:15 +03:00
func (t *FlushFSTest) CloseError() {
2015-03-23 02:32:30 +03:00
var err error
2015-03-20 04:03:25 +03:00
// Open the file.
2015-03-23 02:32:30 +03:00
t.f1, err = os.OpenFile(path.Join(t.Dir, "foo"), os.O_RDWR, 0)
2015-03-20 04:01:59 +03:00
AssertEq(nil, err)
// Configure a flush error.
t.setFlushError(fuse.ENOENT)
// Close the file.
2015-03-23 02:32:30 +03:00
err = t.f1.Close()
t.f1 = nil
2015-03-20 04:01:59 +03:00
AssertNe(nil, err)
2015-03-20 05:29:15 +03:00
ExpectThat(err, Error(HasSubstr("no such file")))
2015-03-20 03:23:15 +03:00
}
2015-03-20 03:33:15 +03:00
func (t *FlushFSTest) FsyncReports() {
2015-03-20 04:03:25 +03:00
var n int
var err error
// Open the file.
2015-03-23 02:32:30 +03:00
t.f1, err = os.OpenFile(path.Join(t.Dir, "foo"), os.O_WRONLY, 0)
2015-03-20 04:03:25 +03:00
AssertEq(nil, err)
// Write some contents to the file.
2015-03-23 02:32:30 +03:00
n, err = t.f1.Write([]byte("taco"))
2015-03-20 04:03:25 +03:00
AssertEq(nil, err)
AssertEq(4, n)
AssertThat(t.getFlushes(), ElementsAre())
AssertThat(t.getFsyncs(), ElementsAre())
// Fsync.
2015-03-23 02:32:30 +03:00
err = t.f1.Sync()
2015-03-20 04:03:25 +03:00
AssertEq(nil, err)
AssertThat(t.getFlushes(), ElementsAre())
AssertThat(t.getFsyncs(), ElementsAre("taco"))
// Write some more contents.
2015-03-23 02:32:30 +03:00
n, err = t.f1.Write([]byte("s"))
2015-03-20 04:03:25 +03:00
AssertEq(nil, err)
AssertEq(1, n)
AssertThat(t.getFlushes(), ElementsAre())
AssertThat(t.getFsyncs(), ElementsAre("taco"))
// Fsync.
2015-03-23 02:32:30 +03:00
err = t.f1.Sync()
2015-03-20 04:03:25 +03:00
AssertEq(nil, err)
AssertThat(t.getFlushes(), ElementsAre())
AssertThat(t.getFsyncs(), ElementsAre("taco", "tacos"))
2015-03-20 03:23:15 +03:00
}
2015-03-20 03:33:15 +03:00
func (t *FlushFSTest) FsyncError() {
2015-03-23 02:32:30 +03:00
var err error
2015-03-20 04:04:25 +03:00
// Open the file.
2015-03-23 02:32:30 +03:00
t.f1, err = os.OpenFile(path.Join(t.Dir, "foo"), os.O_RDWR, 0)
2015-03-20 04:04:25 +03:00
AssertEq(nil, err)
// Configure an fsync error.
t.setFsyncError(fuse.ENOENT)
// Fsync.
2015-03-23 02:32:30 +03:00
err = t.f1.Sync()
2015-03-20 04:04:25 +03:00
AssertNe(nil, err)
2015-03-20 05:29:15 +03:00
ExpectThat(err, Error(HasSubstr("no such file")))
2015-03-20 03:17:05 +03:00
}
2015-03-20 05:21:14 +03:00
func (t *FlushFSTest) Dup() {
2015-03-20 05:44:34 +03:00
var n int
var err error
2015-03-23 02:41:59 +03:00
isDarwin := runtime.GOOS == "darwin"
var expectedFlushes []interface{}
2015-03-20 05:44:34 +03:00
// Open the file.
2015-03-23 02:32:30 +03:00
t.f1, err = os.OpenFile(path.Join(t.Dir, "foo"), os.O_WRONLY, 0)
2015-03-20 05:44:34 +03:00
AssertEq(nil, err)
2015-03-23 02:32:30 +03:00
fd1 := t.f1.Fd()
2015-03-20 05:44:34 +03:00
// Use dup(2) to get another copy.
fd2, err := syscall.Dup(int(fd1))
AssertEq(nil, err)
2015-03-23 02:32:30 +03:00
t.f2 = os.NewFile(uintptr(fd2), t.f1.Name())
2015-03-20 05:44:34 +03:00
// Write some contents with each handle.
2015-03-23 02:32:30 +03:00
n, err = t.f1.Write([]byte("taco"))
2015-03-20 05:44:34 +03:00
AssertEq(nil, err)
AssertEq(4, n)
2015-03-23 02:32:30 +03:00
n, err = t.f2.Write([]byte("s"))
2015-03-20 05:44:34 +03:00
AssertEq(nil, err)
AssertEq(1, n)
// At this point, no flushes or fsyncs should have happened.
AssertThat(t.getFlushes(), ElementsAre())
AssertThat(t.getFsyncs(), ElementsAre())
2015-03-23 02:41:59 +03:00
// 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)
2015-03-23 02:32:30 +03:00
err = t.f1.Close()
t.f1 = nil
2015-03-20 05:44:34 +03:00
AssertEq(nil, err)
2015-03-23 02:41:59 +03:00
if !isDarwin {
expectedFlushes = append(expectedFlushes, "tacos")
}
AssertThat(t.getFlushes(), ElementsAre(expectedFlushes...))
2015-03-20 05:44:34 +03:00
AssertThat(t.getFsyncs(), ElementsAre())
// Write some more contents via the other handle. Again, no further flushes.
2015-03-23 02:32:30 +03:00
n, err = t.f2.Write([]byte("!"))
2015-03-20 05:44:34 +03:00
AssertEq(nil, err)
2015-03-20 05:47:47 +03:00
AssertEq(1, n)
2015-03-20 05:44:34 +03:00
2015-03-23 02:41:59 +03:00
AssertThat(t.getFlushes(), ElementsAre(expectedFlushes...))
2015-03-20 05:44:34 +03:00
AssertThat(t.getFsyncs(), ElementsAre())
// Close the handle. Now the new contents should be flushed.
2015-03-23 02:32:30 +03:00
err = t.f2.Close()
t.f2 = nil
2015-03-20 05:44:34 +03:00
AssertEq(nil, err)
2015-03-23 02:41:59 +03:00
expectedFlushes = append(expectedFlushes, "tacos!")
ExpectThat(t.getFlushes(), ElementsAre(expectedFlushes...))
ExpectThat(t.getFsyncs(), ElementsAre())
2015-03-20 05:21:14 +03:00
}
2015-03-20 05:54:52 +03:00
func (t *FlushFSTest) Dup_FlushError() {
var err error
// Open the file.
2015-03-23 02:32:30 +03:00
t.f1, err = os.OpenFile(path.Join(t.Dir, "foo"), os.O_WRONLY, 0)
2015-03-20 05:54:52 +03:00
AssertEq(nil, err)
2015-03-23 02:32:30 +03:00
fd1 := t.f1.Fd()
2015-03-20 05:54:52 +03:00
// Use dup(2) to get another copy.
fd2, err := syscall.Dup(int(fd1))
AssertEq(nil, err)
2015-03-23 02:32:30 +03:00
t.f2 = os.NewFile(uintptr(fd2), t.f1.Name())
2015-03-20 05:54:52 +03:00
// 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.
2015-03-23 02:32:30 +03:00
err = t.f1.Close()
t.f1 = nil
2015-03-20 05:54:52 +03:00
if runtime.GOOS == "darwin" {
AssertEq(nil, err)
} else {
AssertNe(nil, err)
ExpectThat(err, Error(HasSubstr("no such file")))
}
2015-03-20 05:54:52 +03:00
// Close by the second handle.
2015-03-23 02:32:30 +03:00
err = t.f2.Close()
t.f2 = nil
2015-03-20 05:54:52 +03:00
AssertNe(nil, err)
ExpectThat(err, Error(HasSubstr("no such file")))
2015-03-20 05:21:14 +03:00
}
func (t *FlushFSTest) Dup2() {
2015-03-20 06:48:47 +03:00
var n int
var err error
// Open the file.
2015-03-23 02:32:30 +03:00
t.f1, err = os.OpenFile(path.Join(t.Dir, "foo"), os.O_WRONLY, 0)
2015-03-20 06:48:47 +03:00
AssertEq(nil, err)
// Write some contents to the file.
2015-03-23 02:32:30 +03:00
n, err = t.f1.Write([]byte("taco"))
2015-03-20 06:48:47 +03:00
AssertEq(nil, err)
AssertEq(4, n)
// Open and unlink some temporary file.
2015-03-23 02:32:30 +03:00
t.f2, err = ioutil.TempFile("", "")
2015-03-20 06:48:47 +03:00
AssertEq(nil, err)
2015-03-23 02:32:30 +03:00
err = os.Remove(t.f2.Name())
2015-03-20 06:48:47 +03:00
AssertEq(nil, err)
// Duplicate the temporary file descriptor on top of the file from our file
// system. We should see a flush.
2015-03-23 02:32:30 +03:00
err = dup2(int(t.f2.Fd()), int(t.f1.Fd()))
2015-03-20 06:48:47 +03:00
ExpectEq(nil, err)
ExpectThat(t.getFlushes(), ElementsAre("taco"))
ExpectThat(t.getFsyncs(), ElementsAre())
2015-03-20 05:21:14 +03:00
}
2015-03-20 05:54:52 +03:00
func (t *FlushFSTest) Dup2_FlushError() {
2015-03-23 02:28:15 +03:00
var err error
// Open the file.
2015-03-23 02:32:30 +03:00
t.f1, err = os.OpenFile(path.Join(t.Dir, "foo"), os.O_WRONLY, 0)
2015-03-23 02:28:15 +03:00
AssertEq(nil, err)
// Open and unlink some temporary file.
2015-03-23 02:32:30 +03:00
t.f2, err = ioutil.TempFile("", "")
2015-03-23 02:28:15 +03:00
AssertEq(nil, err)
2015-03-23 02:32:30 +03:00
err = os.Remove(t.f2.Name())
2015-03-23 02:28:15 +03:00
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.
2015-03-23 02:32:30 +03:00
err = dup2(int(t.f2.Fd()), int(t.f1.Fd()))
2015-03-23 02:28:15 +03:00
ExpectEq(nil, err)
2015-03-20 05:21:14 +03:00
}
2015-03-23 03:40:03 +03:00
func (t *FlushFSTest) Mmap_MunmapBeforeClose() {
2015-03-23 03:40:36 +03:00
var n int
var err error
// If we run this test with GOMAXPROCS=1 (the default), the program will
// deadlock for the reason described here:
//
// https://groups.google.com/d/msg/golang-nuts/11rdExWP6ac/TzwT6HBOb3wJ
//
// In summary, the goroutine reading from the mmap'd file is camping on a
// scheduler slot while it blocks on a page fault, and the goroutine handling
// fuse requests is waiting for the scheduler slot.
//
// So run with GOMAXPROCS=2.
old := runtime.GOMAXPROCS(2)
defer runtime.GOMAXPROCS(old)
// Open the file.
t.f1, err = os.OpenFile(path.Join(t.Dir, "foo"), os.O_RDWR, 0)
AssertEq(nil, err)
// Write some contents to the file.
n, err = t.f1.Write([]byte("taco"))
AssertEq(nil, err)
AssertEq(4, n)
// mmap the file.
data, err := syscall.Mmap(
int(t.f1.Fd()), 0, 4,
syscall.PROT_READ|syscall.PROT_WRITE,
syscall.MAP_SHARED)
AssertEq(nil, err)
AssertEq("taco", string(data))
// Modify then unmap.
data[0] = 'p'
err = syscall.Munmap(data)
AssertEq(nil, err)
// munmap does not cause a flush.
ExpectThat(t.getFlushes(), ElementsAre())
ExpectThat(t.getFsyncs(), ElementsAre())
2015-03-23 04:28:13 +03:00
// Close the file. We should see a flush. On Darwin, this will contain out of
// date contents (cf. https://github.com/osxfuse/osxfuse/issues/202).
2015-03-23 03:40:36 +03:00
err = t.f1.Close()
t.f1 = nil
AssertEq(nil, err)
2015-03-23 04:28:13 +03:00
if runtime.GOOS == "darwin" {
ExpectThat(t.getFlushes(), ElementsAre("taco"))
ExpectThat(t.getFsyncs(), ElementsAre())
} else {
ExpectThat(t.getFlushes(), ElementsAre("paco"))
ExpectThat(t.getFsyncs(), ElementsAre())
}
2015-03-23 03:40:03 +03:00
}
func (t *FlushFSTest) Mmap_CloseBeforeMunmap() {
2015-03-23 02:52:37 +03:00
var n int
var err error
// If we run this test with GOMAXPROCS=1 (the default), the program will
// deadlock for the reason described here:
//
// https://groups.google.com/d/msg/golang-nuts/11rdExWP6ac/TzwT6HBOb3wJ
//
// In summary, the goroutine reading from the mmap'd file is camping on a
// scheduler slot while it blocks on a page fault, and the goroutine handling
// fuse requests is waiting for the scheduler slot.
//
// So run with GOMAXPROCS=2.
old := runtime.GOMAXPROCS(2)
defer runtime.GOMAXPROCS(old)
2015-03-23 02:52:37 +03:00
// Open the file.
t.f1, err = os.OpenFile(path.Join(t.Dir, "foo"), os.O_RDWR, 0)
AssertEq(nil, err)
// Write some contents to the file.
n, err = t.f1.Write([]byte("taco"))
AssertEq(nil, err)
AssertEq(4, n)
// mmap the file.
data, err := syscall.Mmap(
int(t.f1.Fd()), 0, 4,
syscall.PROT_READ|syscall.PROT_WRITE,
syscall.MAP_SHARED)
AssertEq(nil, err)
AssertEq("taco", string(data))
// Close the file. We should see a flush.
err = t.f1.Close()
t.f1 = nil
AssertEq(nil, err)
AssertThat(t.getFlushes(), ElementsAre("taco"))
AssertThat(t.getFsyncs(), ElementsAre())
2015-03-23 03:40:03 +03:00
// Modify then unmap.
2015-03-23 02:52:37 +03:00
data[0] = 'p'
err = syscall.Munmap(data)
AssertEq(nil, err)
2015-03-23 03:40:03 +03:00
// munmap does not cause a flush.
ExpectThat(t.getFlushes(), ElementsAre("taco"))
2015-03-23 02:52:37 +03:00
ExpectThat(t.getFsyncs(), ElementsAre())
2015-03-20 05:21:14 +03:00
}
2015-03-20 05:24:02 +03:00
func (t *FlushFSTest) Directory() {
AssertTrue(false, "TODO")
}
2015-03-23 08:11:01 +03:00
////////////////////////////////////////////////////////////////////////
// No errors
////////////////////////////////////////////////////////////////////////
type NoErrorsTest struct {
flushFSTest
}
func init() { RegisterTestSuite(&NoErrorsTest{}) }
////////////////////////////////////////////////////////////////////////
// Flush error
////////////////////////////////////////////////////////////////////////
type FlushErrorTest struct {
flushFSTest
}
func init() { RegisterTestSuite(&FlushErrorTest{}) }
func (t *FlushFSTest) SetUp(ti *TestInfo)
////////////////////////////////////////////////////////////////////////
// Fsync error
////////////////////////////////////////////////////////////////////////
type FsyncErrorTest struct {
flushFSTest
}
func init() { RegisterTestSuite(&FsyncErrorTest{}) }
func (t *FlushFSTest) SetUp(ti *TestInfo)