File: handle_test.go

package info (click to toggle)
golang-github-hanwen-go-fuse 0.0~git20161210.0.6c2b7d8-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 920 kB
  • ctags: 1,666
  • sloc: cpp: 78; sh: 77; makefile: 21
file content (131 lines) | stat: -rw-r--r-- 2,672 bytes parent folder | download
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// 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 nodefs

import (
	"strings"
	"testing"
)

func markSeen(t *testing.T, substr string) {
	if r := recover(); r != nil {
		s := r.(string)
		if strings.Contains(s, substr) {
			t.Log("expected recovery from: ", r)
		} else {
			panic(s)
		}
	}
}

func TestHandleMapLookupCount(t *testing.T) {
	for _, portable := range []bool{true, false} {
		t.Log("portable:", portable)
		v := new(handled)
		hm := newPortableHandleMap()
		h1, g1 := hm.Register(v)
		h2, g2 := hm.Register(v)

		if h1 != h2 {
			t.Fatalf("double register should reuse handle: got %d want %d.", h2, h1)
		}

		if g1 != g2 {
			t.Fatalf("double register should reuse generation: got %d want %d.", g2, g1)
		}

		hm.Register(v)

		forgotten, obj := hm.Forget(h1, 1)
		if forgotten {
			t.Fatalf("single forget unref forget object.")
		}

		if obj != v {
			t.Fatalf("should return input object.")
		}

		if !hm.Has(h1) {
			t.Fatalf("handlemap.Has() returned false for live object.")
		}

		forgotten, obj = hm.Forget(h1, 2)
		if !forgotten {
			t.Fatalf("unref did not forget object.")
		}

		if obj != v {
			t.Fatalf("should return input object.")
		}

		if hm.Has(h1) {
			t.Fatalf("handlemap.Has() returned false for live object.")
		}
	}
}

func TestHandleMapBasic(t *testing.T) {
	v := new(handled)
	hm := newPortableHandleMap()
	h, _ := hm.Register(v)
	t.Logf("Got handle 0x%x", h)
	if !hm.Has(h) {
		t.Fatal("Does not have handle")
	}
	if hm.Handle(v) != h {
		t.Fatalf("handle mismatch, got %x want %x", hm.Handle(v), h)
	}
	if hm.Decode(h) != v {
		t.Fatal("address mismatch")
	}
	if hm.Count() != 1 {
		t.Fatal("count error")
	}
	hm.Forget(h, 1)
	if hm.Count() != 0 {
		t.Fatal("count error")
	}
	if hm.Has(h) {
		t.Fatal("Still has handle")
	}
	if v.check != 0 {
		t.Errorf("forgotten object still has a check.")
	}
}

func TestHandleMapMultiple(t *testing.T) {
	hm := newPortableHandleMap()
	for i := 0; i < 10; i++ {
		v := &handled{}
		h, _ := hm.Register(v)
		if hm.Decode(h) != v {
			t.Fatal("address mismatch")
		}
		if hm.Count() != i+1 {
			t.Fatal("count error")
		}
	}
}

func TestHandleMapGeneration(t *testing.T) {
	hm := newPortableHandleMap()

	h1, g1 := hm.Register(&handled{})

	forgotten, _ := hm.Forget(h1, 1)
	if !forgotten {
		t.Fatalf("unref did not forget object.")
	}

	h2, g2 := hm.Register(&handled{})

	if h1 != h2 {
		t.Fatalf("register should reuse handle: got %d want %d.", h2, h1)
	}

	if g1 >= g2 {
		t.Fatalf("register should increase generation: got %d want greater than %d.", g2, g1)
	}
}