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.

GcTemporaryFilesTest.java 2.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. * Copyright (C) 2017 Ericsson 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.assertFalse;
  12. import static org.junit.Assert.assertTrue;
  13. import java.io.File;
  14. import java.nio.file.Paths;
  15. import java.time.Instant;
  16. import org.junit.Before;
  17. import org.junit.Test;
  18. public class GcTemporaryFilesTest extends GcTestCase {
  19. private static final String TEMP_IDX = "gc_1234567890.idx_tmp";
  20. private static final String TEMP_PACK = "gc_1234567890.pack_tmp";
  21. private File packDir;
  22. @Override
  23. @Before
  24. public void setUp() throws Exception {
  25. super.setUp();
  26. packDir = Paths.get(repo.getObjectsDirectory().getAbsolutePath(),
  27. "pack").toFile(); //$NON-NLS-1$
  28. }
  29. @Test
  30. public void oldTempPacksAndIdxAreDeleted() throws Exception {
  31. File tempIndex = new File(packDir, TEMP_IDX);
  32. File tempPack = new File(packDir, TEMP_PACK);
  33. if (!packDir.exists() || !packDir.isDirectory()) {
  34. assertTrue(packDir.mkdirs());
  35. }
  36. assertTrue(tempPack.createNewFile());
  37. assertTrue(tempIndex.createNewFile());
  38. assertTrue(tempIndex.exists());
  39. assertTrue(tempPack.exists());
  40. long _24HoursBefore = Instant.now().toEpochMilli()
  41. - 24 * 60 * 62 * 1000;
  42. tempIndex.setLastModified(_24HoursBefore);
  43. tempPack.setLastModified(_24HoursBefore);
  44. gc.gc();
  45. assertFalse(tempIndex.exists());
  46. assertFalse(tempPack.exists());
  47. }
  48. @Test
  49. public void recentTempPacksAndIdxAreNotDeleted() throws Exception {
  50. File tempIndex = new File(packDir, TEMP_IDX);
  51. File tempPack = new File(packDir, TEMP_PACK);
  52. if (!packDir.exists() || !packDir.isDirectory()) {
  53. assertTrue(packDir.mkdirs());
  54. }
  55. assertTrue(tempPack.createNewFile());
  56. assertTrue(tempIndex.createNewFile());
  57. assertTrue(tempIndex.exists());
  58. assertTrue(tempPack.exists());
  59. gc.gc();
  60. assertTrue(tempIndex.exists());
  61. assertTrue(tempPack.exists());
  62. }
  63. }