| 1 | package gcachescip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/ |
| 2 | |
| 3 | import ( |
| 4 | "fmtscip-go gomod github.com/golang/go/src go1.15 fmt/" |
| 5 | "testingscip-go gomod github.com/golang/go/src go1.15 testing/" |
| 6 | "timescip-go gomod github.com/golang/go/src go1.15 time/" |
| 7 | ) |
| 8 | |
| 9 | func loaderscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/loader().(key interface{}) (interface{}, error) { |
| 10 | return fmtscip-go gomod github.com/golang/go/src go1.15 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.15 fmt/Sprintf().("valueFor%s", key), nil |
| 11 | } |
| 12 | |
| 13 | func testSetCachescip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/testSetCache().(t *testingscip-go gomod github.com/golang/go/src go1.15 testing/.Tscip-go gomod github.com/golang/go/src go1.15 testing/T#, gc Cachescip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/Cache#, numbers int) { |
| 14 | for i := 0; i < numbers; i++ { |
| 15 | key := fmtscip-go gomod github.com/golang/go/src go1.15 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.15 fmt/Sprintf().("Key-%d", i) |
| 16 | value, err := loaderscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/loader().(key) |
| 17 | if err != nil { |
| 18 | t.Errorscip-go gomod github.com/golang/go/src go1.15 testing/common#Error().(err) |
| 19 | return |
| 20 | } |
| 21 | gc.Setscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/Cache#Set.(key, value) |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | func testGetCachescip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/testGetCache().(t *testingscip-go gomod github.com/golang/go/src go1.15 testing/.Tscip-go gomod github.com/golang/go/src go1.15 testing/T#, gc Cachescip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/Cache#, numbers int) { |
| 26 | for i := 0; i < numbers; i++ { |
| 27 | key := fmtscip-go gomod github.com/golang/go/src go1.15 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.15 fmt/Sprintf().("Key-%d", i) |
| 28 | v, err := gc.Getscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/Cache#Get.(key) |
| 29 | if err != nil { |
| 30 | t.Errorfscip-go gomod github.com/golang/go/src go1.15 testing/common#Errorf().("Unexpected error: %v", err) |
| 31 | } |
| 32 | expectedV, _ := loaderscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/loader().(key) |
| 33 | if v != expectedV { |
| 34 | t.Errorfscip-go gomod github.com/golang/go/src go1.15 testing/common#Errorf().("Expected value is %v, not %v", expectedV, v) |
| 35 | } |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | func testGetIFPresentscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/testGetIFPresent().(t *testingscip-go gomod github.com/golang/go/src go1.15 testing/.Tscip-go gomod github.com/golang/go/src go1.15 testing/T#, evT string) { |
| 40 | cache := |
| 41 | Newscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/New().(8). |
| 42 | EvictTypescip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/CacheBuilder#EvictType().(evT). |
| 43 | LoaderFuncscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/CacheBuilder#LoaderFunc().( |
| 44 | func(key interface{}) (interface{}, error) { |
| 45 | return "value", nil |
| 46 | }). |
| 47 | Buildscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/CacheBuilder#Build().() |
| 48 | |
| 49 | v, err := cache.GetIFPresentscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/Cache#GetIFPresent.("key") |
| 50 | if err != KeyNotFoundErrorscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/KeyNotFoundError. { |
| 51 | t.Errorfscip-go gomod github.com/golang/go/src go1.15 testing/common#Errorf().("err should not be %v", err) |
| 52 | } |
| 53 | |
| 54 | timescip-go gomod github.com/golang/go/src go1.15 time/.Sleepscip-go gomod github.com/golang/go/src go1.15 time/Sleep().(2 * timescip-go gomod github.com/golang/go/src go1.15 time/.Millisecondscip-go gomod github.com/golang/go/src go1.15 time/Millisecond.) |
| 55 | |
| 56 | v, err = cache.GetIFPresentscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/Cache#GetIFPresent.("key") |
| 57 | if err != nil { |
| 58 | t.Errorfscip-go gomod github.com/golang/go/src go1.15 testing/common#Errorf().("err should not be %v", err) |
| 59 | } |
| 60 | if v != "value" { |
| 61 | t.Errorfscip-go gomod github.com/golang/go/src go1.15 testing/common#Errorf().("v should not be %v", v) |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | func setItemsByRangescip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/setItemsByRange().(t *testingscip-go gomod github.com/golang/go/src go1.15 testing/.Tscip-go gomod github.com/golang/go/src go1.15 testing/T#, c Cachescip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/Cache#, start, end int) { |
| 66 | for i := start; i < end; i++ { |
| 67 | if err := c.Setscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/Cache#Set.(i, i); err != nil { |
| 68 | t.Errorscip-go gomod github.com/golang/go/src go1.15 testing/common#Error().(err) |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | func keysToMapscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/keysToMap().(keys []interface{}) map[interface{}]struct{} { |
| 74 | m := make(map[interface{}]struct{}, len(keys)) |
| 75 | for _, k := range keys { |
| 76 | m[k] = struct{}{} |
| 77 | } |
| 78 | return m |
| 79 | } |
| 80 | |
| 81 | func checkItemsByRangescip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/checkItemsByRange().(t *testingscip-go gomod github.com/golang/go/src go1.15 testing/.Tscip-go gomod github.com/golang/go/src go1.15 testing/T#, keys []interface{}, m map[interface{}]interface{}, size, start, end int) { |
| 82 | if len(keys) != size { |
| 83 | t.Fatalfscip-go gomod github.com/golang/go/src go1.15 testing/common#Fatalf().("%v != %v", len(keys), size) |
| 84 | } else if len(m) != size { |
| 85 | t.Fatalfscip-go gomod github.com/golang/go/src go1.15 testing/common#Fatalf().("%v != %v", len(m), size) |
| 86 | } |
| 87 | km := keysToMapscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/keysToMap().(keys) |
| 88 | for i := start; i < end; i++ { |
| 89 | if _, ok := km[i]; !ok { |
| 90 | t.Errorfscip-go gomod github.com/golang/go/src go1.15 testing/common#Errorf().("keys should contain %v", i) |
| 91 | } |
| 92 | v, ok := m[i] |
| 93 | if !ok { |
| 94 | t.Errorfscip-go gomod github.com/golang/go/src go1.15 testing/common#Errorf().("m should contain %v", i) |
| 95 | continue |
| 96 | } |
| 97 | if v.(int) != i { |
| 98 | t.Errorfscip-go gomod github.com/golang/go/src go1.15 testing/common#Errorf().("%v != %v", v, i) |
| 99 | continue |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | func testExpiredItemsscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/testExpiredItems().(t *testingscip-go gomod github.com/golang/go/src go1.15 testing/.Tscip-go gomod github.com/golang/go/src go1.15 testing/T#, evT string) { |
| 105 | size := 8 |
| 106 | cache := |
| 107 | Newscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/New().(size). |
| 108 | Expirationscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/CacheBuilder#Expiration().(timescip-go gomod github.com/golang/go/src go1.15 time/.Millisecondscip-go gomod github.com/golang/go/src go1.15 time/Millisecond.). |
| 109 | EvictTypescip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/CacheBuilder#EvictType().(evT). |
| 110 | Buildscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/CacheBuilder#Build().() |
| 111 | |
| 112 | setItemsByRangescip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/setItemsByRange().(t, cache, 0, size) |
| 113 | checkItemsByRangescip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/checkItemsByRange().(t, cache.Keysscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/Cache#Keys.(true), cache.GetALLscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/Cache#GetALL.(true), cache.Lenscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/Cache#Len.(true), 0, size) |
| 114 | |
| 115 | timescip-go gomod github.com/golang/go/src go1.15 time/.Sleepscip-go gomod github.com/golang/go/src go1.15 time/Sleep().(timescip-go gomod github.com/golang/go/src go1.15 time/.Millisecondscip-go gomod github.com/golang/go/src go1.15 time/Millisecond.) |
| 116 | |
| 117 | checkItemsByRangescip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/checkItemsByRange().(t, cache.Keysscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/Cache#Keys.(false), cache.GetALLscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/Cache#GetALL.(false), cache.Lenscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/Cache#Len.(false), 0, size) |
| 118 | |
| 119 | if l := cache.Lenscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/Cache#Len.(true); l != 0 { |
| 120 | t.Fatalfscip-go gomod github.com/golang/go/src go1.15 testing/common#Fatalf().("GetALL should returns no items, but got length %v", l) |
| 121 | } |
| 122 | |
| 123 | cache.Setscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/Cache#Set.(1, 1) |
| 124 | m := cache.GetALLscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/Cache#GetALL.(true) |
| 125 | if len(m) != 1 { |
| 126 | t.Fatalfscip-go gomod github.com/golang/go/src go1.15 testing/common#Fatalf().("%v != %v", len(m), 1) |
| 127 | } else if l := cache.Lenscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/Cache#Len.(true); l != 1 { |
| 128 | t.Fatalfscip-go gomod github.com/golang/go/src go1.15 testing/common#Fatalf().("%v != %v", l, 1) |
| 129 | } |
| 130 | if m[1] != 1 { |
| 131 | t.Fatalfscip-go gomod github.com/golang/go/src go1.15 testing/common#Fatalf().("%v != %v", m[1], 1) |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | func getSimpleEvictedFuncscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/getSimpleEvictedFunc().(t *testingscip-go gomod github.com/golang/go/src go1.15 testing/.Tscip-go gomod github.com/golang/go/src go1.15 testing/T#) func(interface{}, interface{}) { |
| 136 | return func(key, value interface{}) { |
| 137 | t.Logfscip-go gomod github.com/golang/go/src go1.15 testing/common#Logf().("Key=%v Value=%v will be evicted.\n", key, value) |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | func buildTestCachescip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/buildTestCache().(t *testingscip-go gomod github.com/golang/go/src go1.15 testing/.Tscip-go gomod github.com/golang/go/src go1.15 testing/T#, tp string, size int) Cachescip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/Cache# { |
| 142 | return Newscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/New().(size). |
| 143 | EvictTypescip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/CacheBuilder#EvictType().(tp). |
| 144 | EvictedFuncscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/CacheBuilder#EvictedFunc().(getSimpleEvictedFuncscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/getSimpleEvictedFunc().(t)). |
| 145 | Buildscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/CacheBuilder#Build().() |
| 146 | } |
| 147 | |
| 148 | func buildTestLoadingCachescip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/buildTestLoadingCache().(t *testingscip-go gomod github.com/golang/go/src go1.15 testing/.Tscip-go gomod github.com/golang/go/src go1.15 testing/T#, tp string, size int, loader LoaderFuncscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/LoaderFunc#) Cachescip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/Cache# { |
| 149 | return Newscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/New().(size). |
| 150 | EvictTypescip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/CacheBuilder#EvictType().(tp). |
| 151 | LoaderFuncscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/CacheBuilder#LoaderFunc().(loader). |
| 152 | EvictedFuncscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/CacheBuilder#EvictedFunc().(getSimpleEvictedFuncscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/getSimpleEvictedFunc().(t)). |
| 153 | Buildscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/CacheBuilder#Build().() |
| 154 | } |
| 155 | |
| 156 | func buildTestLoadingCacheWithExpirationscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/buildTestLoadingCacheWithExpiration().(t *testingscip-go gomod github.com/golang/go/src go1.15 testing/.Tscip-go gomod github.com/golang/go/src go1.15 testing/T#, tp string, size int, ep timescip-go gomod github.com/golang/go/src go1.15 time/.Durationscip-go gomod github.com/golang/go/src go1.15 time/Duration#) Cachescip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/Cache# { |
| 157 | return Newscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/New().(size). |
| 158 | EvictTypescip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/CacheBuilder#EvictType().(tp). |
| 159 | Expirationscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/CacheBuilder#Expiration().(ep). |
| 160 | LoaderFuncscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/CacheBuilder#LoaderFunc().(loaderscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/loader().). |
| 161 | EvictedFuncscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/CacheBuilder#EvictedFunc().(getSimpleEvictedFuncscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/getSimpleEvictedFunc().(t)). |
| 162 | Buildscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/CacheBuilder#Build().() |
| 163 | } |