comparison test/org/tmatesoft/hg/test/TestRepositoryLock.java @ 631:8a5cdcb27b8f

AIOOBE in HgManifest.RevisionMapper. Provide more details about exception context. Create lock file atomically. Test concurrent pull-rebase and read
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Thu, 30 May 2013 15:24:17 +0200
parents 5e0313485eef
children
comparison
equal deleted inserted replaced
630:72c979555cb8 631:8a5cdcb27b8f
15 * contact TMate Software at support@hg4j.com 15 * contact TMate Software at support@hg4j.com
16 */ 16 */
17 package org.tmatesoft.hg.test; 17 package org.tmatesoft.hg.test;
18 18
19 import java.io.File; 19 import java.io.File;
20 import java.util.HashMap;
21 import java.util.Map;
22 import java.util.concurrent.CountDownLatch;
20 23
21 import org.junit.Assert; 24 import org.junit.Assert;
22 import org.junit.Test; 25 import org.junit.Test;
26 import org.tmatesoft.hg.core.HgStatusCommand;
27 import org.tmatesoft.hg.internal.BasicSessionContext;
28 import org.tmatesoft.hg.internal.DataAccessProvider;
23 import org.tmatesoft.hg.repo.HgLookup; 29 import org.tmatesoft.hg.repo.HgLookup;
24 import org.tmatesoft.hg.repo.HgRepository; 30 import org.tmatesoft.hg.repo.HgRepository;
25 import org.tmatesoft.hg.repo.HgRepositoryLock; 31 import org.tmatesoft.hg.repo.HgRepositoryLock;
26 32
27 /** 33 /**
48 Assert.assertTrue(p.result().toString().contains("abort")); 54 Assert.assertTrue(p.result().toString().contains("abort"));
49 } finally { 55 } finally {
50 wdLock.release(); 56 wdLock.release();
51 } 57 }
52 } 58 }
59
60 public static void main(String[] args) throws Exception {
61 Map<String, Object> po = new HashMap<String, Object>();
62 po.put(DataAccessProvider.CFG_PROPERTY_MAPIO_LIMIT, 0);
63 final HgLookup hgLookup = new HgLookup(new BasicSessionContext(po , null));
64 final File rebaseFromRepoLoc = RepoUtils.cloneRepoToTempLocation(new File("/temp/hg/junit-test-repos/test-annotate"), "repo-lock-remote", false, true);
65 final File rebaseToRepoLoc = RepoUtils.cloneRepoToTempLocation(rebaseFromRepoLoc, "repo-lock-local", false, true);
66 final File remoteChanges = new File(rebaseFromRepoLoc, "file1");
67 //
68 // create commit in the "local" repository that will be rebased on top of changes
69 // pulled from "remote repository"
70 File localChanges = new File(rebaseToRepoLoc, "file-new");
71 if (localChanges.exists()) {
72 RepoUtils.modifyFileAppend(localChanges, "whatever");
73 } else {
74 RepoUtils.createFile(localChanges, "whatever");
75 }
76 commit(rebaseToRepoLoc, "local change");
77 //
78 final int rebaseRevisionCount = 70;
79 final CountDownLatch latch = new CountDownLatch(2);
80 Runnable r1 = new Runnable() {
81 public void run() {
82 for (int i = 0; i < rebaseRevisionCount; i++) {
83 commitPullRebaseNative(rebaseFromRepoLoc, rebaseToRepoLoc, remoteChanges);
84 sleep(500, 1000);
85 }
86 latch.countDown();
87 }
88 };
89 Runnable r2 = new Runnable() {
90 public void run() {
91 for (int i = 0; i < 100; i++) {
92 readWithHg4J(hgLookup, rebaseToRepoLoc);
93 sleep(800, 400);
94 }
95 latch.countDown();
96 }
97 };
98 new Thread(r1, "pull-rebase-thread").start();
99 new Thread(r2, "hg4j-read-thread").start();
100 latch.await();
101 System.out.println("DONE.");
102 // now `hg log` in rebaseToRepoLoc shall show
103 // all rebaseRevisionCount revisions from rebaseFromRepoLoc + 1 more, "local change", on top of them
104 }
105
106 private static int count = 0;
107
108 private static void commitPullRebaseNative(final File rebaseFromRepoLoc, final File rebaseToRepoLoc, final File rebaseFromChanges) {
109 try {
110 OutputParser.Stub p = new OutputParser.Stub();
111 final ExecHelper eh = new ExecHelper(p, rebaseToRepoLoc);
112 RepoUtils.modifyFileAppend(rebaseFromChanges, "Change #" + count++);
113 commit(rebaseFromRepoLoc, "remote change");
114 p.reset();
115 eh.run("hg", "--traceback", "pull", rebaseFromRepoLoc.toString());
116 if (eh.getExitValue() != 0) {
117 System.out.println(p.result());
118 }
119 Assert.assertEquals(0, eh.getExitValue());
120 p.reset();
121 eh.run("hg", "--traceback", "--config", "extensions.hgext.rebase=", "rebase");
122 if (eh.getExitValue() != 0) {
123 System.out.println(p.result());
124 }
125 System.out.print("X");
126 Assert.assertEquals(0, eh.getExitValue());
127 } catch (RuntimeException ex) {
128 throw ex;
129 } catch (Exception ex) {
130 ex.printStackTrace();
131 throw new RuntimeException(null, ex);
132 }
133 }
134
135 private static void readWithHg4J(final HgLookup hgLookup, final File repoLoc) {
136 try {
137 System.out.print("(");
138 final long start = System.nanoTime();
139 HgRepository hgRepo = hgLookup.detect(repoLoc);
140 final HgRepositoryLock wcLock = hgRepo.getWorkingDirLock();
141 final HgRepositoryLock storeLock = hgRepo.getStoreLock();
142 wcLock.acquire();
143 System.out.print(".");
144 storeLock.acquire();
145 System.out.print(".");
146 try {
147 new HgStatusCommand(hgRepo).execute(new TestStatus.StatusCollector());
148 System.out.printf("%d ms)\n", (System.nanoTime() - start) / 1000000);
149 } finally {
150 storeLock.release();
151 wcLock.release();
152 }
153 } catch (RuntimeException ex) {
154 throw ex;
155 } catch (Exception ex) {
156 ex.printStackTrace();
157 throw new RuntimeException(null, ex);
158 }
159 }
160
161 private static void commit(File repoLoc, String message) throws Exception {
162 OutputParser.Stub p = new OutputParser.Stub();
163 final ExecHelper eh = new ExecHelper(p, repoLoc);
164 eh.run("hg", "commit", "--addremove", "-m", "\"" + message + "\"");
165 if (eh.getExitValue() != 0) {
166 System.out.println(p.result());
167 }
168 Assert.assertEquals(0, eh.getExitValue());
169 }
170
171 private static void sleep(int msBase, int msDelta) {
172 try {
173 Thread.sleep(msBase + Math.round(Math.random() * msDelta));
174 } catch (InterruptedException ex) {
175 // IGNORE
176 }
177 }
53 } 178 }