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