aboutsummaryrefslogtreecommitdiffstats
path: root/plumbing/cache/object_test.go
blob: 2a55acf67723ed322125214323b911b8a91bfbde (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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.Put(s.aObject)
	c.Assert(s.c.actualSize, Equals, 1*Byte)
	s.c.Put(s.aObject)
	c.Assert(s.c.actualSize, Equals, 1*Byte)
}

func (s *ObjectSuite) TestAdd_BigObject(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)
}

func (s *ObjectSuite) TestAdd_CacheOverflow(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)
	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.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)
}

type dummyObject struct {
	hash plumbing.Hash
	size FileSize
}

func newObject(hash string, size FileSize) 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 int64(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 }