Test that ObjectLoader stays valid across repacks
[egit.git] / org.spearce.jgit.test / tst / org / spearce / jgit / lib / ConcurrentRepackTest.java
blobb56e0f4658bbe9e35f51b7d13e1d9312b245280f
1 /*
2 * Copyright (C) 2009, Robin Rosenberg <robin.rosenberg@dewire.com>
3 * Copyright (C) 2009, Google Inc.
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or
8 * without modification, are permitted provided that the following
9 * conditions are met:
11 * - Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * - Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer in the documentation and/or other materials provided
17 * with the distribution.
19 * - Neither the name of the Git Development Community nor the
20 * names of its contributors may be used to endorse or promote
21 * products derived from this software without specific prior
22 * written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
25 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
26 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
29 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
31 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
32 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
33 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
34 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
36 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 package org.spearce.jgit.lib;
41 import java.io.File;
42 import java.io.FileOutputStream;
43 import java.io.IOException;
44 import java.util.Arrays;
46 import org.spearce.jgit.errors.IncorrectObjectTypeException;
47 import org.spearce.jgit.errors.MissingObjectException;
48 import org.spearce.jgit.revwalk.RevObject;
49 import org.spearce.jgit.revwalk.RevWalk;
51 public class ConcurrentRepackTest extends RepositoryTestCase {
52 public void setUp() throws Exception {
53 WindowCacheConfig windowCacheConfig = new WindowCacheConfig();
54 windowCacheConfig.setPackedGitOpenFiles(1);
55 WindowCache.reconfigure(windowCacheConfig);
56 super.setUp();
59 protected void tearDown() throws Exception {
60 super.tearDown();
61 WindowCacheConfig windowCacheConfig = new WindowCacheConfig();
62 WindowCache.reconfigure(windowCacheConfig);
65 public void testObjectInNewPack() throws IncorrectObjectTypeException,
66 IOException {
67 // Create a new object in a new pack, and test that it is present.
69 final Repository eden = createNewEmptyRepo();
70 final RevObject o1 = writeBlob(eden, "o1");
71 pack(eden, o1);
72 assertEquals(o1.name(), parse(o1).name());
75 public void testObjectMovedToNewPack1()
76 throws IncorrectObjectTypeException, IOException {
77 // Create an object and pack it. Then remove that pack and put the
78 // object into a different pack file, with some other object. We
79 // still should be able to access the objects.
81 final Repository eden = createNewEmptyRepo();
82 final RevObject o1 = writeBlob(eden, "o1");
83 final File[] out1 = pack(eden, o1);
84 assertEquals(o1.name(), parse(o1).name());
86 final RevObject o2 = writeBlob(eden, "o2");
87 pack(eden, o2, o1);
89 // Force close, and then delete, the old pack.
91 whackCache();
92 delete(out1);
94 // Now here is the interesting thing. Will git figure the new
95 // object exists in the new pack, and not the old one.
97 assertEquals(o2.name(), parse(o2).name());
98 assertEquals(o1.name(), parse(o1).name());
101 public void testObjectMovedWithinPack()
102 throws IncorrectObjectTypeException, IOException {
103 // Create an object and pack it.
105 final Repository eden = createNewEmptyRepo();
106 final RevObject o1 = writeBlob(eden, "o1");
107 final File[] out1 = pack(eden, o1);
108 assertEquals(o1.name(), parse(o1).name());
110 // Force close the old pack.
112 whackCache();
114 // Now overwrite the old pack in place. This method of creating a
115 // different pack under the same file name is partially broken. We
116 // should also have a different file name because the list of objects
117 // within the pack has been modified.
119 final RevObject o2 = writeBlob(eden, "o2");
120 final PackWriter pw = new PackWriter(eden, NullProgressMonitor.INSTANCE);
121 pw.addObject(o2);
122 pw.addObject(o1);
123 write(out1, pw);
125 // Try the old name, then the new name. The old name should cause the
126 // pack to reload when it opens and the index and pack mismatch.
128 assertEquals(o1.name(), parse(o1).name());
129 assertEquals(o2.name(), parse(o2).name());
132 public void testObjectMovedToNewPack2()
133 throws IncorrectObjectTypeException, IOException {
134 // Create an object and pack it. Then remove that pack and put the
135 // object into a different pack file, with some other object. We
136 // still should be able to access the objects.
138 final Repository eden = createNewEmptyRepo();
139 final RevObject o1 = writeBlob(eden, "o1");
140 final File[] out1 = pack(eden, o1);
141 assertEquals(o1.name(), parse(o1).name());
143 final ObjectLoader load1 = db.openBlob(o1);
144 assertNotNull(load1);
146 final RevObject o2 = writeBlob(eden, "o2");
147 pack(eden, o2, o1);
149 // Force close, and then delete, the old pack.
151 whackCache();
152 delete(out1);
154 // Now here is the interesting thing... can the loader we made
155 // earlier still resolve the object, even though its underlying
156 // pack is gone, but the object still exists.
158 final ObjectLoader load2 = db.openBlob(o1);
159 assertNotNull(load2);
160 assertNotSame(load1, load2);
162 final byte[] data2 = load2.getCachedBytes();
163 final byte[] data1 = load1.getCachedBytes();
164 assertNotNull(data2);
165 assertNotNull(data1);
166 assertNotSame(data1, data2); // cache should be per-pack, not per object
167 assertTrue(Arrays.equals(data1, data2));
168 assertEquals(load2.getType(), load1.getType());
171 private static void whackCache() {
172 final WindowCacheConfig config = new WindowCacheConfig();
174 config.setPackedGitOpenFiles(0);
175 WindowCache.reconfigure(config);
177 config.setPackedGitOpenFiles(1);
178 WindowCache.reconfigure(config);
181 private RevObject parse(final AnyObjectId id)
182 throws MissingObjectException, IOException {
183 return new RevWalk(db).parseAny(id);
186 private File[] pack(final Repository src, final RevObject... list)
187 throws IOException {
188 final PackWriter pw = new PackWriter(src, NullProgressMonitor.INSTANCE);
189 for (final RevObject o : list) {
190 pw.addObject(o);
193 final ObjectId name = pw.computeName();
194 final File packFile = fullPackFileName(name, ".pack");
195 final File idxFile = fullPackFileName(name, ".idx");
196 final File[] files = new File[] { packFile, idxFile };
197 write(files, pw);
198 return files;
201 private static void write(final File[] files, final PackWriter pw)
202 throws IOException {
203 final long begin = files[0].getParentFile().lastModified();
204 FileOutputStream out;
206 out = new FileOutputStream(files[0]);
207 try {
208 pw.writePack(out);
209 } finally {
210 out.close();
213 out = new FileOutputStream(files[1]);
214 try {
215 pw.writeIndex(out);
216 } finally {
217 out.close();
220 touch(begin, files[0].getParentFile());
223 private static void delete(final File[] list) {
224 final long begin = list[0].getParentFile().lastModified();
225 for (final File f : list) {
226 f.delete();
227 assertFalse(f + " was removed", f.exists());
229 touch(begin, list[0].getParentFile());
232 private static void touch(final long begin, final File dir) {
233 while (begin >= dir.lastModified()) {
234 try {
235 Thread.sleep(25);
236 } catch (InterruptedException ie) {
239 dir.setLastModified(System.currentTimeMillis());
243 private File fullPackFileName(final ObjectId name, final String suffix) {
244 final File packdir = new File(db.getObjectsDirectory(), "pack");
245 return new File(packdir, "pack-" + name.name() + suffix);
248 private RevObject writeBlob(final Repository repo, final String data)
249 throws IOException {
250 final RevWalk revWalk = new RevWalk(repo);
251 final byte[] bytes = data.getBytes(Constants.CHARACTER_ENCODING);
252 final ObjectWriter ow = new ObjectWriter(repo);
253 final ObjectId id = ow.writeBlob(bytes);
254 try {
255 parse(id);
256 fail("Object " + id.name() + " should not exist in test repository");
257 } catch (MissingObjectException e) {
258 // Ok
260 return revWalk.lookupBlob(id);