From f07672f5c3cad2e73596ab3d7ca16660f6881df6 Mon Sep 17 00:00:00 2001 From: "Santiago M. Mola" Date: Mon, 24 Jul 2017 13:36:41 +0200 Subject: plumbing/cache: use more explicit interface * renamed Add to Put * Get returns a second bool value to indicate if there was hit or miss. --- plumbing/cache/object_test.go | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) (limited to 'plumbing/cache/object_test.go') diff --git a/plumbing/cache/object_test.go b/plumbing/cache/object_test.go index 80cd17b..2a55acf 100644 --- a/plumbing/cache/object_test.go +++ b/plumbing/cache/object_test.go @@ -31,14 +31,14 @@ func (s *ObjectSuite) SetUpTest(c *C) { } func (s *ObjectSuite) TestAdd_SameObject(c *C) { - s.c.Add(s.aObject) + s.c.Put(s.aObject) c.Assert(s.c.actualSize, Equals, 1*Byte) - s.c.Add(s.aObject) + s.c.Put(s.aObject) c.Assert(s.c.actualSize, Equals, 1*Byte) } func (s *ObjectSuite) TestAdd_BigObject(c *C) { - s.c.Add(s.bObject) + s.c.Put(s.bObject) c.Assert(s.c.actualSize, Equals, 0*Byte) c.Assert(s.c.actualSize, Equals, 0*KiByte) c.Assert(s.c.actualSize, Equals, 0*MiByte) @@ -47,24 +47,32 @@ func (s *ObjectSuite) TestAdd_BigObject(c *C) { } func (s *ObjectSuite) TestAdd_CacheOverflow(c *C) { - s.c.Add(s.aObject) + s.c.Put(s.aObject) c.Assert(s.c.actualSize, Equals, 1*Byte) - s.c.Add(s.cObject) + s.c.Put(s.cObject) c.Assert(len(s.c.objects), Equals, 2) - s.c.Add(s.dObject) + s.c.Put(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) + obj, ok := s.c.Get(s.aObject.Hash()) + c.Assert(ok, Equals, false) + c.Assert(obj, IsNil) + obj, ok = s.c.Get(s.cObject.Hash()) + c.Assert(ok, Equals, true) + c.Assert(obj, NotNil) + obj, ok = s.c.Get(s.dObject.Hash()) + c.Assert(ok, Equals, true) + c.Assert(obj, NotNil) } func (s *ObjectSuite) TestClear(c *C) { - s.c.Add(s.aObject) + s.c.Put(s.aObject) c.Assert(s.c.actualSize, Equals, 1*Byte) s.c.Clear() c.Assert(s.c.actualSize, Equals, 0*Byte) - c.Assert(s.c.Get(s.aObject.Hash()), IsNil) + obj, ok := s.c.Get(s.aObject.Hash()) + c.Assert(ok, Equals, false) + c.Assert(obj, IsNil) } type dummyObject struct { -- cgit From ae1c4f3df729c3a7fed4cd5a1f530c95d640497a Mon Sep 17 00:00:00 2001 From: "Santiago M. Mola" Date: Mon, 24 Jul 2017 14:19:21 +0200 Subject: plumbing/cache: change FIFO to LRU cache --- plumbing/cache/object_test.go | 26 +++++++++----------------- 1 file changed, 9 insertions(+), 17 deletions(-) (limited to 'plumbing/cache/object_test.go') diff --git a/plumbing/cache/object_test.go b/plumbing/cache/object_test.go index 2a55acf..9359455 100644 --- a/plumbing/cache/object_test.go +++ b/plumbing/cache/object_test.go @@ -12,7 +12,7 @@ import ( func Test(t *testing.T) { TestingT(t) } type ObjectSuite struct { - c *ObjectFIFO + c Object aObject plumbing.EncodedObject bObject plumbing.EncodedObject cObject plumbing.EncodedObject @@ -27,32 +27,26 @@ func (s *ObjectSuite) SetUpTest(c *C) { s.cObject = newObject("cccccccccccccccccccccccccccccccccccccccc", 1*Byte) s.dObject = newObject("dddddddddddddddddddddddddddddddddddddddd", 1*Byte) - s.c = NewObjectFIFO(2 * Byte) + s.c = NewObjectLRU(2 * Byte) } -func (s *ObjectSuite) TestAdd_SameObject(c *C) { +func (s *ObjectSuite) TestPutSameObject(c *C) { s.c.Put(s.aObject) - c.Assert(s.c.actualSize, Equals, 1*Byte) s.c.Put(s.aObject) - c.Assert(s.c.actualSize, Equals, 1*Byte) + _, ok := s.c.Get(s.aObject.Hash()) + c.Assert(ok, Equals, true) } -func (s *ObjectSuite) TestAdd_BigObject(c *C) { +func (s *ObjectSuite) TestPutBigObject(c *C) { s.c.Put(s.bObject) - c.Assert(s.c.actualSize, Equals, 0*Byte) - c.Assert(s.c.actualSize, Equals, 0*KiByte) - c.Assert(s.c.actualSize, Equals, 0*MiByte) - c.Assert(s.c.actualSize, Equals, 0*GiByte) - c.Assert(len(s.c.objects), Equals, 0) + _, ok := s.c.Get(s.aObject.Hash()) + c.Assert(ok, Equals, false) } -func (s *ObjectSuite) TestAdd_CacheOverflow(c *C) { +func (s *ObjectSuite) TestPutCacheOverflow(c *C) { s.c.Put(s.aObject) - c.Assert(s.c.actualSize, Equals, 1*Byte) s.c.Put(s.cObject) - c.Assert(len(s.c.objects), Equals, 2) s.c.Put(s.dObject) - c.Assert(len(s.c.objects), Equals, 2) obj, ok := s.c.Get(s.aObject.Hash()) c.Assert(ok, Equals, false) @@ -67,9 +61,7 @@ func (s *ObjectSuite) TestAdd_CacheOverflow(c *C) { func (s *ObjectSuite) TestClear(c *C) { s.c.Put(s.aObject) - c.Assert(s.c.actualSize, Equals, 1*Byte) s.c.Clear() - c.Assert(s.c.actualSize, Equals, 0*Byte) obj, ok := s.c.Get(s.aObject.Hash()) c.Assert(ok, Equals, false) c.Assert(obj, IsNil) -- cgit