You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DfsBlockCacheTest.java 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. package org.eclipse.jgit.internal.storage.dfs;
  11. import static org.eclipse.jgit.lib.Constants.OBJ_BLOB;
  12. import static org.junit.Assert.assertEquals;
  13. import static org.junit.Assert.assertTrue;
  14. import java.util.Arrays;
  15. import java.util.Collections;
  16. import java.util.List;
  17. import java.util.stream.LongStream;
  18. import org.eclipse.jgit.junit.TestRng;
  19. import org.eclipse.jgit.lib.ObjectId;
  20. import org.eclipse.jgit.lib.ObjectInserter;
  21. import org.eclipse.jgit.lib.ObjectReader;
  22. import org.junit.Before;
  23. import org.junit.Rule;
  24. import org.junit.Test;
  25. import org.junit.rules.TestName;
  26. public class DfsBlockCacheTest {
  27. @Rule
  28. public TestName testName = new TestName();
  29. private TestRng rng;
  30. private DfsBlockCache cache;
  31. @Before
  32. public void setUp() {
  33. rng = new TestRng(testName.getMethodName());
  34. resetCache();
  35. }
  36. @SuppressWarnings("resource")
  37. @Test
  38. public void streamKeyReusesBlocks() throws Exception {
  39. DfsRepositoryDescription repo = new DfsRepositoryDescription("test");
  40. InMemoryRepository r1 = new InMemoryRepository(repo);
  41. byte[] content = rng.nextBytes(424242);
  42. ObjectId id;
  43. try (ObjectInserter ins = r1.newObjectInserter()) {
  44. id = ins.insert(OBJ_BLOB, content);
  45. ins.flush();
  46. }
  47. long oldSize = LongStream.of(cache.getCurrentSize()).sum();
  48. assertTrue(oldSize > 2000);
  49. assertEquals(0, LongStream.of(cache.getHitCount()).sum());
  50. List<DfsPackDescription> packs = r1.getObjectDatabase().listPacks();
  51. InMemoryRepository r2 = new InMemoryRepository(repo);
  52. r2.getObjectDatabase().commitPack(packs, Collections.emptyList());
  53. try (ObjectReader rdr = r2.newObjectReader()) {
  54. byte[] actual = rdr.open(id, OBJ_BLOB).getBytes();
  55. assertTrue(Arrays.equals(content, actual));
  56. }
  57. assertEquals(0, LongStream.of(cache.getMissCount()).sum());
  58. assertEquals(oldSize, LongStream.of(cache.getCurrentSize()).sum());
  59. }
  60. @SuppressWarnings("resource")
  61. @Test
  62. public void weirdBlockSize() throws Exception {
  63. DfsRepositoryDescription repo = new DfsRepositoryDescription("test");
  64. InMemoryRepository r1 = new InMemoryRepository(repo);
  65. byte[] content1 = rng.nextBytes(4);
  66. byte[] content2 = rng.nextBytes(424242);
  67. ObjectId id1;
  68. ObjectId id2;
  69. try (ObjectInserter ins = r1.newObjectInserter()) {
  70. id1 = ins.insert(OBJ_BLOB, content1);
  71. id2 = ins.insert(OBJ_BLOB, content2);
  72. ins.flush();
  73. }
  74. resetCache();
  75. List<DfsPackDescription> packs = r1.getObjectDatabase().listPacks();
  76. InMemoryRepository r2 = new InMemoryRepository(repo);
  77. r2.getObjectDatabase().setReadableChannelBlockSizeForTest(500);
  78. r2.getObjectDatabase().commitPack(packs, Collections.emptyList());
  79. try (ObjectReader rdr = r2.newObjectReader()) {
  80. byte[] actual = rdr.open(id1, OBJ_BLOB).getBytes();
  81. assertTrue(Arrays.equals(content1, actual));
  82. }
  83. InMemoryRepository r3 = new InMemoryRepository(repo);
  84. r3.getObjectDatabase().setReadableChannelBlockSizeForTest(500);
  85. r3.getObjectDatabase().commitPack(packs, Collections.emptyList());
  86. try (ObjectReader rdr = r3.newObjectReader()) {
  87. byte[] actual = rdr.open(id2, OBJ_BLOB).getBytes();
  88. assertTrue(Arrays.equals(content2, actual));
  89. }
  90. }
  91. private void resetCache() {
  92. DfsBlockCache.reconfigure(new DfsBlockCacheConfig()
  93. .setBlockSize(512)
  94. .setBlockLimit(1 << 20));
  95. cache = DfsBlockCache.getInstance();
  96. }
  97. }