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"
|
|
|
|
"os"
|
|
|
|
"path"
|
2015-03-20 03:20:29 +03:00
|
|
|
"sync"
|
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-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-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-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.
|
|
|
|
f, 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"))
|
|
|
|
AssertEq(nil, err)
|
|
|
|
AssertEq(4, n)
|
|
|
|
|
|
|
|
// Seek and read them back.
|
|
|
|
off, err = f.Seek(0, 0)
|
|
|
|
AssertEq(nil, err)
|
2015-03-20 03:49:45 +03:00
|
|
|
AssertEq(0, off)
|
2015-03-20 03:33:15 +03:00
|
|
|
|
|
|
|
n, err = f.Read(buf)
|
|
|
|
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.
|
|
|
|
err = f.Close()
|
|
|
|
f = nil
|
|
|
|
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.
|
|
|
|
f, 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
|
|
|
|
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.
|
|
|
|
f, 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"))
|
|
|
|
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.
|
|
|
|
err = f.Close()
|
|
|
|
f = nil
|
|
|
|
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() {
|
2015-03-20 03:56:38 +03:00
|
|
|
var n int
|
|
|
|
var err error
|
|
|
|
|
|
|
|
// Open the file.
|
|
|
|
f, 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"))
|
|
|
|
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.
|
|
|
|
err = f.Close()
|
|
|
|
f = 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"))
|
2015-03-20 03:56:38 +03:00
|
|
|
AssertThat(t.getFsyncs(), ElementsAre())
|
|
|
|
|
|
|
|
// Open the file again.
|
|
|
|
f, 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"))
|
|
|
|
AssertEq(nil, err)
|
|
|
|
AssertEq(1, n)
|
|
|
|
|
2015-03-20 03:57:21 +03:00
|
|
|
AssertThat(t.getFlushes(), ElementsAre("taco"))
|
2015-03-20 03:56:38 +03:00
|
|
|
AssertThat(t.getFsyncs(), ElementsAre())
|
|
|
|
|
|
|
|
// Close the file. Now the new contents should be flushed.
|
|
|
|
err = f.Close()
|
|
|
|
f = nil
|
|
|
|
AssertEq(nil, err)
|
|
|
|
|
2015-03-20 03:57:21 +03:00
|
|
|
AssertThat(t.getFlushes(), ElementsAre("taco", "paco"))
|
2015-03-20 03:56:38 +03:00
|
|
|
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() {
|
2015-03-20 03:59:52 +03:00
|
|
|
var n int
|
|
|
|
var err error
|
|
|
|
|
|
|
|
// Open the file with two handles.
|
|
|
|
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)
|
|
|
|
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"))
|
|
|
|
AssertEq(nil, err)
|
|
|
|
AssertEq(4, n)
|
|
|
|
|
|
|
|
n, err = 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.
|
|
|
|
err = f1.Close()
|
|
|
|
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"))
|
|
|
|
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.
|
|
|
|
err = f2.Close()
|
|
|
|
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 04:45:12 +03:00
|
|
|
func (t *FlushFSTest) CloseReports_DuplicatedFileDescriptor() {
|
|
|
|
AssertTrue(false, "TODO")
|
|
|
|
}
|
|
|
|
|
2015-03-20 03:33:15 +03:00
|
|
|
func (t *FlushFSTest) CloseError() {
|
2015-03-20 04:03:25 +03:00
|
|
|
// Open the file.
|
2015-03-20 04:01:59 +03:00
|
|
|
f, err := os.OpenFile(path.Join(t.Dir, "foo"), os.O_RDWR, 0)
|
|
|
|
AssertEq(nil, err)
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if f != nil {
|
|
|
|
ExpectEq(nil, f.Close())
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Configure a flush error.
|
|
|
|
t.setFlushError(fuse.ENOENT)
|
|
|
|
|
|
|
|
// Close the file.
|
|
|
|
err = f.Close()
|
|
|
|
f = nil
|
|
|
|
|
|
|
|
AssertNe(nil, err)
|
|
|
|
ExpectThat(err, Error(HasSubstr("TODO")))
|
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.
|
|
|
|
f, 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"))
|
|
|
|
AssertEq(nil, err)
|
|
|
|
AssertEq(4, n)
|
|
|
|
|
|
|
|
AssertThat(t.getFlushes(), ElementsAre())
|
|
|
|
AssertThat(t.getFsyncs(), ElementsAre())
|
|
|
|
|
|
|
|
// Fsync.
|
|
|
|
err = f.Sync()
|
|
|
|
AssertEq(nil, err)
|
|
|
|
|
|
|
|
AssertThat(t.getFlushes(), ElementsAre())
|
|
|
|
AssertThat(t.getFsyncs(), ElementsAre("taco"))
|
|
|
|
|
|
|
|
// Write some more contents.
|
|
|
|
n, err = f.Write([]byte("s"))
|
|
|
|
AssertEq(nil, err)
|
|
|
|
AssertEq(1, n)
|
|
|
|
|
|
|
|
AssertThat(t.getFlushes(), ElementsAre())
|
|
|
|
AssertThat(t.getFsyncs(), ElementsAre("taco"))
|
|
|
|
|
|
|
|
// Fsync.
|
|
|
|
err = f.Sync()
|
|
|
|
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-20 04:04:25 +03:00
|
|
|
// Open the file.
|
|
|
|
f, err := os.OpenFile(path.Join(t.Dir, "foo"), os.O_RDWR, 0)
|
|
|
|
AssertEq(nil, err)
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if f != nil {
|
|
|
|
ExpectEq(nil, f.Close())
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Configure an fsync error.
|
|
|
|
t.setFsyncError(fuse.ENOENT)
|
|
|
|
|
|
|
|
// Fsync.
|
|
|
|
err = f.Sync()
|
|
|
|
|
|
|
|
AssertNe(nil, err)
|
|
|
|
ExpectThat(err, Error(HasSubstr("TODO")))
|
2015-03-20 03:17:05 +03:00
|
|
|
}
|
2015-03-20 05:21:14 +03:00
|
|
|
|
|
|
|
func (t *FlushFSTest) Dup() {
|
|
|
|
AssertTrue(false, "TODO")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *FlushFSTest) Dup_CloseError() {
|
|
|
|
AssertTrue(false, "TODO")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *FlushFSTest) Dup2() {
|
|
|
|
AssertTrue(false, "TODO")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *FlushFSTest) Dup2_CloseError() {
|
|
|
|
AssertTrue(false, "TODO")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *FlushFSTest) Mmap() {
|
|
|
|
AssertTrue(false, "TODO")
|
|
|
|
}
|
2015-03-20 05:24:02 +03:00
|
|
|
|
|
|
|
func (t *FlushFSTest) Directory() {
|
|
|
|
AssertTrue(false, "TODO")
|
|
|
|
}
|