fusego/samples/flushfs/flush_fs_test.go

732 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-24 00:51:19 +03:00
"bufio"
"encoding/hex"
2015-03-23 08:34:39 +03:00
"fmt"
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 05:44:34 +03:00
"syscall"
2015-03-20 03:17:05 +03:00
"testing"
2015-03-23 08:12:02 +03:00
"github.com/jacobsa/bazilfuse"
2015-03-20 03:17:05 +03:00
"github.com/jacobsa/fuse/samples"
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
////////////////////////////////////////////////////////////////////////
2015-03-23 08:12:02 +03:00
type flushFSTest struct {
2015-03-23 08:34:39 +03:00
samples.SubprocessTest
2015-03-20 03:17:05 +03:00
2015-03-24 00:42:39 +03:00
// Files to which mount_sample is writing reported flushes and fsyncs.
flushes *os.File
fsyncs *os.File
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:17:05 +03:00
}
2015-03-23 08:12:02 +03:00
func (t *flushFSTest) setUp(
ti *TestInfo,
flushErr bazilfuse.Errno,
fsyncErr bazilfuse.Errno) {
2015-03-20 03:21:38 +03:00
var err error
2015-03-23 08:34:39 +03:00
// Set up files to receive flush and fsync reports.
2015-03-24 00:42:39 +03:00
t.flushes, err = ioutil.TempFile("", "")
AssertEq(nil, err)
t.fsyncs, err = ioutil.TempFile("", "")
AssertEq(nil, err)
2015-03-20 03:52:54 +03:00
2015-03-23 08:34:39 +03:00
// Set up test config.
t.MountType = "flushfs"
t.MountFlags = []string{
"--flushfs.flushes_file",
t.flushes.Name(),
"--flushfs.fsyncs_file",
t.fsyncs.Name(),
2015-03-20 03:20:29 +03:00
2015-03-23 08:34:39 +03:00
"--flushfs.flush_error",
fmt.Sprintf("%d", int(flushErr)),
2015-03-20 03:21:38 +03:00
2015-03-23 08:34:39 +03:00
"--fsyncfs.fsync_error",
fmt.Sprintf("%d", int(fsyncErr)),
2015-03-20 03:21:38 +03:00
}
2015-03-23 08:34:39 +03:00
t.SubprocessTest.SetUp(ti)
2015-03-20 03:20:29 +03:00
}
2015-03-20 03:17:05 +03:00
2015-03-24 00:43:03 +03:00
func (t *flushFSTest) TearDown() {
2015-03-24 00:42:39 +03:00
// Unlink reporting files.
os.Remove(t.flushes.Name())
os.Remove(t.fsyncs.Name())
// Close reporting files.
t.flushes.Close()
t.fsyncs.Close()
// Close test files if non-nil.
2015-03-23 02:32:30 +03:00
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.
2015-03-24 00:43:03 +03:00
t.SubprocessTest.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-24 00:51:19 +03:00
func readReports(f *os.File) (reports []string, err error) {
// Seek the file to the start.
_, err = f.Seek(0, 0)
if err != nil {
err = fmt.Errorf("Seek: %v", err)
return
}
// We expect reports to end in a newline (including the final one).
reader := bufio.NewReader(f)
for {
var record []byte
record, err = reader.ReadBytes('\n')
if err == io.EOF {
if len(record) != 0 {
err = fmt.Errorf("Unexpected record:\n%s", hex.Dump(record))
return
}
err = nil
return
}
if err != nil {
err = fmt.Errorf("ReadBytes: %v", err)
return
}
// Strip the newline.
reports = append(reports, string(record[:len(record)-1]))
}
}
2015-03-24 00:45:01 +03:00
2015-03-20 03:25:15 +03:00
// Return a copy of the current contents of t.flushes.
2015-03-23 08:35:19 +03:00
func (t *flushFSTest) getFlushes() (p []string) {
2015-03-24 00:45:01 +03:00
var err error
if p, err = readReports(t.flushes); err != nil {
panic(err)
}
return
2015-03-20 03:26:32 +03:00
}
2015-03-20 03:25:15 +03:00
// Return a copy of the current contents of t.fsyncs.
2015-03-23 08:35:19 +03:00
func (t *flushFSTest) getFsyncs() (p []string) {
2015-03-24 00:45:01 +03:00
var err error
if p, err = readReports(t.fsyncs); err != nil {
panic(err)
}
return
2015-03-20 04:01:59 +03:00
}
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
////////////////////////////////////////////////////////////////////////
2015-03-24 00:39:22 +03:00
// No errors
2015-03-20 03:25:15 +03:00
////////////////////////////////////////////////////////////////////////
2015-03-24 00:39:22 +03:00
type NoErrorsTest struct {
flushFSTest
}
func init() { RegisterTestSuite(&NoErrorsTest{}) }
2015-03-24 00:52:05 +03:00
func (t *NoErrorsTest) SetUp(ti *TestInfo) {
const noErr = 0
t.flushFSTest.setUp(ti, noErr, noErr)
}
2015-03-24 00:39:22 +03:00
2015-03-24 00:40:35 +03:00
func (t *NoErrorsTest) Close_ReadWrite() {
2015-03-20 03:33:15 +03:00
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())
}
2015-03-24 00:40:35 +03:00
func (t *NoErrorsTest) Close_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
}
2015-03-24 00:40:35 +03:00
func (t *NoErrorsTest) Close_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
}
2015-03-24 00:40:35 +03:00
func (t *NoErrorsTest) Close_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-24 00:40:35 +03:00
func (t *NoErrorsTest) Close_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-24 00:40:35 +03:00
func (t *NoErrorsTest) Fsync() {
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-24 00:40:35 +03:00
func (t *NoErrorsTest) 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-24 00:40:35 +03:00
func (t *NoErrorsTest) 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-24 00:40:35 +03:00
func (t *NoErrorsTest) 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
}
2015-03-24 00:40:35 +03:00
func (t *NoErrorsTest) 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
2015-03-24 00:40:35 +03:00
func (t *NoErrorsTest) Directory() {
2015-03-20 05:24:02 +03:00
AssertTrue(false, "TODO")
}
2015-03-23 08:11:01 +03:00
////////////////////////////////////////////////////////////////////////
2015-03-24 00:39:22 +03:00
// Flush error
2015-03-23 08:11:01 +03:00
////////////////////////////////////////////////////////////////////////
2015-03-24 00:39:22 +03:00
type FlushErrorTest struct {
2015-03-23 08:11:01 +03:00
flushFSTest
}
2015-03-24 00:39:22 +03:00
func init() { RegisterTestSuite(&FlushErrorTest{}) }
2015-03-23 08:11:01 +03:00
2015-03-24 00:39:22 +03:00
func (t *FlushErrorTest) SetUp(ti *TestInfo)
2015-03-23 08:35:42 +03:00
2015-03-24 00:40:35 +03:00
func (t *FlushErrorTest) Close() {
2015-03-24 00:39:22 +03:00
var err error
2015-03-23 08:11:01 +03:00
2015-03-24 00:39:22 +03:00
// Open the file.
t.f1, err = os.OpenFile(path.Join(t.Dir, "foo"), os.O_RDWR, 0)
AssertEq(nil, err)
// Close the file.
err = t.f1.Close()
t.f1 = nil
AssertNe(nil, err)
ExpectThat(err, Error(HasSubstr("no such file")))
2015-03-23 08:11:01 +03:00
}
2015-03-24 00:40:35 +03:00
func (t *FlushErrorTest) Dup() {
2015-03-24 00:39:22 +03:00
var err error
2015-03-23 08:11:01 +03:00
2015-03-24 00:39:22 +03:00
// 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())
// 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")))
}
2015-03-24 00:40:35 +03:00
func (t *FlushErrorTest) Dup2() {
2015-03-24 00:39:22 +03:00
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)
// 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)
}
2015-03-23 08:11:01 +03:00
////////////////////////////////////////////////////////////////////////
// Fsync error
////////////////////////////////////////////////////////////////////////
type FsyncErrorTest struct {
flushFSTest
}
func init() { RegisterTestSuite(&FsyncErrorTest{}) }
2015-03-23 08:36:07 +03:00
func (t *FsyncErrorTest) SetUp(ti *TestInfo)
2015-03-24 00:39:22 +03:00
2015-03-24 00:40:35 +03:00
func (t *FsyncErrorTest) Fsync() {
2015-03-24 00:39:22 +03:00
var err error
// Open the file.
t.f1, err = os.OpenFile(path.Join(t.Dir, "foo"), os.O_RDWR, 0)
AssertEq(nil, err)
// Fsync.
err = t.f1.Sync()
AssertNe(nil, err)
ExpectThat(err, Error(HasSubstr("no such file")))
}