Example #1
0
func TestDelFlush(t *testing.T) {
	cache.Init("localhost:11211")

	cache.Set("asdf", "1234", []string{"a", "xyz"})
	cache.Set("asdf", "2", "anything")
	cache.Set("x", "2", "1234")

	cache.Del("asdf", "1234")
	var foo []string
	found, err := cache.Get("asdf", "1234", &foo)
	if err != nil {
		t.Error("Accessing not set value should not trigger error")
	}
	if found {
		t.Error("cache.Del() did not delete value")
	}
	var str string
	found1, _ := cache.Get("asdf", "2", &str)
	found2, _ := cache.Get("x", "2", &str)
	if !found1 || !found2 {
		t.Error("cache.Del() deleted more values than it should")
	}

	cache.Flush()
	found1, _ = cache.Get("asdf", "2", &str)
	found2, _ = cache.Get("x", "2", &str)
	if found1 || found2 {
		t.Error("cache.Flush() did not flush all data")
	}
}
Example #2
0
func TestSetGet(t *testing.T) {
	cache.Init("localhost:11211")

	// Set:
	for i, val := range ints {
		cache.Set("ints", strconv.Itoa(i), val)
	}
	for i, val := range floats {
		cache.Set("floats", strconv.Itoa(i), val)
	}
	for i, val := range strings {
		cache.Set("strings", strconv.Itoa(i), val)
	}
	expectedStruct := TestStruct{0, -1.2345, "asdf.asdf_asdf234@@^&5", true}
	cache.Set("struct", "struct", expectedStruct)

	// Get:
	for i, expected := range ints {
		var actual int
		found, err := cache.Get("ints", strconv.Itoa(i), &actual)
		if err != nil {
			t.Error(err)
		}
		if !found {
			t.Errorf("Value was set but not found: %d", expected)
		}
		if actual != expected {
			t.Errorf("cache.Get() returned %d, expected %d", actual, expected)
		}
	}
	for i, expected := range floats {
		var actual float64
		found, err := cache.Get("floats", strconv.Itoa(i), &actual)
		if err != nil {
			t.Error(err)
		}
		if !found {
			t.Errorf("Value was set but not found: %d", expected)
		}
		if actual != expected {
			t.Errorf("cache.Get() returned %d, expected %d", actual, expected)
		}
	}
	for i, expected := range strings {
		var actual string
		found, err := cache.Get("strings", strconv.Itoa(i), &actual)
		if err != nil {
			t.Error(err)
		}
		if !found {
			t.Errorf("Value was set but not found: %d", expected)
		}
		if actual != expected {
			t.Errorf("cache.Get() returned %d, expected %d", actual, expected)
		}
	}
	actualStruct := TestStruct{}
	cache.Get("struct", "struct", &actualStruct)
	if actualStruct.A != expectedStruct.A {
		t.Errorf("cache.Get() returned %d, expected %d", actualStruct.A, expectedStruct.A)
	}
	if actualStruct.B != expectedStruct.B {
		t.Errorf("cache.Get() returned %d, expected %d", actualStruct.B, expectedStruct.B)
	}
	if actualStruct.C != expectedStruct.C {
		t.Errorf("cache.Get() returned %d, expected %d", actualStruct.C, expectedStruct.C)
	}
	if actualStruct.D != expectedStruct.D {
		t.Errorf("cache.Get() returned %d, expected %d", actualStruct.D, expectedStruct.D)
	}
}