You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

GcPackRefsTest.java 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * Copyright (C) 2012, Christian Halstrick <christian.halstrick@sap.com>
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.internal.storage.file;
  44. import static java.lang.Integer.valueOf;
  45. import static org.junit.Assert.assertEquals;
  46. import static org.junit.Assert.assertSame;
  47. import java.io.File;
  48. import java.io.IOException;
  49. import java.util.concurrent.BrokenBarrierException;
  50. import java.util.concurrent.Callable;
  51. import java.util.concurrent.CyclicBarrier;
  52. import java.util.concurrent.ExecutorService;
  53. import java.util.concurrent.Executors;
  54. import java.util.concurrent.Future;
  55. import java.util.concurrent.TimeUnit;
  56. import org.eclipse.jgit.lib.Ref.Storage;
  57. import org.eclipse.jgit.lib.RefUpdate;
  58. import org.eclipse.jgit.lib.RefUpdate.Result;
  59. import org.eclipse.jgit.revwalk.RevBlob;
  60. import org.junit.Test;
  61. public class GcPackRefsTest extends GcTestCase {
  62. @Test
  63. public void looseRefPacked() throws Exception {
  64. RevBlob a = tr.blob("a");
  65. tr.lightweightTag("t", a);
  66. gc.packRefs();
  67. assertSame(repo.getRef("t").getStorage(), Storage.PACKED);
  68. }
  69. @Test
  70. public void concurrentOnlyOneWritesPackedRefs() throws Exception {
  71. RevBlob a = tr.blob("a");
  72. tr.lightweightTag("t", a);
  73. final CyclicBarrier syncPoint = new CyclicBarrier(2);
  74. Callable<Integer> packRefs = new Callable<Integer>() {
  75. /** @return 0 for success, 1 in case of error when writing pack */
  76. public Integer call() throws Exception {
  77. syncPoint.await();
  78. try {
  79. gc.packRefs();
  80. return valueOf(0);
  81. } catch (IOException e) {
  82. return valueOf(1);
  83. }
  84. }
  85. };
  86. ExecutorService pool = Executors.newFixedThreadPool(2);
  87. try {
  88. Future<Integer> p1 = pool.submit(packRefs);
  89. Future<Integer> p2 = pool.submit(packRefs);
  90. assertEquals(1, p1.get().intValue() + p2.get().intValue());
  91. } finally {
  92. pool.shutdown();
  93. pool.awaitTermination(Long.MAX_VALUE, TimeUnit.SECONDS);
  94. }
  95. }
  96. @Test
  97. public void whileRefLockedRefNotPackedNoError()
  98. throws Exception {
  99. RevBlob a = tr.blob("a");
  100. tr.lightweightTag("t1", a);
  101. tr.lightweightTag("t2", a);
  102. LockFile refLock = new LockFile(new File(repo.getDirectory(),
  103. "refs/tags/t1"), repo.getFS());
  104. try {
  105. refLock.lock();
  106. gc.packRefs();
  107. } finally {
  108. refLock.unlock();
  109. }
  110. assertSame(repo.getRef("refs/tags/t1").getStorage(), Storage.LOOSE);
  111. assertSame(repo.getRef("refs/tags/t2").getStorage(), Storage.PACKED);
  112. }
  113. @Test
  114. public void whileRefUpdatedRefUpdateSucceeds()
  115. throws Exception {
  116. RevBlob a = tr.blob("a");
  117. tr.lightweightTag("t", a);
  118. final RevBlob b = tr.blob("b");
  119. final CyclicBarrier refUpdateLockedRef = new CyclicBarrier(2);
  120. final CyclicBarrier packRefsDone = new CyclicBarrier(2);
  121. ExecutorService pool = Executors.newFixedThreadPool(2);
  122. try {
  123. Future<Result> result = pool.submit(new Callable<Result>() {
  124. public Result call() throws Exception {
  125. RefUpdate update = new RefDirectoryUpdate(
  126. (RefDirectory) repo.getRefDatabase(),
  127. repo.getRef("refs/tags/t")) {
  128. @Override
  129. public boolean isForceUpdate() {
  130. try {
  131. refUpdateLockedRef.await();
  132. packRefsDone.await();
  133. } catch (InterruptedException e) {
  134. Thread.currentThread().interrupt();
  135. } catch (BrokenBarrierException e) {
  136. Thread.currentThread().interrupt();
  137. }
  138. return super.isForceUpdate();
  139. }
  140. };
  141. update.setForceUpdate(true);
  142. update.setNewObjectId(b);
  143. return update.update();
  144. }
  145. });
  146. pool.submit(new Callable<Void>() {
  147. public Void call() throws Exception {
  148. refUpdateLockedRef.await();
  149. gc.packRefs();
  150. packRefsDone.await();
  151. return null;
  152. }
  153. });
  154. assertSame(result.get(), Result.FORCED);
  155. } finally {
  156. pool.shutdownNow();
  157. pool.awaitTermination(Long.MAX_VALUE, TimeUnit.SECONDS);
  158. }
  159. assertEquals(repo.getRef("refs/tags/t").getObjectId(), b);
  160. }
  161. }