aboutsummaryrefslogtreecommitdiffstats
path: root/cache/object_test.go
diff options
context:
space:
mode:
authorAntonio Navarro Perez <antnavper@gmail.com>2017-01-25 14:29:39 +0100
committerSantiago M. Mola <santi@mola.io>2017-01-25 14:29:39 +0100
commitec28bd3808d42f523eeb05e23909dbfc54eb9bcd (patch)
treec7c5470a3f0089f01c5c9e2d80fef60da16a3c94 /cache/object_test.go
parentdc45de29f87a43078356a5be4c4b5aa24f626ee0 (diff)
downloadgo-git-ec28bd3808d42f523eeb05e23909dbfc54eb9bcd.tar.gz
packfile: cache undeltified objects to improve decode performance (#218)
* Simple object cache that keeps in memory the last undeltified objects. When no more objects can be kept into memory, the oldest one is deleted (FIFO). This speeds up packfile operations preventing redundant seeks and decodes.
Diffstat (limited to 'cache/object_test.go')
-rw-r--r--cache/object_test.go85
1 files changed, 85 insertions, 0 deletions
diff --git a/cache/object_test.go b/cache/object_test.go
new file mode 100644
index 0000000..91aba8f
--- /dev/null
+++ b/cache/object_test.go
@@ -0,0 +1,85 @@
+package cache
+
+import (
+ "io"
+ "testing"
+
+ "gopkg.in/src-d/go-git.v4/plumbing"
+
+ . "gopkg.in/check.v1"
+)
+
+func Test(t *testing.T) { TestingT(t) }
+
+type ObjectSuite struct {
+ c *ObjectFIFO
+ aObject plumbing.EncodedObject
+ bObject plumbing.EncodedObject
+ cObject plumbing.EncodedObject
+ dObject plumbing.EncodedObject
+}
+
+var _ = Suite(&ObjectSuite{})
+
+func (s *ObjectSuite) SetUpTest(c *C) {
+ s.aObject = newObject("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 1*Byte)
+ s.bObject = newObject("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", 3*Byte)
+ s.cObject = newObject("cccccccccccccccccccccccccccccccccccccccc", 1*Byte)
+ s.dObject = newObject("dddddddddddddddddddddddddddddddddddddddd", 1*Byte)
+
+ s.c = NewObjectFIFO(2 * Byte)
+}
+
+func (s *ObjectSuite) TestAdd_SameObject(c *C) {
+ s.c.Add(s.aObject)
+ c.Assert(s.c.actualSize, Equals, int64(1*Byte))
+ s.c.Add(s.aObject)
+ c.Assert(s.c.actualSize, Equals, int64(1*Byte))
+}
+
+func (s *ObjectSuite) TestAdd_BigObject(c *C) {
+ s.c.Add(s.bObject)
+ c.Assert(s.c.actualSize, Equals, int64(0))
+ c.Assert(len(s.c.objects), Equals, 0)
+}
+
+func (s *ObjectSuite) TestAdd_CacheOverflow(c *C) {
+ s.c.Add(s.aObject)
+ c.Assert(s.c.actualSize, Equals, int64(1*Byte))
+ s.c.Add(s.cObject)
+ c.Assert(len(s.c.objects), Equals, 2)
+ s.c.Add(s.dObject)
+ c.Assert(len(s.c.objects), Equals, 2)
+
+ c.Assert(s.c.Get(s.aObject.Hash()), IsNil)
+ c.Assert(s.c.Get(s.cObject.Hash()), NotNil)
+ c.Assert(s.c.Get(s.dObject.Hash()), NotNil)
+}
+
+func (s *ObjectSuite) TestClear(c *C) {
+ s.c.Add(s.aObject)
+ c.Assert(s.c.actualSize, Equals, int64(1*Byte))
+ s.c.Clear()
+ c.Assert(s.c.actualSize, Equals, int64(0))
+ c.Assert(s.c.Get(s.aObject.Hash()), IsNil)
+}
+
+type dummyObject struct {
+ hash plumbing.Hash
+ size int64
+}
+
+func newObject(hash string, size int64) plumbing.EncodedObject {
+ return &dummyObject{
+ hash: plumbing.NewHash(hash),
+ size: size,
+ }
+}
+
+func (d *dummyObject) Hash() plumbing.Hash { return d.hash }
+func (*dummyObject) Type() plumbing.ObjectType { return plumbing.InvalidObject }
+func (*dummyObject) SetType(plumbing.ObjectType) {}
+func (d *dummyObject) Size() int64 { return d.size }
+func (*dummyObject) SetSize(s int64) {}
+func (*dummyObject) Reader() (io.ReadCloser, error) { return nil, nil }
+func (*dummyObject) Writer() (io.WriteCloser, error) { return nil, nil }