Esempio n. 1
0
func TestKeyStringCacheFullOnPurge(t *testing.T) {
	cache := NewLRUCacheKeyString(1)
	value := &PurgeCacheValueKeyString{}
	k1 := key.String(1)
	k2 := key.String(2)

	cache.Set(k1, value)                                // after this cache is full
	purgeReasonFlag4TestKeyString = PURGE_REASON_DELETE // init
	cache.Set(k2, value)                                // after this k1 is delete and reason set to  PURGE_REASON_CACHEFULL
	if purgeReasonFlag4TestKeyString != PURGE_REASON_CACHEFULL {
		t.Errorf("after cache.Delete ,purgeReason should be %d ,but get %d", PURGE_REASON_CACHEFULL, purgeReasonFlag4TestKeyString)
	}

}
Esempio n. 2
0
func TestKeyStringLRUIsEvicted(t *testing.T) {
	size := int64(3)
	cache := NewLRUCacheKeyString(size)

	cache.Set(key.String("1"), &CacheValue{1})
	cache.Set(key.String("2"), &CacheValue{1})
	cache.Set(key.String("3"), &CacheValue{1})
	// lru: [k3, k2, k1]

	// Look up the elements. This will rearrange the LRU ordering.
	cache.Get(key.String("3"))
	// beforeKey2 := time.Now()
	cache.Get(key.String("2"))
	// afterKey2 := time.Now()
	cache.Get(key.String("1"))
	// lru: [k1, k2, k3]

	cache.Set(key.String("0"), &CacheValue{1})
	// lru: [k0, k1, k2]

	// The least recently used one should have been evicted.
	if _, ok := cache.Get(key.String("3")); ok {
		t.Error("Least recently used element was not evicted.")
	}

	// // Check oldest
	// if o := cache.Oldest(); o.Before(beforeKey2) || o.After(afterKey2) {
	// 	t.Errorf("cache.Oldest returned an unexpected value: got %v, expected a value between %v and %v", o, beforeKey2, afterKey2)
	// }
}
Esempio n. 3
0
func TestKeyStringSetUpdatesSize(t *testing.T) {
	cache := NewLRUCacheKeyString(100)
	emptyValue := &CacheValue{0}
	k := key.String(1)
	cache.Set(k, emptyValue)
	if _, sz, _ := cache.Stats(); sz != 0 {
		t.Errorf("cache.Size() = %v, expected 0", sz)
	}
	someValue := &CacheValue{20}
	k = key.String(2)
	cache.Set(k, someValue)
	if _, sz, _ := cache.Stats(); sz != 20 {
		t.Errorf("cache.Size() = %v, expected 20", sz)
	}
}
Esempio n. 4
0
func TestKeyStringGetNonExistent(t *testing.T) {
	cache := NewLRUCacheKeyString(100)

	if _, ok := cache.Get(key.String("1")); ok {
		t.Error("Cache returned a crap value after no inserts.")
	}
}
Esempio n. 5
0
func TestKeyStringCapacityIsObeyed(t *testing.T) {
	size := int64(3)
	cache := NewLRUCacheKeyString(100)
	cache.SetCapacity(size)
	value := &CacheValue{1}

	// Insert up to the cache's capacity.
	cache.Set(key.String("1"), value)
	cache.Set(key.String("2"), value)
	cache.Set(key.String("3"), value)
	if _, sz, _ := cache.Stats(); sz != size {
		t.Errorf("cache.Size() = %v, expected %v", sz, size)
	}
	// Insert one more; something should be evicted to make room.
	cache.Set(key.String("4"), value)
	if _, sz, _ := cache.Stats(); sz != size {
		t.Errorf("post-evict cache.Size() = %v, expected %v", sz, size)
	}

	// Check json stats
	data := cache.StatsJSON()
	m := make(map[string]interface{})
	if err := json.Unmarshal([]byte(data), &m); err != nil {
		t.Errorf("cache.StatsJSON() returned bad json data: %v %v", data, err)
	}
	if m["Size"].(float64) != float64(size) {
		t.Errorf("cache.StatsJSON() returned bad size: %v", m)
	}

	// Check various other stats
	if l := cache.Length(); l != size {
		t.Errorf("cache.StatsJSON() returned bad length: %v", l)
	}
	if s := cache.Size(); s != size {
		t.Errorf("cache.StatsJSON() returned bad size: %v", s)
	}
	if c := cache.Capacity(); c != size {
		t.Errorf("cache.StatsJSON() returned bad length: %v", c)
	}

	// checks StatsJSON on nil
	cache = nil
	if s := cache.StatsJSON(); s != "{}" {
		t.Errorf("cache.StatsJSON() on nil object returned %v", s)
	}
}
Esempio n. 6
0
func TestKeyStringClearOnPurge(t *testing.T) {
	cache := NewLRUCacheKeyString(1)
	value := &PurgeCacheValueKeyString{}
	k1 := key.String(1)
	purgeReasonFlag4TestKeyString = PURGE_REASON_DELETE // init
	cache.Set(k1, value)                                // after this cache is full
	cache.Clear()
	if purgeReasonFlag4TestKeyString != PURGE_REASON_CLEAR_ALL {
		t.Errorf("after cache.Delete ,purgeReason should be %d ,but get %d", PURGE_REASON_CLEAR_ALL, purgeReasonFlag4TestKeyString)
	}

}
Esempio n. 7
0
func TestKeyStringOnMiss(t *testing.T) {
	fun := func(k key.String) (Cacheable, bool) {
		return 1, true
	}
	cache := NewLRUCacheKeyString(1)
	cache.OnMiss(fun)
	k1 := key.String(1)
	v, ok := cache.Get(k1) //
	if ok != true && v != 1 {
		t.Errorf("lru.onMiss is errror")
	}

}
Esempio n. 8
0
func TestKeyStringSetWithOldKeyUpdatesValue(t *testing.T) {
	cache := NewLRUCacheKeyString(100)
	emptyValue := &CacheValue{0}
	k := key.String(1)
	cache.Set(k, emptyValue)
	someValue := &CacheValue{20}
	cache.Set(k, someValue)

	v, ok := cache.Get(k)
	if !ok || v.(*CacheValue) != someValue {
		t.Errorf("Cache has incorrect value: %v != %v", someValue, v)
	}
}
Esempio n. 9
0
func TestKeyStringSetWithOldKeyUpdatesSize(t *testing.T) {
	cache := NewLRUCacheKeyString(100)
	emptyValue := &CacheValue{0}
	k := key.String(1)
	cache.Set(k, emptyValue)

	if _, sz, _ := cache.Stats(); sz != 0 {
		t.Errorf("cache.Size() = %v, expected %v", sz, 0)
	}

	someValue := &CacheValue{20}
	cache.Set(k, someValue)
	expected := int64(someValue.size)
	if _, sz, _ := cache.Stats(); sz != expected {
		t.Errorf("cache.Size() = %v, expected %v", sz, expected)
	}
}