github.com/hanwen/go-fuse@v1.0.0/fuse/nodefs/handle_test.go (about) 1 // Copyright 2016 the Go-FUSE Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package nodefs 6 7 import ( 8 "strings" 9 "testing" 10 ) 11 12 func markSeen(t *testing.T, substr string) { 13 if r := recover(); r != nil { 14 s := r.(string) 15 if strings.Contains(s, substr) { 16 t.Log("expected recovery from: ", r) 17 } else { 18 panic(s) 19 } 20 } 21 } 22 23 func TestHandleMapLookupCount(t *testing.T) { 24 for _, portable := range []bool{true, false} { 25 t.Log("portable:", portable) 26 v := new(handled) 27 hm := newPortableHandleMap() 28 h1, g1 := hm.Register(v) 29 h2, g2 := hm.Register(v) 30 31 if h1 != h2 { 32 t.Fatalf("double register should reuse handle: got %d want %d.", h2, h1) 33 } 34 35 if g1 != g2 { 36 t.Fatalf("double register should reuse generation: got %d want %d.", g2, g1) 37 } 38 39 hm.Register(v) 40 41 forgotten, obj := hm.Forget(h1, 1) 42 if forgotten { 43 t.Fatalf("single forget unref forget object.") 44 } 45 46 if obj != v { 47 t.Fatalf("should return input object.") 48 } 49 50 if !hm.Has(h1) { 51 t.Fatalf("handlemap.Has() returned false for live object.") 52 } 53 54 forgotten, obj = hm.Forget(h1, 2) 55 if !forgotten { 56 t.Fatalf("unref did not forget object.") 57 } 58 59 if obj != v { 60 t.Fatalf("should return input object.") 61 } 62 63 if hm.Has(h1) { 64 t.Fatalf("handlemap.Has() returned false for live object.") 65 } 66 } 67 } 68 69 func TestHandleMapBasic(t *testing.T) { 70 v := new(handled) 71 hm := newPortableHandleMap() 72 h, _ := hm.Register(v) 73 t.Logf("Got handle 0x%x", h) 74 if !hm.Has(h) { 75 t.Fatal("Does not have handle") 76 } 77 if hm.Handle(v) != h { 78 t.Fatalf("handle mismatch, got %x want %x", hm.Handle(v), h) 79 } 80 if hm.Decode(h) != v { 81 t.Fatal("address mismatch") 82 } 83 if hm.Count() != 1 { 84 t.Fatal("count error") 85 } 86 hm.Forget(h, 1) 87 if hm.Count() != 0 { 88 t.Fatal("count error") 89 } 90 if hm.Has(h) { 91 t.Fatal("Still has handle") 92 } 93 } 94 95 func TestHandleMapMultiple(t *testing.T) { 96 hm := newPortableHandleMap() 97 for i := 0; i < 10; i++ { 98 v := &handled{} 99 h, _ := hm.Register(v) 100 if hm.Decode(h) != v { 101 t.Fatal("address mismatch") 102 } 103 if hm.Count() != i+1 { 104 t.Fatal("count error") 105 } 106 } 107 } 108 109 func TestHandleMapGeneration(t *testing.T) { 110 hm := newPortableHandleMap() 111 112 h1, g1 := hm.Register(&handled{}) 113 114 forgotten, _ := hm.Forget(h1, 1) 115 if !forgotten { 116 t.Fatalf("unref did not forget object.") 117 } 118 119 h2, g2 := hm.Register(&handled{}) 120 121 if h1 != h2 { 122 t.Fatalf("register should reuse handle: got %d want %d.", h2, h1) 123 } 124 125 if g1 >= g2 { 126 t.Fatalf("register should increase generation: got %d want greater than %d.", g2, g1) 127 } 128 } 129 130 func TestHandleMapGenerationKnown(t *testing.T) { 131 hm := newPortableHandleMap() 132 133 o1 := &handled{} 134 h1, g1 := hm.Register(o1) 135 136 o2 := &handled{} 137 h2, _ := hm.Register(o2) 138 139 h3, g3 := hm.Register(o1) 140 141 if h1 != h3 { 142 t.Fatalf("register known should reuse handle: got %d want %d.", h3, h1) 143 } 144 if g1 != g3 { 145 t.Fatalf("register known should reuse generation: got %d want %d.", g3, g1) 146 } 147 148 hm.Forget(h1, 2) 149 hm.Forget(h2, 1) 150 151 h1, g1 = hm.Register(o1) 152 h2, _ = hm.Register(o2) 153 h3, g3 = hm.Register(o1) 154 155 if h1 != h3 { 156 t.Fatalf("register known should reuse handle: got %d want %d.", h3, h1) 157 } 158 if g1 != g3 { 159 t.Fatalf("register known should reuse generation: got %d want %d.", g3, g1) 160 } 161 }