[test] Fix closing of test repositories

Fix tests failing on Windows because Repository instance is created but
not closed on tear down.

Fix repositories closed twice, except in tests that test this behavior
explicitly.

Name the temporary directories the tests run in after the test method;
that makes it easier to figure out in which tests repositories are
closed twice if it should occur again in the future.

Bug: 550111
Change-Id: I9398b58f0f36d2c29236d2a9a8599117d9083980
Signed-off-by: Nail Samatov <sanail@yandex.ru>
Signed-off-by: Thomas Wolf <thomas.wolf@paranor.ch>
This commit is contained in:
Nail Samatov 2021-09-15 21:00:59 +03:00 committed by Matthias Sohn
parent 8633ea4f07
commit 2b01ac3389
21 changed files with 65 additions and 14 deletions

View File

@ -97,6 +97,7 @@ public ReceivePack create(HttpServletRequest req, Repository db)
server.setUp();
remoteRepository = src.getRepository();
addRepoToClose(remoteRepository);
remoteURI = toURIish(app, srcName);
StoredConfig cfg = remoteRepository.getConfig();

View File

@ -90,6 +90,7 @@ public ReceivePack create(HttpServletRequest req, Repository db)
server.setUp();
remoteRepository = src.getRepository();
addRepoToClose(remoteRepository);
remoteURI = toURIish(app, srcName);
StoredConfig cfg = remoteRepository.getConfig();

View File

