forked from hanwen/go-fuse
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: jiefenghuang <[email protected]>
- Loading branch information
1 parent
b58ceb4
commit ec040c7
Showing
1 changed file
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
//go:build linux | ||
// +build linux | ||
|
||
// Copyright 2024 the Go-FUSE Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package test | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/hanwen/go-fuse/v2/fuse" | ||
"github.com/hanwen/go-fuse/v2/fuse/nodefs" | ||
"github.com/hanwen/go-fuse/v2/internal/testutil" | ||
) | ||
|
||
type writebackFS struct { | ||
nodefs.Node | ||
writeCnt int | ||
} | ||
|
||
func newWritebackFS() *writebackFS { | ||
return &writebackFS{ | ||
Node: nodefs.NewDefaultNode(), | ||
} | ||
} | ||
|
||
func (fs *writebackFS) Open(flags uint32, context *fuse.Context) (file nodefs.File, code fuse.Status) { | ||
return nil, fuse.OK | ||
} | ||
|
||
func (fs *writebackFS) Write(file nodefs.File, data []byte, off int64, context *fuse.Context) (written uint32, code fuse.Status) { | ||
fs.writeCnt++ | ||
return uint32(len(data)), fuse.OK | ||
} | ||
|
||
func TestWriteback(t *testing.T) { | ||
dir := t.TempDir() | ||
|
||
root := nodefs.NewDefaultNode() | ||
opts := nodefs.NewOptions() | ||
opts.Debug = testutil.VerboseTest() | ||
|
||
mountOpts := &fuse.MountOptions{ | ||
EnableWriteback: true, | ||
} | ||
if opts != nil && opts.Debug { | ||
mountOpts.Debug = opts.Debug | ||
} | ||
|
||
srv, _, err := nodefs.Mount(dir, root, mountOpts, opts) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
fileName := "writeback.txt" | ||
node := newWritebackFS() | ||
root.Inode().NewChild(fileName, false, node) | ||
|
||
go srv.Serve() | ||
if err := srv.WaitMount(); err != nil { | ||
t.Fatal("WaitMount", err) | ||
} | ||
defer func() { | ||
err := srv.Unmount() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
}() | ||
|
||
path := filepath.Join(dir, fileName) | ||
f, err := os.OpenFile(path, os.O_WRONLY, 0644) | ||
if err != nil { | ||
t.Fatalf("OpenFile %s: %v", path, err) | ||
} | ||
defer f.Close() | ||
|
||
cnt := 10 | ||
for i := 0; i < cnt; i++ { | ||
if _, err = f.Write([]byte("1")); err != nil { | ||
t.Fatalf("Write: %v", err) | ||
} | ||
} | ||
|
||
if node.writeCnt >= cnt { | ||
t.Fatalf("write count: got %d; want < %d", node.writeCnt, cnt) | ||
} | ||
} |