pkg/objcache: Add more tests. (#2371)

master
Harshavardhana 8 years ago committed by GitHub
parent 8c2985a9f5
commit b23605a2b5
  1. 67
      pkg/objcache/objcache_test.go
  2. 24
      pkg/quick/quick_test.go

@ -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")
}
} }

@ -51,13 +51,22 @@ func (s *MySuite) TestCheckData(c *C) {
err := quick.CheckData(nil) err := quick.CheckData(nil)
c.Assert(err, Not(IsNil)) c.Assert(err, Not(IsNil))
type myStructBad struct { type myStructBadNoVersion struct {
User string User string
Password string Password string
Folders []string Folders []string
} }
saveMeBad := myStructBad{"guest", "nopassword", []string{"Work", "Documents", "Music"}} saveMeBadNoVersion := myStructBadNoVersion{"guest", "nopassword", []string{"Work", "Documents", "Music"}}
err = quick.CheckData(&saveMeBad) err = quick.CheckData(&saveMeBadNoVersion)
c.Assert(err, Not(IsNil))
type myStructBadVersionInt struct {
Version int
User string
Password string
}
saveMeBadVersionInt := myStructBadVersionInt{1, "guest", "nopassword"}
err = quick.CheckData(&saveMeBadVersionInt)
c.Assert(err, Not(IsNil)) c.Assert(err, Not(IsNil))
type myStructGood struct { type myStructGood struct {
@ -94,6 +103,15 @@ func (s *MySuite) TestVersion(c *C) {
valid, err = quick.CheckVersion("test.json", "2") valid, err = quick.CheckVersion("test.json", "2")
c.Assert(err, IsNil) c.Assert(err, IsNil)
c.Assert(valid, Equals, false) c.Assert(valid, Equals, false)
_, err = quick.CheckVersion("test1.json", "1")
c.Assert(err, Not(IsNil))
file, err := os.Create("test.json")
c.Assert(err, IsNil)
c.Assert(file.Close(), IsNil)
_, err = quick.CheckVersion("test.json", "1")
c.Assert(err, Not(IsNil))
} }
func (s *MySuite) TestSaveLoad(c *C) { func (s *MySuite) TestSaveLoad(c *C) {

Loading…
Cancel
Save