Exemplo n.º 1
0
func TestKeyUint64CacheFullOnPurge(t *testing.T) {
	cache := NewLRUCacheKeyUint64(1)
	value := &PurgeCacheValueKeyUint64{}
	k1 := key.KeyUint64(1)
	k2 := key.KeyUint64(2)

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

}
Exemplo n.º 2
0
func TestKeyUint64SetUpdatesSize(t *testing.T) {
	cache := NewLRUCacheKeyUint64(100)
	emptyValue := &CacheValue{0}
	k := key.KeyUint64(1)
	cache.Set(k, emptyValue)
	if _, sz, _ := cache.Stats(); sz != 0 {
		t.Errorf("cache.Size() = %v, expected 0", sz)
	}
	someValue := &CacheValue{20}
	k = key.KeyUint64(2)
	cache.Set(k, someValue)
	if _, sz, _ := cache.Stats(); sz != 20 {
		t.Errorf("cache.Size() = %v, expected 20", sz)
	}
}
Exemplo n.º 3
0
func TestKeyUint64ClearOnPurge(t *testing.T) {
	cache := NewLRUCacheKeyUint64(1)
	value := &PurgeCacheValueKeyUint64{}
	k1 := key.KeyUint64(1)
	purgeReasonFlag4TestKeyUint64 = PURGE_REASON_DELETE // init
	cache.Set(k1, value)                                // after this cache is full
	cache.Clear()
	if purgeReasonFlag4TestKeyUint64 != PURGE_REASON_CLEAR_ALL {
		t.Errorf("after cache.Delete ,purgeReason should be %d ,but get %d", PURGE_REASON_CLEAR_ALL, purgeReasonFlag4TestKeyUint64)
	}

}
Exemplo n.º 4
0
func TestKeyUint64OnMiss(t *testing.T) {
	fun := func(k key.KeyUint64) (Cacheable, bool) {
		return 1, true
	}
	cache := NewLRUCacheKeyUint64(1)
	cache.OnMiss(fun)
	k1 := key.KeyUint64(1)
	v, ok := cache.Get(k1) //
	if ok != true && v != 1 {
		t.Errorf("lru.onMiss is errror")
	}

}
Exemplo n.º 5
0
func TestKeyUint64SetWithOldKeyUpdatesValue(t *testing.T) {
	cache := NewLRUCacheKeyUint64(100)
	emptyValue := &CacheValue{0}
	k := key.KeyUint64(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)
	}
}
Exemplo n.º 6
0
func TestKeyUint64SetWithOldKeyUpdatesSize(t *testing.T) {
	cache := NewLRUCacheKeyUint64(100)
	emptyValue := &CacheValue{0}
	k := key.KeyUint64(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)
	}
}