github.com/hanwen/go-fuse@v1.0.0/unionfs/timedcache_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 unionfs 6 7 import ( 8 "testing" 9 "time" 10 ) 11 12 func TestTimedCacheUncacheable(t *testing.T) { 13 fetchCount := 0 14 fetch := func(n string) (interface{}, bool) { 15 fetchCount++ 16 i := int(n[0]) 17 return &i, false 18 } 19 20 cache := NewTimedCache(fetch, 0) 21 v := cache.Get("n").(*int) 22 w := cache.Get("n").(*int) 23 if *v != int('n') || *w != *v { 24 t.Errorf("value mismatch: got %d, %d want %d", *v, *w, int('n')) 25 } 26 27 if fetchCount != 2 { 28 t.Fatalf("Should have fetched twice: %d", fetchCount) 29 } 30 } 31 32 func TestTimedCache(t *testing.T) { 33 fetchCount := 0 34 fetch := func(n string) (interface{}, bool) { 35 fetchCount++ 36 i := int(n[0]) 37 return &i, true 38 } 39 40 // This fails with 1e6 on some Opteron CPUs. 41 ttl := 100 * time.Millisecond 42 43 cache := NewTimedCache(fetch, ttl) 44 v := cache.Get("n").(*int) 45 if *v != int('n') { 46 t.Errorf("value mismatch: got %d, want %d", *v, int('n')) 47 } 48 if fetchCount != 1 { 49 t.Errorf("fetch count mismatch: got %d want 1", fetchCount) 50 } 51 52 // The cache update is async. 53 time.Sleep(time.Duration(ttl / 10)) 54 55 w := cache.Get("n") 56 if v != w { 57 t.Errorf("Huh, inconsistent: 1st = %v != 2nd = %v", v, w) 58 } 59 60 if fetchCount > 1 { 61 t.Errorf("fetch count fail: %d > 1", fetchCount) 62 } 63 64 time.Sleep(time.Duration(ttl * 2)) 65 cache.Purge() 66 67 w = cache.Get("n") 68 if fetchCount == 1 { 69 t.Error("Did not fetch again. Purge unsuccessful?") 70 } 71 }