Debian Codegraph @

osfs/mmap_file_cleanup_test.go

raw symbols SCIP
1 //go:build linux
2
3 package osfs
4
5 import (
6 "bufio"
7 "fmt"
8 "os"
9 "path/filepath"
10 "runtime"
11 "strings"
12 "testing"
13
14 "github.com/stretchr/testify/require"
15 )
16
17 func TestRootOSMmapCleanup(t *testing.T) {
18 dir := t.TempDir()
19 path := filepath.Join(dir, "data")
20 _ = writePattern(t, path, 64*1024)
21
22 fs := newTestBoundOS(t, dir, WithMmap())
23 f, err := fs.Open("data")
24 require.NoError(t, err)
25
26 ok, err := isFileMapped(path)
27 require.NoError(t, err)
28 require.True(t, ok)
29
30 _ = f.Name()
31 f = nil
32 _ = f
33
34 for range 10 {
35 runtime.GC()
36
37 ok, err = isFileMapped(path)
38 require.NoError(t, err)
39 if !ok {
40 break
41 }
42 }
43
44 ok, err = isFileMapped(path)
45 require.NoError(t, err)
46 require.False(t, ok)
47 }
48
49 // isFileMapped checks if a path appears in the proc maps file. Only tested
50 // in Linux.
51 func isFileMapped(path string) (bool, error) {
52 pid := os.Getpid()
53
54 mapsPath := fmt.Sprintf("/proc/%d/maps", pid)
55 f, err := os.Open(mapsPath)
56 if err != nil {
57 return false, err
58 }
59 defer f.Close()
60
61 s := bufio.NewScanner(f)
62 for s.Scan() {
63 l := s.Text()
64 if strings.Contains(l, path) {
65 return true, nil
66 }
67 }
68
69 return false, s.Err()
70 }