1
2
3
4
5
6
7
8
9
10
11 package org.eclipse.jgit.internal.storage.dfs;
12
13 import static org.eclipse.jgit.lib.Constants.OBJ_BLOB;
14 import static org.junit.Assert.assertEquals;
15 import static org.junit.Assert.assertNotNull;
16 import static org.junit.Assert.assertNull;
17 import static org.junit.Assert.assertSame;
18
19 import org.eclipse.jgit.internal.storage.dfs.DeltaBaseCache.Entry;
20 import org.eclipse.jgit.junit.TestRng;
21 import org.junit.Before;
22 import org.junit.Test;
23
24 public class DeltaBaseCacheTest {
25 private static final int SZ = 512;
26
27 private DfsStreamKey key;
28 private DeltaBaseCache cache;
29 private TestRng rng;
30
31 @Before
32 public void setUp() {
33 DfsRepositoryDescription repo = new DfsRepositoryDescription("test");
34 key = DfsStreamKey.of(repo, "test.key", null);
35 cache = new DeltaBaseCache(SZ);
36 rng = new TestRng(getClass().getSimpleName());
37 }
38
39 @Test
40 public void testObjectLargerThanCacheDoesNotEvict() {
41 byte[] obj12 = put(12, 32);
42 put(24, SZ + 5);
43 assertNull("does not store large object", cache.get(key, 24));
44 get(obj12, 12);
45 }
46
47 @Test
48 public void testCacheLruExpires1() {
49 byte[] obj1 = put(1, SZ / 4);
50 put(2, SZ / 4);
51 byte[] obj3 = put(3, SZ / 4);
52 put(4, SZ / 4);
53 assertEquals(SZ, cache.getMemoryUsed());
54
55 get(obj3, 3);
56 get(obj1, 1);
57 put(5, SZ / 2);
58 assertEquals(SZ, cache.getMemoryUsed());
59 assertEquals(SZ, cache.getMemoryUsedByTableForTest());
60 assertEquals(SZ, cache.getMemoryUsedByLruChainForTest());
61 assertNull(cache.get(key, 4));
62 assertNull(cache.get(key, 2));
63
64 get(obj1, 1);
65 get(obj3, 3);
66 }
67
68 @Test
69 public void testCacheLruExpires2() {
70 int pos0 = (0 << 10) | 2;
71 int pos1 = (1 << 10) | 2;
72 int pos2 = (2 << 10) | 2;
73 int pos5 = (5 << 10) | 2;
74 int pos6 = (6 << 10) | 2;
75
76 put(pos0, SZ / 4);
77 put(pos5, SZ / 4);
78 byte[] obj1 = put(pos1, SZ / 4);
79 byte[] obj2 = put(pos2, SZ / 4);
80 assertEquals(SZ, cache.getMemoryUsed());
81
82 byte[] obj6 = put(pos6, SZ / 2);
83 assertEquals(SZ, cache.getMemoryUsed());
84 assertEquals(SZ, cache.getMemoryUsedByTableForTest());
85 assertEquals(SZ, cache.getMemoryUsedByLruChainForTest());
86 assertNull(cache.get(key, pos0));
87 assertNull(cache.get(key, pos5));
88
89 get(obj1, pos1);
90 get(obj2, pos2);
91 get(obj6, pos6);
92 }
93
94 @Test
95 public void testCacheMemoryUsedConsistentWithExpectations() {
96 put(1, 32);
97 put(2, 32);
98 put(3, 32);
99
100 assertNotNull(cache.get(key, 1));
101 assertNotNull(cache.get(key, 1));
102
103 assertEquals(32 * 3, cache.getMemoryUsed());
104 assertEquals(32 * 3, cache.getMemoryUsedByTableForTest());
105 assertEquals(32 * 3, cache.getMemoryUsedByLruChainForTest());
106 }
107
108 private void get(byte[] data, int position) {
109 Entry e = cache.get(key, position);
110 assertNotNull("expected entry at " + position, e);
111 assertEquals("expected blob for " + position, OBJ_BLOB, e.type);
112 assertSame("expected data for " + position, data, e.data);
113 }
114
115 private byte[] put(int position, int sz) {
116 byte[] data = rng.nextBytes(sz);
117 cache.put(key, position, OBJ_BLOB, data);
118 return data;
119 }
120 }