Make TestRepository AutoCloseable
Currently, unit tests need to either close the Repository underlying a TestRepository manually, or not close it at all. Both are error prone. The TestRepository holds a reference to 4 AutoCloseable objects: Repository, ObjectInserter, Git, and RevWalk. The last two can escape the TestRepository scope, so they are not closed when TestRepository is closed. Change-Id: I4461bb9104d517bd6bef09c38507c7c2ef5c31d4 Signed-off-by: Jackson Toeniskoetter <jackdt@google.com>
This commit is contained in:
parent
23c30c6310
commit
8ed59c511c
|
@ -112,7 +112,7 @@
|
||||||
* @param <R>
|
* @param <R>
|
||||||
* type of Repository the test data is stored on.
|
* type of Repository the test data is stored on.
|
||||||
*/
|
*/
|
||||||
public class TestRepository<R extends Repository> {
|
public class TestRepository<R extends Repository> implements AutoCloseable {
|
||||||
|
|
||||||
/** Constant <code>AUTHOR="J. Author"</code> */
|
/** Constant <code>AUTHOR="J. Author"</code> */
|
||||||
public static final String AUTHOR = "J. Author";
|
public static final String AUTHOR = "J. Author";
|
||||||
|
@ -933,6 +933,23 @@ public void packAndPrune() throws Exception {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Closes the underlying {@link Repository} object and any other internal
|
||||||
|
* resources.
|
||||||
|
* <p>
|
||||||
|
* {@link AutoCloseable} resources that may escape this object, such as
|
||||||
|
* those returned by the {@link #git} and {@link #getRevWalk()} methods are
|
||||||
|
* not closed.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void close() {
|
||||||
|
try {
|
||||||
|
inserter.close();
|
||||||
|
} finally {
|
||||||
|
db.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private static void prunePacked(ObjectDirectory odb) throws IOException {
|
private static void prunePacked(ObjectDirectory odb) throws IOException {
|
||||||
for (PackFile p : odb.getPacks()) {
|
for (PackFile p : odb.getPacks()) {
|
||||||
for (MutableEntry e : p)
|
for (MutableEntry e : p)
|
||||||
|
|
Loading…
Reference in New Issue