View Javadoc
1   /*
2    * Copyright (C) 2019, Google LLC. 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.junit.Assert.assertEquals;
14  
15  import java.io.ByteArrayOutputStream;
16  import java.io.IOException;
17  import java.util.zip.Deflater;
18  
19  import org.eclipse.jgit.internal.storage.pack.PackExt;
20  import org.eclipse.jgit.internal.storage.pack.PackOutputStream;
21  import org.eclipse.jgit.internal.storage.pack.PackWriter;
22  import org.eclipse.jgit.junit.JGitTestUtil;
23  import org.eclipse.jgit.junit.TestRng;
24  import org.eclipse.jgit.lib.Constants;
25  import org.eclipse.jgit.lib.NullProgressMonitor;
26  import org.junit.Before;
27  import org.junit.Test;
28  
29  public class DfsPackFileTest {
30  	InMemoryRepository db;
31  	boolean bypassCache;
32  	boolean clearCache;
33  
34  	@Before
35  	public void setUp() {
36  		db = new InMemoryRepository(new DfsRepositoryDescription("test"));
37  	}
38  
39  	@Test
40  	public void testCopyPackBypassCachesSmallCached() throws IOException {
41  		bypassCache = true;
42  		clearCache = false;
43  		setupPack(512, 256);
44  		assertPackSize();
45  	}
46  
47  	@Test
48  	public void testCopyPackBypassCacheSmallNoCache() throws IOException {
49  		bypassCache = true;
50  		clearCache = true;
51  		setupPack(512, 256);
52  		assertPackSize();
53  	}
54  
55  	@Test
56  	public void testCopyPackBypassCacheLargeCached() throws IOException {
57  		bypassCache = true;
58  		clearCache = false;
59  		setupPack(512, 8000);
60  		assertPackSize();
61  	}
62  
63  	@Test
64  	public void testCopyPackBypassCacheLargeNoCache() throws IOException {
65  		bypassCache = true;
66  		clearCache = true;
67  		setupPack(512, 8000);
68  		assertPackSize();
69  	}
70  
71  	@Test
72  	public void testCopyPackThroughCacheSmallCached() throws IOException {
73  		bypassCache = false;
74  		clearCache = false;
75  		setupPack(512, 256);
76  		assertPackSize();
77  	}
78  
79  	@Test
80  	public void testCopyPackThroughCacheSmallNoCache() throws IOException {
81  		bypassCache = false;
82  		clearCache = true;
83  		setupPack(512, 256);
84  		assertPackSize();
85  	}
86  
87  	@Test
88  	public void testCopyPackThroughCacheLargeCached() throws IOException {
89  		bypassCache = false;
90  		clearCache = false;
91  		setupPack(512, 8000);
92  		assertPackSize();
93  	}
94  
95  	@Test
96  	public void testCopyPackThroughCacheLargeNoCache() throws IOException {
97  		bypassCache = false;
98  		clearCache = true;
99  		setupPack(512, 8000);
100 		assertPackSize();
101 	}
102 
103 	private void setupPack(int bs, int ps) throws IOException {
104 		DfsBlockCacheConfig cfg = new DfsBlockCacheConfig().setBlockSize(bs)
105 				.setBlockLimit(bs * 100).setStreamRatio(bypassCache ? 0F : 1F);
106 		DfsBlockCache.reconfigure(cfg);
107 
108 		byte[] data = new TestRng(JGitTestUtil.getName()).nextBytes(ps);
109 		DfsInserter ins = (DfsInserter) db.newObjectInserter();
110 		ins.setCompressionLevel(Deflater.NO_COMPRESSION);
111 		ins.insert(Constants.OBJ_BLOB, data);
112 		ins.flush();
113 
114 		if (clearCache) {
115 			DfsBlockCache.reconfigure(cfg);
116 			db.getObjectDatabase().clearCache();
117 		}
118 	}
119 
120 	private void assertPackSize() throws IOException {
121 		try (DfsReader ctx = db.getObjectDatabase().newReader();
122 				PackWriter pw = new PackWriter(ctx);
123 				ByteArrayOutputStream os = new ByteArrayOutputStream();
124 				PackOutputStream out = new PackOutputStream(
125 						NullProgressMonitor.INSTANCE, os, pw)) {
126 			DfsPackFile pack = db.getObjectDatabase().getPacks()[0];
127 			long packSize = pack.getPackDescription().getFileSize(PackExt.PACK);
128 			pack.copyPackAsIs(out, ctx);
129 			assertEquals(packSize - (12 + 20), os.size());
130 		}
131 	}
132 }