View Javadoc
1   /*
2    * Copyright (C) 2017, Google Inc. and others
3    *
4    * This program and the accompanying materials are made available under the
5    * terms of the Eclipse Distribution License v. 1.0 which is available at
6    * https://www.eclipse.org/org/documents/edl-v10.php.
7    *
8    * SPDX-License-Identifier: BSD-3-Clause
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.assertTrue;
16  
17  import java.util.Arrays;
18  import java.util.Collections;
19  import java.util.List;
20  import java.util.stream.LongStream;
21  
22  import org.eclipse.jgit.junit.TestRng;
23  import org.eclipse.jgit.lib.ObjectId;
24  import org.eclipse.jgit.lib.ObjectInserter;
25  import org.eclipse.jgit.lib.ObjectReader;
26  import org.junit.Before;
27  import org.junit.Rule;
28  import org.junit.Test;
29  import org.junit.rules.TestName;
30  
31  public class DfsBlockCacheTest {
32  	@Rule
33  	public TestName testName = new TestName();
34  	private TestRng rng;
35  	private DfsBlockCache cache;
36  
37  	@Before
38  	public void setUp() {
39  		rng = new TestRng(testName.getMethodName());
40  		resetCache();
41  	}
42  
43  	@SuppressWarnings("resource")
44  	@Test
45  	public void streamKeyReusesBlocks() throws Exception {
46  		DfsRepositoryDescription repo = new DfsRepositoryDescription("test");
47  		InMemoryRepository r1 = new InMemoryRepository(repo);
48  		byte[] content = rng.nextBytes(424242);
49  		ObjectId id;
50  		try (ObjectInserter ins = r1.newObjectInserter()) {
51  			id = ins.insert(OBJ_BLOB, content);
52  			ins.flush();
53  		}
54  
55  		long oldSize = LongStream.of(cache.getCurrentSize()).sum();
56  		assertTrue(oldSize > 2000);
57  		assertEquals(0, LongStream.of(cache.getHitCount()).sum());
58  
59  		List<DfsPackDescription> packs = r1.getObjectDatabase().listPacks();
60  		InMemoryRepository r2 = new InMemoryRepository(repo);
61  		r2.getObjectDatabase().commitPack(packs, Collections.emptyList());
62  		try (ObjectReader rdr = r2.newObjectReader()) {
63  			byte[] actual = rdr.open(id, OBJ_BLOB).getBytes();
64  			assertTrue(Arrays.equals(content, actual));
65  		}
66  		assertEquals(0, LongStream.of(cache.getMissCount()).sum());
67  		assertEquals(oldSize, LongStream.of(cache.getCurrentSize()).sum());
68  	}
69  
70  	@SuppressWarnings("resource")
71  	@Test
72  	public void weirdBlockSize() throws Exception {
73  		DfsRepositoryDescription repo = new DfsRepositoryDescription("test");
74  		InMemoryRepository r1 = new InMemoryRepository(repo);
75  
76  		byte[] content1 = rng.nextBytes(4);
77  		byte[] content2 = rng.nextBytes(424242);
78  		ObjectId id1;
79  		ObjectId id2;
80  		try (ObjectInserter ins = r1.newObjectInserter()) {
81  			id1 = ins.insert(OBJ_BLOB, content1);
82  			id2 = ins.insert(OBJ_BLOB, content2);
83  			ins.flush();
84  		}
85  
86  		resetCache();
87  		List<DfsPackDescription> packs = r1.getObjectDatabase().listPacks();
88  
89  		InMemoryRepository r2 = new InMemoryRepository(repo);
90  		r2.getObjectDatabase().setReadableChannelBlockSizeForTest(500);
91  		r2.getObjectDatabase().commitPack(packs, Collections.emptyList());
92  		try (ObjectReader rdr = r2.newObjectReader()) {
93  			byte[] actual = rdr.open(id1, OBJ_BLOB).getBytes();
94  			assertTrue(Arrays.equals(content1, actual));
95  		}
96  
97  		InMemoryRepository r3 = new InMemoryRepository(repo);
98  		r3.getObjectDatabase().setReadableChannelBlockSizeForTest(500);
99  		r3.getObjectDatabase().commitPack(packs, Collections.emptyList());
100 		try (ObjectReader rdr = r3.newObjectReader()) {
101 			byte[] actual = rdr.open(id2, OBJ_BLOB).getBytes();
102 			assertTrue(Arrays.equals(content2, actual));
103 		}
104 	}
105 
106 	private void resetCache() {
107 		DfsBlockCache.reconfigure(new DfsBlockCacheConfig()
108 				.setBlockSize(512)
109 				.setBlockLimit(1 << 20));
110 		cache = DfsBlockCache.getInstance();
111 	}
112 }