|
|
@ -25,6 +25,44 @@ import ( |
|
|
|
"time" |
|
|
|
"time" |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// TestObjectCache tests cases of object cache with expiry.
|
|
|
|
|
|
|
|
func TestObjExpiry(t *testing.T) { |
|
|
|
|
|
|
|
// Non exhaustive list of all object cache behavior cases.
|
|
|
|
|
|
|
|
testCases := []struct { |
|
|
|
|
|
|
|
expiry time.Duration |
|
|
|
|
|
|
|
cacheSize uint64 |
|
|
|
|
|
|
|
err error |
|
|
|
|
|
|
|
closeErr error |
|
|
|
|
|
|
|
}{ |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
expiry: 100 * time.Millisecond, |
|
|
|
|
|
|
|
cacheSize: 1024, |
|
|
|
|
|
|
|
err: ErrKeyNotFoundInCache, |
|
|
|
|
|
|
|
closeErr: nil, |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Test case 1 validates running of GC.
|
|
|
|
|
|
|
|
testCase := testCases[0] |
|
|
|
|
|
|
|
cache := New(testCase.cacheSize, testCase.expiry) |
|
|
|
|
|
|
|
cache.OnEviction = func(key string) {} |
|
|
|
|
|
|
|
w, err := cache.Create("test", 1) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
t.Errorf("Test case 1 expected to pass, failed instead %s", err) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
// Write a byte.
|
|
|
|
|
|
|
|
w.Write([]byte("1")) |
|
|
|
|
|
|
|
if err = w.Close(); err != nil { |
|
|
|
|
|
|
|
t.Errorf("Test case 1 expected to pass, failed instead %s", err) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
// Wait for 500 millisecond.
|
|
|
|
|
|
|
|
time.Sleep(500 * time.Millisecond) |
|
|
|
|
|
|
|
_, err = cache.Open("test") |
|
|
|
|
|
|
|
if err != testCase.err { |
|
|
|
|
|
|
|
t.Errorf("Test case 1 expected %s, got instead %s", testCase.err, err) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// TestObjCache - tests various cases for object cache behavior.
|
|
|
|
// TestObjCache - tests various cases for object cache behavior.
|
|
|
|
func TestObjCache(t *testing.T) { |
|
|
|
func TestObjCache(t *testing.T) { |
|
|
|
// Non exhaustive list of all object cache behavior cases.
|
|
|
|
// Non exhaustive list of all object cache behavior cases.
|
|
|
@ -68,6 +106,12 @@ func TestObjCache(t *testing.T) { |
|
|
|
expiry: NoExpiry, |
|
|
|
expiry: NoExpiry, |
|
|
|
cacheSize: 1024, |
|
|
|
cacheSize: 1024, |
|
|
|
}, |
|
|
|
}, |
|
|
|
|
|
|
|
// Validate error excess data.
|
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
expiry: NoExpiry, |
|
|
|
|
|
|
|
cacheSize: 5, |
|
|
|
|
|
|
|
closeErr: ErrExcessData, |
|
|
|
|
|
|
|
}, |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Test 1 validating Open failure.
|
|
|
|
// Test 1 validating Open failure.
|
|
|
@ -169,4 +213,27 @@ func TestObjCache(t *testing.T) { |
|
|
|
if deleteKey != "test" { |
|
|
|
if deleteKey != "test" { |
|
|
|
t.Errorf("Test case 6 expected to pass, wanted \"test\", got %s", deleteKey) |
|
|
|
t.Errorf("Test case 6 expected to pass, wanted \"test\", got %s", deleteKey) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Test 7 validates rejecting requests when excess data is being saved.
|
|
|
|
|
|
|
|
testCase = testCases[6] |
|
|
|
|
|
|
|
cache = New(testCase.cacheSize, testCase.expiry) |
|
|
|
|
|
|
|
w, err = cache.Create("test1", 5) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
t.Errorf("Test case 7 expected to pass, failed instead %s", err) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
// Write '5' bytes.
|
|
|
|
|
|
|
|
w.Write([]byte("Hello")) |
|
|
|
|
|
|
|
// Close to successfully save into cache.
|
|
|
|
|
|
|
|
if err = w.Close(); err != nil { |
|
|
|
|
|
|
|
t.Errorf("Test case 7 expected to pass, failed instead %s", err) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
w, err = cache.Create("test2", 1) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
t.Errorf("Test case 7 expected to pass, failed instead %s", err) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
// Write '1' byte.
|
|
|
|
|
|
|
|
w.Write([]byte("H")) |
|
|
|
|
|
|
|
if err = w.Close(); err != testCase.closeErr { |
|
|
|
|
|
|
|
t.Errorf("Test case 7 expected to fail, passed instead") |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|