File: bufferpool_test.go

package info (click to toggle)
golang-github-hanwen-go-fuse 2.8.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,584 kB
  • sloc: cpp: 78; sh: 47; makefile: 16
file content (102 lines) | stat: -rw-r--r-- 2,107 bytes parent folder | download | duplicates (2)
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
96
97
98
99
100
101
102
// Copyright 2016 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 fuse

import (
	"os"
	"testing"

	"github.com/hanwen/go-fuse/v2/internal/testutil"
)

func TestBufferPool(t *testing.T) {
	bp := bufferPool{}
	size := 1500
	buf1 := bp.AllocBuffer(uint32(size))
	if len(buf1) != size {
		t.Errorf("Expected buffer of %d bytes, got %d bytes", size, len(buf1))
	}
	bp.FreeBuffer(buf1)

	// tried testing to see if we get buf1 back if we ask again,
	// but it's not guaranteed and sometimes fails
}

type readFS struct {
	defaultRawFileSystem
}

func (fs *readFS) Open(cancel <-chan struct{}, input *OpenIn, out *OpenOut) (status Status) {
	if input.NodeId != 2 {
		return ENOENT
	}

	return OK
}

func (fs *readFS) Read(cancel <-chan struct{}, input *ReadIn, buf []byte) (ReadResult, Status) {
	if input.NodeId != 2 {
		return nil, ENOENT
	}

	dest := buf[:input.Size]
	for i := range dest {
		dest[i] = 'x'
	}

	return ReadResultData(dest), OK
}

func (f *readFS) Lookup(cancel <-chan struct{}, header *InHeader, name string, out *EntryOut) (code Status) {
	if name != "file" {
		return ENOENT
	}

	*out = EntryOut{
		NodeId: 2,
		Attr: Attr{
			Mode: S_IFREG | 0666,
			Size: 1 << 20,
		},
		AttrValid: 1,
	}

	return OK
}

func TestBufferPoolRequestHandler(t *testing.T) {
	mnt := t.TempDir()
	opts := &MountOptions{
		Debug: testutil.VerboseTest(),
	}

	rfs := readFS{}
	srv, err := NewServer(&rfs, mnt, opts)
	if err != nil {
		t.Fatal(err)
	}
	t.Cleanup(func() { srv.Unmount() })
	go srv.Serve()
	if err := srv.WaitMount(); err != nil {
		t.Fatal(err)
	}

	if _, err := os.ReadFile(mnt + "/file"); err != nil {
		t.Fatal(err)
	}

	// The last FreeBuffer happens after returning OK for the
	// read, so thread scheduling may cause it to happen after we
	// check.  Unmount to be sure we have finished all the work.
	srv.Unmount()
	ctr := srv.buffers.counters()
	for i, c := range ctr {
		if c != 0 {
			t.Errorf("page count %d: %d buffers outstanding", i, c)
		}
	}

	// TODO: test write as well?
}