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 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
84 public Integer call() throws Exception {
85 try {
86 gc.setProgressMonitor(this);
87 gc.repack();
88 return valueOf(0);
89 } catch (IOException e) {
90
91
92
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 }