Ejemplo n.º 1
0
func TestCacheSetSuite3(t *testing.T) {
	cache := New(4)
	cache.Set(tools.NewStoredData([]byte("TEST"), "key"), 0, 0, 0)
	l := cache.list.Len()
	cache.Set(tools.NewStoredData([]byte("TEST"), "not_key"), 0, 0, 0)
	if cache.list.Len() != l {
		t.Fatalf("Error occured during appending of exceeding item.")
	}
}
Ejemplo n.º 2
0
func TestCacheSetSuite4(t *testing.T) {
	cache := New(4)
	cache.Set(tools.NewStoredData([]byte("TEST"), "key"), 0, 0, 0)
	if cache.Set(tools.NewStoredData([]byte("HUGE AMOUNT OF DATA"), "not_key"), 0, 0, 0) {
		t.Fatalf("Error occured during appending item of unappropriate size.")
	}
	if cache.list.Len() != 0 {
		t.Fatalf("Error occured during appending of exceeding item.")
	}
}
Ejemplo n.º 3
0
func TestOldestItem(t *testing.T) {
	cache := New(42)
	cache.Set(tools.NewStoredData([]byte("NOTOLD"), "key1"), 0, 0, 0)
	cache.Set(tools.NewStoredData([]byte("TEST"), "key2"), 0, 0, 0)
	cache.Set(tools.NewStoredData([]byte("OLD"), "key3"), 0, 0, 0)
	cache.Get("key1")
	cache.Get("key2")
	ts := cache.Oldest()
	if ts != cache.Get("key3").ts {
		t.Fatalf("Unexpected oldest value; expected timestamp %d, received %d", cache.Get("key3").ts, ts)
	}
}
Ejemplo n.º 4
0
func TestCacheGetSuite2(t *testing.T) {
	cache := New(10)
	cache.Set(tools.NewStoredData([]byte("TEST"), "key1"), 0, 0, 0)
	l_elem := cache.items["key1"].listElement
	cache.Set(tools.NewStoredData([]byte("TEST"), "key2"), 0, 0, 0)
	if l_elem == cache.list.Front() {
		t.Fatalf("Wrong list element position.")
	}
	cache.Get("key1")
	if l_elem != cache.list.Front() {
		t.Fatalf("Wrong list element position after retrieving.")
	}
}
Ejemplo n.º 5
0
func TestCacheFlushAll(t *testing.T) {
	cache := New(10)
	cache.Set(tools.NewStoredData([]byte("TEST"), "key1"), 0, 0, 0)
	cache.Set(tools.NewStoredData([]byte("TEST"), "key2"), 0, 0, 0)
	if cache.list.Len() != 2 {
		t.Fatalf("Error occurred during setting of elements.")
	}
	cache.FlushAll()
	if cache.list.Len() != 0 {
		t.Fatalf("Error occured during flushing all elements.")
	}

}
Ejemplo n.º 6
0
func TestCacheSetSuite2(t *testing.T) {
	cache := New(50)
	cache.Set(tools.NewStoredData([]byte("TEST1"), "key1"), 0, 0, 0)
	l_elem := cache.items["key1"].listElement
	cache.Set(tools.NewStoredData([]byte("TEST2"), "key2"), 0, 0, 0)
	l := cache.list.Len()
	if !cache.Set(tools.NewStoredData([]byte("CHANGED"), "key1"), 0, 0, 0) {
		t.Fatalf("Unexpected value.")
	}
	if cache.list.Len() != l {
		t.Fatalf("Error occured during updating of item.")
	}
	if l_elem != cache.list.Front() {
		t.Fatalf("Error occured during promoting of item.")
	}
}
Ejemplo n.º 7
0
// Implements set method
func (enum *Ascii_protocol_enum) set(storage *cache.LRUCache) (string, error) {
	if storage.Set(tools.NewStoredData(enum.data_string, enum.key[0]), enum.flags, enum.exptime, 0) {
		return STORED, nil
	} else {
		return strings.Replace(SERVER_ERROR_TEMP, "%s", "Not enough memory", 1), errors.New("SERVER_ERROR")
	}
}
Ejemplo n.º 8
0
func TestCacheGetSuite4(t *testing.T) {
	cache := New(10)
	cache.Set(tools.NewStoredData([]byte("TEST"), "key"), 0, 0, 0)
	res := cache.Get("key1")
	if res != nil {
		t.Fatalf("Unexpected value.", res)
	}
}
Ejemplo n.º 9
0
func TestCacheGetSuite3(t *testing.T) {
	cache := New(10)
	cache.Set(tools.NewStoredData([]byte("TEST"), "key"), 0, 1111111, 0) // Should be immediately expired
	res := cache.Get("key")
	if res != nil {
		t.Fatalf("Unexpected value.", res)
	}
}
Ejemplo n.º 10
0
func TestCacheSetSuite1(t *testing.T) {
	cache := New(50)
	if !cache.Set(tools.NewStoredData([]byte("TEST"), "key"), 0, 0, 0) {
		t.Fatalf("Unexpected value.")
	}
	if cache.list.Len() == 0 {
		t.Fatalf("Error occured during setting of element.")
	}
}
Ejemplo n.º 11
0
func TestCacheSetCasSuite(t *testing.T) {
	cache := New(10)
	cache.Set(tools.NewStoredData([]byte("TEST"), "key"), 0, 0, 0)
	if cache.SetCas("not_key", 424242) || !cache.SetCas("key", 424242) {
		t.Fatalf("Unexpected behavior")
	}
	if cache.Get("key").Cas_unique != 424242 {
		t.Fatalf("Cas unique wasn't set")
	}
}
Ejemplo n.º 12
0
func TestHandlingSuiteCas2(t *testing.T) {
	var storage = cache.New(42)
	if !storage.Set(tools.NewStoredData([]byte("test1"), "key"), 0, 0, 0) {
		t.Fatalf("Unexpecting behavior ")
	}
	var testEnum = Ascii_protocol_enum{"cas", []string{"key"}, 1, 0, 42, 424242, false, make([]byte, 42), ""}
	res, err := testEnum.HandleRequest(storage, nil)
	if err != nil || string(res) != NOT_FOUND {
		t.Fatalf("Unexpected returned values of handling: ", err, res)
	}
}
Ejemplo n.º 13
0
func TestCacheGetSuite1(t *testing.T) {
	cache := New(10)
	cache.Set(tools.NewStoredData([]byte("TEST"), "key"), 0, 0, 0)
	res := cache.Get("key")
	if res == nil {
		t.Fatalf("Unexpected value.", res)
	}
	extr := tools.ExtractStoredData(res.Cacheable)
	if string(extr) != "TEST" {
		t.Fatalf("Wrong returned value: %s", string(extr))
	}
}
Ejemplo n.º 14
0
func TestCacheFlushItemSuite2(t *testing.T) {
	cache := New(10)
	cache.Set(tools.NewStoredData([]byte("TEST"), "key1"), 0, 0, 0)
	if cache.list.Len() == 0 {
		t.Fatalf("Error occurred during setting of element.")
	}
	if cache.Flush("key2") {
		t.Fatalf("Unexpected result of flushing.")
	}
	if cache.list.Len() == 0 {
		t.Fatalf("The length of list was changed.")
	}
}
Ejemplo n.º 15
0
func TestCrawlerExpiring(t *testing.T) {
	cache := New(4242)
	crawler := cache.Crawler
	if crawler.SetSleep(10) != nil {
		t.Fatalf("Unexpected behavior")
	}
	crawler.ItemsPerRun = 10
	for i := 0; i < 100; i++ {
		cache.Set(tools.NewStoredData([]byte("TEST"), "key"+string(byte(i))), 0, 424242, 0)
	}
	for i := 0; i < 50; i++ {
		cache.Set(tools.NewStoredData([]byte("TEST"), "1key"+string(byte(i))), 0, 4242424242, 0)
	}
	err := cache.EnableCrawler()
	if err != nil {
		t.Fatalf("Unexpected behavior: crawler is disabled.", err)
	}
	time.Sleep(time.Millisecond * time.Duration(100))
	end_len := cache.list.Len()
	if end_len != 50 {
		t.Fatalf("Unexpected crawler's behavior: cache has %d items.", end_len)
	}
}
Ejemplo n.º 16
0
func TestCrawlerEnablingDisabling(t *testing.T) {
	cache := New(42)
	crawler := cache.Crawler
	err := cache.EnableCrawler()
	time.Sleep(time.Millisecond)
	if crawler.enabled || err == nil {
		t.Fatalf("Crawler enabled meanwhile items per run wasn't specified.")
	}
	crawler.ItemsPerRun = 10
	cache.Set(tools.NewStoredData([]byte("TEST"), "test"), 0, 0, 0)
	err = cache.EnableCrawler()
	time.Sleep(time.Millisecond)
	if !crawler.enabled || err != nil {
		t.Fatalf("Unexpected behavior: crawler is disabled.", err)
	}
	cache.DisableCrawler()
	if crawler.enabled {
		t.Fatalf("Unexpected behavior: crawler still runing.")
	}
}
Ejemplo n.º 17
0
// Utility method, for joining common parts of incr/decr methods.
// Receives additional param sign, which defines operation: -1 or 1
func (enum *Ascii_protocol_enum) fold(storage *cache.LRUCache, sign int) (string, error) {
	if item := storage.Get(enum.key[0]); item != nil && (sign == 1 || sign == -1) {
		existed_data := tools.ExtractStoredData(item.Cacheable)
		if existed_data != nil {
			evaluated_data_for_existed, err_for_existed := tools.StringToInt64(string(existed_data))
			evaluated_data_for_passed, err_for_passed := tools.StringToUInt64(string(enum.data_string))
			if err_for_existed == nil && err_for_passed == nil {
				var result string
				if sign > 0 {
					result = tools.IntToString(evaluated_data_for_existed + int64(evaluated_data_for_passed))
				} else {
					result = tools.IntToString(evaluated_data_for_existed - int64(evaluated_data_for_passed))
				}
				if storage.Set(tools.NewStoredData([]byte(result), enum.key[0]), item.Flags, item.Exptime, 0) {
					return result + "\r\n", nil
				}
				return strings.Replace(SERVER_ERROR_TEMP, "%s", "Not enough memory", 1), errors.New("SERVER_ERROR")
			}
			return ERROR_TEMP, nil
		}
	}
	return NOT_FOUND, nil
}