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.

GcPruneNonReferencedTest.java 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * Copyright (C) 2012, Christian Halstrick <christian.halstrick@sap.com> 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.file;
  11. import static org.junit.Assert.assertEquals;
  12. import static org.junit.Assert.assertFalse;
  13. import static org.junit.Assert.assertNotNull;
  14. import static org.junit.Assert.assertTrue;
  15. import java.io.File;
  16. import java.util.Collections;
  17. import java.util.Date;
  18. import org.eclipse.jgit.junit.TestRepository.BranchBuilder;
  19. import org.eclipse.jgit.lib.ObjectId;
  20. import org.eclipse.jgit.revwalk.RevBlob;
  21. import org.eclipse.jgit.revwalk.RevCommit;
  22. import org.eclipse.jgit.revwalk.RevTree;
  23. import org.junit.Test;
  24. public class GcPruneNonReferencedTest extends GcTestCase {
  25. @Test
  26. public void nonReferencedNonExpiredObject_notPruned() throws Exception {
  27. RevBlob a = tr.blob("a");
  28. gc.setExpire(new Date(lastModified(a)));
  29. gc.prune(Collections.<ObjectId> emptySet());
  30. assertTrue(repo.getObjectDatabase().has(a));
  31. }
  32. @Test
  33. public void nonReferencedExpiredObject_pruned() throws Exception {
  34. RevBlob a = tr.blob("a");
  35. gc.setExpireAgeMillis(0);
  36. fsTick();
  37. gc.prune(Collections.<ObjectId> emptySet());
  38. assertFalse(repo.getObjectDatabase().has(a));
  39. }
  40. @Test
  41. public void nonReferencedExpiredObjectTree_pruned() throws Exception {
  42. RevBlob a = tr.blob("a");
  43. RevTree t = tr.tree(tr.file("a", a));
  44. gc.setExpireAgeMillis(0);
  45. fsTick();
  46. gc.prune(Collections.<ObjectId> emptySet());
  47. assertFalse(repo.getObjectDatabase().has(t));
  48. assertFalse(repo.getObjectDatabase().has(a));
  49. }
  50. @Test
  51. public void nonReferencedObjects_onlyExpiredPruned() throws Exception {
  52. RevBlob a = tr.blob("a");
  53. gc.setExpire(new Date(lastModified(a) + 1));
  54. fsTick();
  55. RevBlob b = tr.blob("b");
  56. gc.prune(Collections.<ObjectId> emptySet());
  57. assertFalse(repo.getObjectDatabase().has(a));
  58. assertTrue(repo.getObjectDatabase().has(b));
  59. }
  60. @Test
  61. public void testPackCommitsAndLooseOneWithPruneNow() throws Exception {
  62. BranchBuilder bb = tr.branch("refs/heads/master");
  63. RevCommit first = bb.commit().add("A", "A").add("B", "B").create();
  64. bb.commit().add("A", "A2").add("B", "B2").create();
  65. tr.update("refs/heads/master", first);
  66. stats = gc.getStatistics();
  67. assertEquals(8, stats.numberOfLooseObjects);
  68. assertEquals(0, stats.numberOfPackedObjects);
  69. gc.setExpireAgeMillis(0);
  70. fsTick();
  71. gc.gc();
  72. stats = gc.getStatistics();
  73. assertNoEmptyFanoutDirectories();
  74. assertEquals(0, stats.numberOfLooseObjects);
  75. assertEquals(8, stats.numberOfPackedObjects);
  76. assertEquals(2, stats.numberOfPackFiles);
  77. }
  78. private void assertNoEmptyFanoutDirectories() {
  79. File[] fanout = repo.getObjectsDirectory().listFiles();
  80. assertNotNull(fanout);
  81. for (File f : fanout) {
  82. if (f.isDirectory()) {
  83. String[] entries = f.list();
  84. if (entries == null || entries.length == 0) {
  85. assertFalse(
  86. "Found empty fanout directory "
  87. + f.getAbsolutePath() + " after pruning",
  88. f.getName().length() == 2);
  89. }
  90. }
  91. }
  92. }
  93. }