]> source.dussan.org Git - jgit.git/blob
fa13bb57c8eea4335c6f50bf111a75ad8cf98330
[jgit.git] /
1 /*
2  * Copyright (c) 2024 Jacek Centkowski <geminica.programs@gmail.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
11 package org.eclipse.jgit.internal.storage.file;
12
13 import static org.junit.Assert.assertEquals;
14
15 import java.io.IOException;
16 import java.nio.file.Files;
17 import java.util.stream.StreamSupport;
18
19 import org.eclipse.jgit.lib.PersonIdent;
20 import org.eclipse.jgit.revwalk.RevCommit;
21 import org.junit.Test;
22
23 public class GcNumberOfPackFilesSinceBitmapStatisticsTest extends GcTestCase {
24         @Test
25         public void testShouldReportZeroObjectsForInitializedRepo()
26                         throws IOException {
27                 assertEquals(0L, gc.getStatistics().numberOfPackFilesSinceBitmap);
28         }
29
30         @Test
31         public void testShouldReportAllPackFilesWhenNoGcWasPerformed()
32                         throws Exception {
33                 tr.packAndPrune();
34                 long result = gc.getStatistics().numberOfPackFilesSinceBitmap;
35
36                 assertEquals(repo.getObjectDatabase().getPacks().size(), result);
37         }
38
39         @Test
40         public void testShouldReportNoObjectsDirectlyAfterGc() throws Exception {
41                 // given
42                 addCommit(null);
43                 gc.gc().get();
44                 assertEquals(1L, repositoryBitmapFiles());
45                 assertEquals(0L, gc.getStatistics().numberOfPackFilesSinceBitmap);
46         }
47
48         @Test
49         public void testShouldReportNewObjectsSinceGcWhenRepositoryProgresses()
50                         throws Exception {
51                 // commit & gc
52                 RevCommit parent = addCommit(null);
53                 gc.gc().get();
54                 assertEquals(1L, repositoryBitmapFiles());
55
56                 // progress & pack
57                 addCommit(parent);
58                 tr.packAndPrune();
59
60                 assertEquals(1L, gc.getStatistics().numberOfPackFilesSinceBitmap);
61         }
62
63         @Test
64         public void testShouldReportNewObjectsFromTheLatestBitmapWhenRepositoryProgresses()
65                         throws Exception {
66                 // commit & gc
67                 RevCommit parent = addCommit(null);
68                 gc.gc().get();
69                 assertEquals(1L, repositoryBitmapFiles());
70
71                 // progress & gc
72                 parent = addCommit(parent);
73                 gc.gc().get();
74                 assertEquals(2L, repositoryBitmapFiles());
75
76                 // progress & pack
77                 addCommit(parent);
78                 tr.packAndPrune();
79
80                 assertEquals(1L, gc.getStatistics().numberOfPackFilesSinceBitmap);
81         }
82
83         private RevCommit addCommit(RevCommit parent) throws Exception {
84                 return tr.branch("master").commit()
85                                 .author(new PersonIdent("repo-metrics", "repo@metrics.com"))
86                                 .parent(parent).create();
87         }
88
89         private long repositoryBitmapFiles() throws IOException {
90                 return StreamSupport
91                                 .stream(Files
92                                                 .newDirectoryStream(repo.getObjectDatabase()
93                                                                 .getPackDirectory().toPath(), "pack-*.bitmap")
94                                                 .spliterator(), false)
95                                 .count();
96         }
97 }