MCPcopy Create free account
hub / github.com/couchbase/moss / TestFileRef

Function TestFileRef

file_test.go:19–106  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

17)
18
19func TestFileRef(t *testing.T) {
20 tmpDir, _ := ioutil.TempDir("", "mossStore")
21 defer os.RemoveAll(tmpDir)
22
23 file, _ := os.OpenFile(path.Join(tmpDir, "test.mmap"),
24 os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
25
26 fref := &FileRef{file: file, refs: 1}
27
28 var m sync.Mutex
29 x := 0
30 after := 0
31 fref.OnBeforeClose(func() {
32 m.Lock()
33 x++
34 m.Unlock()
35 })
36 fref.OnAfterClose(func() {
37 m.Lock()
38 after++
39 m.Unlock()
40 })
41
42 file2 := fref.AddRef()
43
44 fref.m.Lock()
45 if fref.refs != 2 {
46 t.Errorf("expected 2 refs")
47 }
48 if fref.file != file2 {
49 t.Errorf("expected file == file2")
50 }
51 fref.m.Unlock()
52
53 fref.DecRef()
54
55 m.Lock()
56 if x != 0 {
57 t.Errorf("expected x 0")
58 }
59 if after != 0 {
60 t.Errorf("expected after 0")
61 }
62 m.Unlock()
63
64 fref.m.Lock()
65 if fref.refs != 1 {
66 t.Errorf("expected 1 refs")
67 }
68 if fref.file != file2 {
69 t.Errorf("expected file == file2")
70 }
71 fref.m.Unlock()
72
73 fref.DecRef()
74
75 m.Lock()
76 if x != 1 {

Callers

nothing calls this directly

Calls 6

OnBeforeCloseMethod · 0.95
OnAfterCloseMethod · 0.95
AddRefMethod · 0.95
DecRefMethod · 0.95
ToOsFileFunction · 0.85
CloseMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…