1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
|
/*
This code is derived from jgit (http://eclipse.org/jgit).
Copyright owners are documented in jgit's IP log.
This program and the accompanying materials are made available
under the terms of the Eclipse Distribution License v1.0 which
accompanies this distribution, is reproduced below, and is
available at http://www.eclipse.org/org/documents/edl-v10.php
All rights reserved.
Redistribution and use in source and binary forms, with or
without modification, are permitted provided that the following
conditions are met:
- Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.
- Neither the name of the Eclipse Foundation, Inc. nor the
names of its contributors may be used to endorse or promote
products derived from this software without specific prior
written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
using System;
using NGit;
using NGit.Errors;
using NGit.Revwalk;
using NGit.Storage.File;
using NGit.Storage.Pack;
using NGit.Util;
using NGit.Util.IO;
using Sharpen;
namespace NGit.Storage.File
{
[NUnit.Framework.TestFixture]
public class ConcurrentRepackTest : RepositoryTestCase
{
/// <exception cref="System.Exception"></exception>
[NUnit.Framework.SetUp]
public override void SetUp()
{
WindowCacheConfig windowCacheConfig = new WindowCacheConfig();
windowCacheConfig.SetPackedGitOpenFiles(1);
WindowCache.Reconfigure(windowCacheConfig);
base.SetUp();
}
/// <exception cref="System.Exception"></exception>
[NUnit.Framework.TearDown]
public override void TearDown()
{
base.TearDown();
WindowCacheConfig windowCacheConfig = new WindowCacheConfig();
WindowCache.Reconfigure(windowCacheConfig);
}
/// <exception cref="NGit.Errors.IncorrectObjectTypeException"></exception>
/// <exception cref="System.IO.IOException"></exception>
[NUnit.Framework.Test]
public virtual void TestObjectInNewPack()
{
// Create a new object in a new pack, and test that it is present.
//
Repository eden = CreateBareRepository();
RevObject o1 = WriteBlob(eden, "o1");
Pack(eden, o1);
NUnit.Framework.Assert.AreEqual(o1.Name, Parse(o1).Name);
}
/// <exception cref="NGit.Errors.IncorrectObjectTypeException"></exception>
/// <exception cref="System.IO.IOException"></exception>
[NUnit.Framework.Test]
public virtual void TestObjectMovedToNewPack1()
{
// Create an object and pack it. Then remove that pack and put the
// object into a different pack file, with some other object. We
// still should be able to access the objects.
//
Repository eden = CreateBareRepository();
RevObject o1 = WriteBlob(eden, "o1");
FilePath[] out1 = Pack(eden, o1);
NUnit.Framework.Assert.AreEqual(o1.Name, Parse(o1).Name);
RevObject o2 = WriteBlob(eden, "o2");
Pack(eden, o2, o1);
// Force close, and then delete, the old pack.
//
WhackCache();
Delete(out1);
// Now here is the interesting thing. Will git figure the new
// object exists in the new pack, and not the old one.
//
NUnit.Framework.Assert.AreEqual(o2.Name, Parse(o2).Name);
NUnit.Framework.Assert.AreEqual(o1.Name, Parse(o1).Name);
}
/// <exception cref="NGit.Errors.IncorrectObjectTypeException"></exception>
/// <exception cref="System.IO.IOException"></exception>
[NUnit.Framework.Test]
public virtual void TestObjectMovedWithinPack()
{
// Create an object and pack it.
//
Repository eden = CreateBareRepository();
RevObject o1 = WriteBlob(eden, "o1");
FilePath[] out1 = Pack(eden, o1);
NUnit.Framework.Assert.AreEqual(o1.Name, Parse(o1).Name);
// Force close the old pack.
//
WhackCache();
// Now overwrite the old pack in place. This method of creating a
// different pack under the same file name is partially broken. We
// should also have a different file name because the list of objects
// within the pack has been modified.
//
RevObject o2 = WriteBlob(eden, "o2");
PackWriter pw = new PackWriter(eden);
pw.AddObject(o2);
pw.AddObject(o1);
Write(out1, pw);
pw.Release();
// Try the old name, then the new name. The old name should cause the
// pack to reload when it opens and the index and pack mismatch.
//
NUnit.Framework.Assert.AreEqual(o1.Name, Parse(o1).Name);
NUnit.Framework.Assert.AreEqual(o2.Name, Parse(o2).Name);
}
/// <exception cref="NGit.Errors.IncorrectObjectTypeException"></exception>
/// <exception cref="System.IO.IOException"></exception>
[NUnit.Framework.Test]
public virtual void TestObjectMovedToNewPack2()
{
// Create an object and pack it. Then remove that pack and put the
// object into a different pack file, with some other object. We
// still should be able to access the objects.
//
Repository eden = CreateBareRepository();
RevObject o1 = WriteBlob(eden, "o1");
FilePath[] out1 = Pack(eden, o1);
NUnit.Framework.Assert.AreEqual(o1.Name, Parse(o1).Name);
ObjectLoader load1 = db.Open(o1, Constants.OBJ_BLOB);
NUnit.Framework.Assert.IsNotNull(load1);
RevObject o2 = WriteBlob(eden, "o2");
Pack(eden, o2, o1);
// Force close, and then delete, the old pack.
//
WhackCache();
Delete(out1);
// Now here is the interesting thing... can the loader we made
// earlier still resolve the object, even though its underlying
// pack is gone, but the object still exists.
//
ObjectLoader load2 = db.Open(o1, Constants.OBJ_BLOB);
NUnit.Framework.Assert.IsNotNull(load2);
NUnit.Framework.Assert.AreNotSame(load1, load2);
byte[] data2 = load2.GetCachedBytes();
byte[] data1 = load1.GetCachedBytes();
NUnit.Framework.Assert.IsNotNull(data2);
NUnit.Framework.Assert.IsNotNull(data1);
NUnit.Framework.Assert.AreNotSame(data1, data2);
// cache should be per-pack, not per object
NUnit.Framework.Assert.IsTrue(Arrays.Equals(data1, data2));
NUnit.Framework.Assert.AreEqual(load2.GetType(), load1.GetType());
}
private static void WhackCache()
{
WindowCacheConfig config = new WindowCacheConfig();
config.SetPackedGitOpenFiles(1);
WindowCache.Reconfigure(config);
}
/// <exception cref="NGit.Errors.MissingObjectException"></exception>
/// <exception cref="System.IO.IOException"></exception>
private RevObject Parse(AnyObjectId id)
{
return new RevWalk(db).ParseAny(id);
}
/// <exception cref="System.IO.IOException"></exception>
private FilePath[] Pack(Repository src, params RevObject[] list)
{
PackWriter pw = new PackWriter(src);
foreach (RevObject o in list)
{
pw.AddObject(o);
}
ObjectId name = pw.ComputeName();
FilePath packFile = FullPackFileName(name, ".pack");
FilePath idxFile = FullPackFileName(name, ".idx");
FilePath[] files = new FilePath[] { packFile, idxFile };
Write(files, pw);
pw.Release();
return files;
}
/// <exception cref="System.IO.IOException"></exception>
private static void Write(FilePath[] files, PackWriter pw)
{
long begin = files[0].GetParentFile().LastModified();
NullProgressMonitor m = NullProgressMonitor.INSTANCE;
OutputStream @out;
@out = new SafeBufferedOutputStream(new FileOutputStream(files[0]));
try
{
pw.WritePack(m, m, @out);
}
finally
{
@out.Close();
}
@out = new SafeBufferedOutputStream(new FileOutputStream(files[1]));
try
{
pw.WriteIndex(@out);
}
finally
{
@out.Close();
}
Touch(begin, files[0].GetParentFile());
}
/// <exception cref="System.IO.IOException"></exception>
private static void Delete(FilePath[] list)
{
long begin = list[0].GetParentFile().LastModified();
foreach (FilePath f in list)
{
FileUtils.Delete(f);
NUnit.Framework.Assert.IsFalse(f.Exists(), f + " was removed");
}
Touch(begin, list[0].GetParentFile());
}
private static void Touch(long begin, FilePath dir)
{
while (begin >= dir.LastModified())
{
try
{
Sharpen.Thread.Sleep(25);
}
catch (Exception)
{
}
//
dir.SetLastModified(Runtime.CurrentTimeMillis());
}
}
private FilePath FullPackFileName(ObjectId name, string suffix)
{
FilePath packdir = new FilePath(((ObjectDirectory)db.ObjectDatabase).GetDirectory
(), "pack");
return new FilePath(packdir, "pack-" + name.Name + suffix);
}
/// <exception cref="System.IO.IOException"></exception>
private RevObject WriteBlob(Repository repo, string data)
{
RevWalk revWalk = new RevWalk(repo);
byte[] bytes = Constants.Encode(data);
ObjectInserter inserter = repo.NewObjectInserter();
ObjectId id;
try
{
id = inserter.Insert(Constants.OBJ_BLOB, bytes);
inserter.Flush();
}
finally
{
inserter.Release();
}
try
{
Parse(id);
NUnit.Framework.Assert.Fail("Object " + id.Name + " should not exist in test repository"
);
}
catch (MissingObjectException)
{
}
// Ok
return revWalk.LookupBlob(id);
}
}
}
|