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 package org.eclipse.jgit.internal.storage.dfs;
45
46 import static org.eclipse.jgit.lib.Constants.OBJ_BLOB;
47 import static org.junit.Assert.assertEquals;
48 import static org.junit.Assert.assertNotNull;
49 import static org.junit.Assert.assertNull;
50 import static org.junit.Assert.assertSame;
51
52 import org.eclipse.jgit.internal.storage.dfs.DeltaBaseCache.Entry;
53 import org.eclipse.jgit.junit.TestRng;
54 import org.junit.Before;
55 import org.junit.Test;
56
57 public class DeltaBaseCacheTest {
58 private static final int SZ = 512;
59
60 private DfsPackKey key;
61 private DeltaBaseCache cache;
62 private TestRng rng;
63
64 @Before
65 public void setUp() {
66 key = new DfsPackKey();
67 cache = new DeltaBaseCache(SZ);
68 rng = new TestRng(getClass().getSimpleName());
69 }
70
71 @Test
72 public void testObjectLargerThanCacheDoesNotEvict() {
73 byte[] obj12 = put(12, 32);
74 put(24, SZ + 5);
75 assertNull("does not store large object", cache.get(key, 24));
76 get(obj12, 12);
77 }
78
79 @Test
80 public void testCacheLruExpires1() {
81 byte[] obj1 = put(1, SZ / 4);
82 put(2, SZ / 4);
83 byte[] obj3 = put(3, SZ / 4);
84 put(4, SZ / 4);
85 assertEquals(SZ, cache.getMemoryUsed());
86
87 get(obj3, 3);
88 get(obj1, 1);
89 put(5, SZ / 2);
90 assertEquals(SZ, cache.getMemoryUsed());
91 assertEquals(SZ, cache.getMemoryUsedByTableForTest());
92 assertEquals(SZ, cache.getMemoryUsedByLruChainForTest());
93 assertNull(cache.get(key, 4));
94 assertNull(cache.get(key, 2));
95
96 get(obj1, 1);
97 get(obj3, 3);
98 }
99
100 @Test
101 public void testCacheLruExpires2() {
102 int pos0 = (0 << 10) | 2;
103 int pos1 = (1 << 10) | 2;
104 int pos2 = (2 << 10) | 2;
105 int pos5 = (5 << 10) | 2;
106 int pos6 = (6 << 10) | 2;
107
108 put(pos0, SZ / 4);
109 put(pos5, SZ / 4);
110 byte[] obj1 = put(pos1, SZ / 4);
111 byte[] obj2 = put(pos2, SZ / 4);
112 assertEquals(SZ, cache.getMemoryUsed());
113
114 byte[] obj6 = put(pos6, SZ / 2);
115 assertEquals(SZ, cache.getMemoryUsed());
116 assertEquals(SZ, cache.getMemoryUsedByTableForTest());
117 assertEquals(SZ, cache.getMemoryUsedByLruChainForTest());
118 assertNull(cache.get(key, pos0));
119 assertNull(cache.get(key, pos5));
120
121 get(obj1, pos1);
122 get(obj2, pos2);
123 get(obj6, pos6);
124 }
125
126 @Test
127 public void testCacheMemoryUsedConsistentWithExpectations() {
128 put(1, 32);
129 put(2, 32);
130 put(3, 32);
131
132 assertNotNull(cache.get(key, 1));
133 assertNotNull(cache.get(key, 1));
134
135 assertEquals(32 * 3, cache.getMemoryUsed());
136 assertEquals(32 * 3, cache.getMemoryUsedByTableForTest());
137 assertEquals(32 * 3, cache.getMemoryUsedByLruChainForTest());
138 }
139
140 private void get(byte[] data, int position) {
141 Entry e = cache.get(key, position);
142 assertNotNull("expected entry at " + position, e);
143 assertEquals("expected blob for " + position, OBJ_BLOB, e.type);
144 assertSame("expected data for " + position, data, e.data);
145 }
146
147 private byte[] put(int position, int sz) {
148 byte[] data = rng.nextBytes(sz);
149 cache.put(key, position, OBJ_BLOB, data);
150 return data;
151 }
152 }