diff options
author | Minh Thai <mthai@google.com> | 2019-02-13 12:16:42 -0800 |
---|---|---|
committer | Minh Thai <mthai@google.com> | 2019-02-13 12:48:05 -0800 |
commit | 2b7fa04dad12c57389281076d54085e536916f19 (patch) | |
tree | 8bba6ce8ceb235cae07eb02774020e8ceec38bca /org.eclipse.jgit.test/tst/org/eclipse | |
parent | 59e75961603f649e6e9d4118f8540e629981e90d (diff) | |
download | jgit-2b7fa04dad12c57389281076d54085e536916f19.tar.gz jgit-2b7fa04dad12c57389281076d54085e536916f19.zip |
Fix bug in copyPackBypassCache's skip 'PACK' header logic
Bug caused the pack to be 12 bytes short when cold cache. Also added
test for copyPackAsIs method.
Change-Id: Idf8fb0e50d1215245d4b032e2e00df4b218c115f
Signed-off-by: Minh Thai <mthai@google.com>
Diffstat (limited to 'org.eclipse.jgit.test/tst/org/eclipse')
-rw-r--r-- | org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/dfs/DfsPackFileTest.java | 165 |
1 files changed, 165 insertions, 0 deletions
diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/dfs/DfsPackFileTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/dfs/DfsPackFileTest.java new file mode 100644 index 0000000000..dd41b6fca6 --- /dev/null +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/dfs/DfsPackFileTest.java @@ -0,0 +1,165 @@ +/* + * Copyright (C) 2019, Google LLC. + * and other copyright owners as documented in the project's IP log. + * + * This program and the accompanying materials are made available + * under the terms of the Eclipse Distribution License v1.0 which + * accompanies this distribution, is reproduced below, and is + * available at http://www.eclipse.org/org/documents/edl-v10.php + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or + * without modification, are permitted provided that the following + * conditions are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. + * + * - Neither the name of the Eclipse Foundation, Inc. nor the + * names of its contributors may be used to endorse or promote + * products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND + * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package org.eclipse.jgit.internal.storage.dfs; + +import static org.junit.Assert.assertEquals; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.util.zip.Deflater; + +import org.eclipse.jgit.internal.storage.pack.PackExt; +import org.eclipse.jgit.internal.storage.pack.PackOutputStream; +import org.eclipse.jgit.internal.storage.pack.PackWriter; +import org.eclipse.jgit.junit.JGitTestUtil; +import org.eclipse.jgit.junit.TestRng; +import org.eclipse.jgit.lib.Constants; +import org.eclipse.jgit.lib.NullProgressMonitor; +import org.junit.Before; +import org.junit.Test; + +public class DfsPackFileTest { + InMemoryRepository db; + boolean bypassCache; + boolean clearCache; + + @Before + public void setUp() { + db = new InMemoryRepository(new DfsRepositoryDescription("test")); + } + + @Test + public void testCopyPackBypassCachesSmallCached() throws IOException { + bypassCache = true; + clearCache = false; + setupPack(512, 256); + assertPackSize(); + } + + @Test + public void testCopyPackBypassCacheSmallNoCache() throws IOException { + bypassCache = true; + clearCache = true; + setupPack(512, 256); + assertPackSize(); + } + + @Test + public void testCopyPackBypassCacheLargeCached() throws IOException { + bypassCache = true; + clearCache = false; + setupPack(512, 8000); + assertPackSize(); + } + + @Test + public void testCopyPackBypassCacheLargeNoCache() throws IOException { + bypassCache = true; + clearCache = true; + setupPack(512, 8000); + assertPackSize(); + } + + @Test + public void testCopyPackThroughCacheSmallCached() throws IOException { + bypassCache = false; + clearCache = false; + setupPack(512, 256); + assertPackSize(); + } + + @Test + public void testCopyPackThroughCacheSmallNoCache() throws IOException { + bypassCache = false; + clearCache = true; + setupPack(512, 256); + assertPackSize(); + } + + @Test + public void testCopyPackThroughCacheLargeCached() throws IOException { + bypassCache = false; + clearCache = false; + setupPack(512, 8000); + assertPackSize(); + } + + @Test + public void testCopyPackThroughCacheLargeNoCache() throws IOException { + bypassCache = false; + clearCache = true; + setupPack(512, 8000); + assertPackSize(); + } + + private void setupPack(int bs, int ps) throws IOException { + DfsBlockCacheConfig cfg = new DfsBlockCacheConfig().setBlockSize(bs) + .setBlockLimit(bs * 100).setStreamRatio(bypassCache ? 0F : 1F); + DfsBlockCache.reconfigure(cfg); + + byte[] data = new TestRng(JGitTestUtil.getName()).nextBytes(ps); + DfsInserter ins = (DfsInserter) db.newObjectInserter(); + ins.setCompressionLevel(Deflater.NO_COMPRESSION); + ins.insert(Constants.OBJ_BLOB, data); + ins.flush(); + + if (clearCache) { + DfsBlockCache.reconfigure(cfg); + db.getObjectDatabase().clearCache(); + } + } + + private void assertPackSize() throws IOException { + try (DfsReader ctx = db.getObjectDatabase().newReader(); + PackWriter pw = new PackWriter(ctx); + ByteArrayOutputStream os = new ByteArrayOutputStream(); + PackOutputStream out = new PackOutputStream( + NullProgressMonitor.INSTANCE, os, pw)) { + DfsPackFile pack = db.getObjectDatabase().getPacks()[0]; + long packSize = pack.getPackDescription().getFileSize(PackExt.PACK); + pack.copyPackAsIs(out, ctx); + assertEquals(packSize - (12 + 20), os.size()); + } + } +} |