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.

GcConcurrentTest.java 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 java.io.IOException;
  47. import java.util.concurrent.BrokenBarrierException;
  48. import java.util.concurrent.Callable;
  49. import java.util.concurrent.CyclicBarrier;
  50. import java.util.concurrent.ExecutorService;
  51. import java.util.concurrent.Executors;
  52. import java.util.concurrent.Future;
  53. import java.util.concurrent.TimeUnit;
  54. import org.eclipse.jgit.internal.JGitText;
  55. import org.eclipse.jgit.lib.EmptyProgressMonitor;
  56. import org.eclipse.jgit.revwalk.RevBlob;
  57. import org.junit.Test;
  58. public class GcConcurrentTest extends GcTestCase {
  59. @Test
  60. public void concurrentRepack() throws Exception {
  61. final CyclicBarrier syncPoint = new CyclicBarrier(2);
  62. class DoRepack extends EmptyProgressMonitor implements
  63. Callable<Integer> {
  64. public void beginTask(String title, int totalWork) {
  65. if (title.equals(JGitText.get().writingObjects)) {
  66. try {
  67. syncPoint.await();
  68. } catch (InterruptedException e) {
  69. Thread.currentThread().interrupt();
  70. } catch (BrokenBarrierException ignored) {
  71. //
  72. }
  73. }
  74. }
  75. /** @return 0 for success, 1 in case of error when writing pack */
  76. public Integer call() throws Exception {
  77. try {
  78. gc.setProgressMonitor(this);
  79. gc.repack();
  80. return valueOf(0);
  81. } catch (IOException e) {
  82. // leave the syncPoint in broken state so any awaiting
  83. // threads and any threads that call await in the future get
  84. // the BrokenBarrierException
  85. Thread.currentThread().interrupt();
  86. try {
  87. syncPoint.await();
  88. } catch (InterruptedException ignored) {
  89. //
  90. }
  91. return valueOf(1);
  92. }
  93. }
  94. }
  95. RevBlob a = tr.blob("a");
  96. tr.lightweightTag("t", a);
  97. ExecutorService pool = Executors.newFixedThreadPool(2);
  98. try {
  99. DoRepack repack1 = new DoRepack();
  100. DoRepack repack2 = new DoRepack();
  101. Future<Integer> result1 = pool.submit(repack1);
  102. Future<Integer> result2 = pool.submit(repack2);
  103. assertEquals(0, result1.get().intValue() + result2.get().intValue());
  104. } finally {
  105. pool.shutdown();
  106. pool.awaitTermination(Long.MAX_VALUE, TimeUnit.SECONDS);
  107. }
  108. }
  109. }