@ -22,6 +22,7 @@
import java.util.Set;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jgit.internal.storage.file.FileRepository;
import org.eclipse.jgit.junit.LocalDiskRepositoryTestCase;
import org.eclipse.jgit.junit.TestRepository;
import org.eclipse.jgit.lib.AnyObjectId;
@ -80,7 +81,9 @@ protected AppServer createServer() {
*/
protected TestRepository<Repository> createTestRepository()
throws IOException {
return new TestRepository<>(createBareRepository());
final FileRepository repository = createBareRepository();
addRepoToClose(repository);
return new TestRepository<>(repository);
}
/**

View File

@ -45,6 +45,8 @@
import org.eclipse.jgit.util.SystemReader;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.rules.TestName;
/**
* JUnit TestCase with specialized support for temporary local repository.
@ -83,6 +85,22 @@ public abstract class LocalDiskRepositoryTestCase {
private final Set<Repository> toClose = new HashSet<>();
private File tmp;
/**
* The current test name.
*/
@Rule
public TestName currentTest = new TestName();
private String getTestName() {
String name = currentTest.getMethodName();
name = name.replaceAll("[^a-zA-Z0-9]", "_");
name = name.replaceAll("__+", "_");
if (name.startsWith("_")) {
name = name.substring(1);
}
return name;
}
/**
* Setup test
*
@ -90,12 +108,11 @@ public abstract class LocalDiskRepositoryTestCase {
*/
@Before
public void setUp() throws Exception {
tmp = File.createTempFile("jgit_test_", "_tmp");
tmp = File.createTempFile("jgit_" + getTestName() + '_', "_tmp");
CleanupThread.deleteOnShutdown(tmp);
if (!tmp.delete() || !tmp.mkdir()) {
throw new IOException("Cannot create " + tmp);
}
mockSystemReader = new MockSystemReader();
SystemReader.setInstance(mockSystemReader);

View File

@ -33,6 +33,7 @@ public void setUp() throws Exception {
git.commit().setMessage("initial commit").call();
Repository remoteRepository = createWorkRepository();
addRepoToClose(remoteRepository);
remoteGit = new Git(remoteRepository);
// setup the first repository to fetch from the second repository

View File

@ -71,6 +71,7 @@ public void setUp() throws Exception {
private Git setUpRepoWithRemote() throws Exception {
Repository remoteRepository = createWorkRepository();
addRepoToClose(remoteRepository);
try (Git remoteGit = new Git(remoteRepository)) {
// commit something
writeTrashFile("Test.txt", "Hello world");
@ -85,6 +86,7 @@ private Git setUpRepoWithRemote() throws Exception {
rup.forceUpdate();
Repository localRepository = createWorkRepository();
addRepoToClose(localRepository);
Git localGit = new Git(localRepository);
StoredConfig config = localRepository.getConfig();
RemoteConfig rc = new RemoteConfig(config, "origin");

View File

@ -292,6 +292,7 @@ public void testCheckoutAnnotatedTag() throws Exception {
@Test
public void testCheckoutRemoteTrackingWithUpstream() throws Exception {
Repository db2 = createRepositoryWithRemote();
addRepoToClose(db2);
Git.wrap(db2).checkout().setCreateBranch(true).setName("test")
.setStartPoint("origin/test")
@ -311,6 +312,7 @@ public void testCheckoutRemoteTrackingWithUpstream() throws Exception {
@Test
public void testCheckoutRemoteTrackingWithoutLocalBranch() throws Exception {
Repository db2 = createRepositoryWithRemote();
addRepoToClose(db2);
// checkout remote tracking branch in second repository
// (no local branches exist yet in second repository)

View File

@ -259,7 +259,8 @@ private RevCommit updateSubmoduleRevision() throws Exception {
// the commit that was created
try (SubmoduleWalk w = SubmoduleWalk.forIndex(git.getRepository())) {
assertTrue(w.next());
try (Git g = new Git(w.getRepository())) {
try (Repository repository = w.getRepository()) {
Git g = new Git(repository);
g.fetch().setRemote(REMOTE).setRefSpecs(REFSPEC).call();
g.reset().setMode(ResetType.HARD).setRef(commit1.name()).call();
}

View File

@ -1881,6 +1881,7 @@ public void testFastForwardOnlyNotPossible() throws Exception {
@Test
public void testRecursiveMergeWithConflict() throws Exception {
try (TestRepository<Repository> db_t = new TestRepository<>(db)) {
db.incrementOpen();
BranchBuilder master = db_t.branch("master");
RevCommit m0 = master.commit()
.add("f", "1\n2\n3\n4\n5\n6\n7\n8\n9\n").message("m0")

View File

@ -567,6 +567,7 @@ private void doTestPullWithRebase(Callable<PullResult> pullSetup,
public void setUp() throws Exception {
super.setUp();
dbTarget = createWorkRepository();
addRepoToClose(dbTarget);
source = new Git(db);
target = new Git(dbTarget);

View File

@ -323,6 +323,7 @@ public void setUp() throws Exception {
dbTarget = createWorkRepository();
source = new Git(db);
target = new Git(dbTarget);
addRepoToClose(dbTarget);
// put some file in the source repo
sourceFile = new File(db.getWorkTree(), "SomeFile.txt");

View File

@ -50,6 +50,7 @@ public void testPush() throws JGitInternalException, IOException,
// create other repository
Repository db2 = createWorkRepository();
addRepoToClose(db2);
final StoredConfig config2 = db2.getConfig();
// this tests that this config can be parsed properly
@ -297,6 +298,7 @@ public void testPushWithoutPushRefSpec() throws Exception {
public void testPushAfterGC() throws Exception {
// create other repository
Repository db2 = createWorkRepository();
addRepoToClose(db2);
// setup the first repository
final StoredConfig config = db.getConfig();
@ -360,6 +362,7 @@ public void testPushWithLease() throws JGitInternalException, IOException,
// create other repository
Repository db2 = createWorkRepository();
addRepoToClose(db2);
// setup the first repository
final StoredConfig config = db.getConfig();

View File

@ -127,6 +127,7 @@ public void setUp() throws Exception {
}
diskRepo = fileRepo;
addRepoToClose(diskRepo);
setLogAllRefUpdates(true);
if (!useReftable) {

View File

@ -42,6 +42,7 @@ public void setUp() throws Exception {
@Override
@After
public void tearDown() throws Exception {
tr.close();
super.tearDown();
}

View File

@ -722,6 +722,7 @@ public void testPartialPackFilesScanWhenDoingSearchForReuseTimeoutCheck()
*/
private FileRepository setUpRepoWithMultiplePackfiles() throws Exception {
FileRepository fileRepository = createWorkRepository();
addRepoToClose(fileRepository);
try (Git git = new Git(fileRepository)) {
// Creates 2 objects (C1 = commit, T1 = tree)
git.commit().setMessage("First commit").call();

View File

@ -242,6 +242,7 @@ public void testInitialCheckout() throws Exception {
ListenerHandle handle = null;
try (Git git = new Git(db);
TestRepository<Repository> db_t = new TestRepository<>(db)) {
db.incrementOpen();
handle = db.getListenerList()
.addWorkingTreeModifiedListener(recorder);
BranchBuilder master = db_t.branch("master");
@ -261,6 +262,7 @@ private void checkoutLineEndings(String inIndex, String expected,
String attributes) throws Exception {
try (Git git = new Git(db);
TestRepository<Repository> db_t = new TestRepository<>(db)) {
db.incrementOpen();
BranchBuilder master = db_t.branch("master");
master.commit().add("f", inIndex).message("m0").create();
if (!StringUtils.isEmptyOrNull(attributes)) {
@ -2065,6 +2067,7 @@ public void testResetWithChangeInGitignore() throws Exception {
public void testCheckoutWithEmptyIndexDoesntOverwrite() throws Exception {
try (Git git = new Git(db);
TestRepository<Repository> db_t = new TestRepository<>(db)) {
db.incrementOpen();
// prepare the commits
BranchBuilder master = db_t.branch("master");
RevCommit mergeCommit = master.commit()

View File

@ -1810,6 +1810,7 @@ private void checkModificationTimeStampOrder(String... pathes) {
private String readBlob(ObjectId treeish, String path) throws Exception {
try (TestRepository<?> tr = new TestRepository<>(db);
RevWalk rw = tr.getRevWalk()) {
db.incrementOpen();
RevTree tree = rw.parseTree(treeish);
RevObject obj = tr.get(tree, path);
if (obj == null) {

View File

@ -217,7 +217,6 @@ public void apply(DirCacheEntry ent) {
assertEqualsFile(modulesGitDir, subRepo.getDirectory());
assertEqualsFile(new File(db.getWorkTree(), path),
subRepo.getWorkTree());
subRepo.close();
assertFalse(gen.next());
}
}

View File

@ -80,6 +80,7 @@ public void testWriteSingleRef() throws Exception {
// Then we clone a new repo from that bundle and do a simple test. This
// makes sure we could read the bundle we created.
Repository newRepo = createBareRepository();
addRepoToClose(newRepo);
FetchResult fetchResult = fetchFromBundle(newRepo, bundle);
Ref advertisedRef = fetchResult
.getAdvertisedRef("refs/heads/firstcommit");
@ -116,6 +117,7 @@ public void testIncrementalBundle() throws Exception {
// makes sure
// we could read the bundle we created.
Repository newRepo = createBareRepository();
addRepoToClose(newRepo);
FetchResult fetchResult = fetchFromBundle(newRepo, bundle);
Ref advertisedRef = fetchResult.getAdvertisedRef("refs/heads/aa");

View File

@ -110,6 +110,7 @@ public void test2() throws IOException {
public void testTinyThinPack() throws Exception {
RevBlob a;
try (TestRepository d = new TestRepository<Repository>(db)) {
db.incrementOpen();
a = d.blob("a");
}
@ -132,6 +133,7 @@ public void testTinyThinPack() throws Exception {
public void testPackWithDuplicateBlob() throws Exception {
final byte[] data = Constants.encode("0123456789abcdefg");
try (TestRepository<Repository> d = new TestRepository<>(db)) {
db.incrementOpen();
assertTrue(db.getObjectDatabase().has(d.blob(data)));
}
@ -151,6 +153,7 @@ public void testPackWithDuplicateBlob() throws Exception {
public void testPackWithTrailingGarbage() throws Exception {
RevBlob a;
try (TestRepository d = new TestRepository<Repository>(db)) {
db.incrementOpen();
a = d.blob("a");
}
@ -180,6 +183,7 @@ public void testPackWithTrailingGarbage() throws Exception {
public void testMaxObjectSizeFullBlob() throws Exception {
final byte[] data = Constants.encode("0123456789");
try (TestRepository d = new TestRepository<Repository>(db)) {
db.incrementOpen();
d.blob(data);
}
@ -213,6 +217,7 @@ public void testMaxObjectSizeFullBlob() throws Exception {
public void testMaxObjectSizeDeltaBlock() throws Exception {
RevBlob a;
try (TestRepository d = new TestRepository<Repository>(db)) {
db.incrementOpen();
a = d.blob("a");
}
@ -246,6 +251,7 @@ public void testMaxObjectSizeDeltaBlock() throws Exception {
public void testMaxObjectSizeDeltaResultSize() throws Exception {
RevBlob a;
try (TestRepository d = new TestRepository<Repository>(db)) {
db.incrementOpen();
a = d.blob("0123456789");
}
@ -278,6 +284,7 @@ public void testMaxObjectSizeDeltaResultSize() throws Exception {
public void testNonMarkingInputStream() throws Exception {
RevBlob a;
try (TestRepository d = new TestRepository<Repository>(db)) {
db.incrementOpen();
a = d.blob("a");
}
@ -318,6 +325,7 @@ public void mark(int maxlength) {
public void testDataAfterPackFooterSingleRead() throws Exception {
RevBlob a;
try (TestRepository d = new TestRepository<Repository>(db)) {
db.incrementOpen();
a = d.blob("a");
}
@ -379,6 +387,7 @@ public void testDataAfterPackFooterSplitHeaderRead() throws Exception {
final byte[] data = Constants.encode("a");
RevBlob b;
try (TestRepository d = new TestRepository<Repository>(db)) {
db.incrementOpen();
b = d.blob(data);
}

View File

@ -73,11 +73,14 @@ public void setUp() throws Exception {
super.setUp();
src = createBareRepository();
addRepoToClose(src);
dst = createBareRepository();
addRepoToClose(dst);
// Fill dst with a some common history.
//
try (TestRepository<Repository> d = new TestRepository<>(dst)) {
dst.incrementOpen();
a = d.blob("a");
A = d.commit(d.tree(d.file("a", a)));
B = d.commit().parent(A).create();
@ -106,9 +109,6 @@ public void testFilterHidesPrivate() throws Exception {
dst.getDirectory()) {
@Override
ReceivePack createReceivePack(Repository db) {
db.close();
dst.incrementOpen();
final ReceivePack rp = super.createReceivePack(dst);
rp.setAdvertiseRefsHook(new HidePrivateHook());
return rp;
@ -136,8 +136,6 @@ public void resetsHaves() throws Exception {
dst.getDirectory()) {
@Override
ReceivePack createReceivePack(Repository db) {
dst.incrementOpen();
ReceivePack rp = super.createReceivePack(dst);
rp.setAdvertiseRefsHook(new AdvertiseRefsHook() {
@Override
@ -173,9 +171,6 @@ private TransportLocal newTransportLocalWithStrictValidation()
return new TransportLocal(src, uriOf(dst), dst.getDirectory()) {
@Override
ReceivePack createReceivePack(Repository db) {
db.close();
dst.incrementOpen();
final ReceivePack rp = super.createReceivePack(dst);
rp.setCheckReceivedObjects(true);
rp.setCheckReferencedObjectsAreReachable(true);
@ -211,6 +206,7 @@ public void testSuccess() throws Exception {
// Now use b but in a different commit than what is hidden.
//
try (TestRepository<Repository> s = new TestRepository<>(src)) {
src.incrementOpen();
RevCommit N = s.commit().parent(B).add("q", b).create();
s.update(R_MASTER, N);
@ -228,7 +224,6 @@ public void testSuccess() throws Exception {
try (TransportLocal t = newTransportLocalWithStrictValidation()) {
t.setPushThin(true);
r = t.push(PM, Collections.singleton(u));
dst.close();
}
assertNotNull("have result", r);
@ -290,6 +285,7 @@ private static void receive(final ReceivePack rp,
public void testUsingHiddenDeltaBaseFails() throws Exception {
byte[] delta = { 0x1, 0x1, 0x1, 'c' };
try (TestRepository<Repository> s = new TestRepository<>(src)) {
src.incrementOpen();
RevCommit N = s.commit().parent(B)
.add("q",
s.blob(BinaryDelta.apply(
@ -348,6 +344,7 @@ public void testUsingHiddenCommonBlobFails() throws Exception {
// Try to use the 'b' blob that is hidden.
//
try (TestRepository<Repository> s = new TestRepository<>(src)) {
src.incrementOpen();
RevCommit N = s.commit().parent(B).add("q", s.blob("b")).create();
// But don't include it in the pack.
@ -401,6 +398,7 @@ public void testUsingUnknownBlobFails() throws Exception {
// Try to use the 'n' blob that is not on the server.
//
try (TestRepository<Repository> s = new TestRepository<>(src)) {
src.incrementOpen();
RevBlob n = s.blob("n");
RevCommit N = s.commit().parent(B).add("q", n).create();
@ -491,6 +489,7 @@ private TemporaryBuffer.Heap setupSourceRepoInvalidGitmodules()
.toString();
try (TestRepository<Repository> s = new TestRepository<>(src)) {
src.incrementOpen();
RevBlob blob = s.blob(fakeGitmodules);
RevCommit N = s.commit().parent(B).add(".gitmodules", blob)
.create();
@ -517,6 +516,7 @@ private TemporaryBuffer.Heap setupSourceRepoInvalidGitmodules()
@Test
public void testUsingUnknownTreeFails() throws Exception {
try (TestRepository<Repository> s = new TestRepository<>(src)) {
src.incrementOpen();
RevCommit N = s.commit().parent(B).add("q", s.blob("a")).create();
RevTree t = s.parseBody(N).getTree();