go.undefinedlabs.com/scopeagent@v0.4.2/agent/cache_test.go (about) 1 package agent 2 3 import ( 4 "fmt" 5 "log" 6 "os" 7 "testing" 8 "time" 9 ) 10 11 func getTenant() interface{} { 12 return map[string]string{ 13 "key1": "value1", 14 "key2": fmt.Sprintf("%v", time.Now()), 15 } 16 } 17 18 func TestLocalCache(t *testing.T) { 19 20 tenant := getTenant() 21 22 cache := newLocalCache(tenant, cacheTimeout, true, log.New(os.Stdout, "", 0)) 23 loader := false 24 result := cache.GetOrSet("MyKey01", false, func(i interface{}, s string) interface{} { 25 loader = true 26 return "hello world" 27 }) 28 29 if !loader { 30 t.Fatal("loader has not been executed.") 31 } 32 if result.(string) != "hello world" { 33 t.Fatal("result was different than expected.") 34 } 35 36 cache2 := newLocalCache(tenant, cacheTimeout, true, log.New(os.Stdout, "", 0)) 37 loader = false 38 for i := 0; i < 10; i++ { 39 result = cache2.GetOrSet("MyKey01", false, func(i interface{}, s string) interface{} { 40 loader = true 41 return "hello world" 42 }) 43 44 if loader { 45 t.Fatal("loader has been executed.") 46 } 47 if result.(string) != "hello world" { 48 t.Fatal("result was different than expected.") 49 } 50 } 51 }