-
Notifications
You must be signed in to change notification settings - Fork 0
/
singleopen_test.go
95 lines (85 loc) · 1.67 KB
/
singleopen_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package singleopen
import (
"io/fs"
"testing"
"testing/fstest"
)
func TestFS(t *testing.T) {
mustOpen := func(fsys fs.FS, name string) fs.File {
t.Helper()
f, err := fsys.Open(name)
if err != nil {
t.Fatal(err)
}
return f
}
mustClose := func(f fs.File) {
t.Helper()
err := f.Close()
if err != nil {
t.Fatal(err)
}
}
wantRefCount := func(f fs.File, want int) {
t.Helper()
got := f.(*fileReaderAt).refc
if got != want {
t.Errorf("got ref count %d, want: %d", got, want)
}
}
sameFile := func(f1, f2 fs.File) bool {
return f1.(*fileReaderAt).file == f2.(*fileReaderAt).file
}
fsys := &FS{FS: fstest.MapFS{
"sub/dir/file1": &fstest.MapFile{},
}}
if err := fstest.TestFS(fsys, "sub/dir/file1"); err != nil {
t.Fatal(err)
}
f1 := mustOpen(fsys, "sub/dir/file1")
// t.Fatal("\n" + spew.Sdump(f1))
f2 := mustOpen(fsys, "sub/dir/file1")
wantRefCount(f1, 2)
if !sameFile(f1, f2) {
t.Error("f1 != f2")
}
fsys.KeepLast(8)
mustClose(f1)
mustClose(f2)
wantRefCount(f1, 0)
sub, err := fs.Sub(fsys, "sub")
if err != nil {
t.Fatal(err)
}
f3 := mustOpen(sub, "dir/file1")
f4 := mustOpen(sub, "dir/file1")
if !sameFile(f3, f1) {
t.Error("f3 != f1")
}
if !sameFile(f3, f4) {
t.Error("f3 != f4")
}
wantRefCount(f3, 2)
mustClose(f3)
subdir, err := fs.Sub(sub, "dir")
if err != nil {
t.Fatal(err)
}
f5 := mustOpen(subdir, "file1")
f6 := mustOpen(subdir, "file1")
wantRefCount(f3, 3)
if !sameFile(f5, f6) {
t.Error("f5 != f6")
}
if !sameFile(f5, f4) {
t.Error("f5 != f4")
}
fsys.KeepLast(0)
mustClose(f4)
mustClose(f5)
mustClose(f6)
wantRefCount(f3, 0)
if f3.Close() != fs.ErrClosed {
t.Error("file is not closed")
}
}