/plugins/svn4idea/testSource/org/jetbrains/idea/svn/SvnCachingRepositoryPoolTest.java

https://bitbucket.org/nbargnesi/idea · Java · 243 lines · 195 code · 20 blank · 28 comment · 6 complexity · b9cc31838e2488eb49da4c80f1d14fdb MD5 · raw file

  1. /*
  2. * Copyright 2000-2012 JetBrains s.r.o.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.jetbrains.idea.svn;
  17. import com.intellij.openapi.progress.EmptyProgressIndicator;
  18. import com.intellij.openapi.progress.ProgressManager;
  19. import com.intellij.openapi.progress.impl.ProgressManagerImpl;
  20. import com.intellij.testFramework.vcs.FileBasedTest;
  21. import com.intellij.util.ThrowableConvertor;
  22. import com.intellij.util.concurrency.Semaphore;
  23. import junit.framework.Assert;
  24. import org.jetbrains.idea.svn.lowLevel.ApplicationLevelNumberConnectionsGuardImpl;
  25. import org.jetbrains.idea.svn.lowLevel.CachingSvnRepositoryPool;
  26. import org.jetbrains.idea.svn.lowLevel.SvnIdeaRepositoryPoolManager;
  27. import org.junit.Test;
  28. import org.tmatesoft.svn.core.SVNException;
  29. import org.tmatesoft.svn.core.SVNURL;
  30. import org.tmatesoft.svn.core.io.ISVNSession;
  31. import org.tmatesoft.svn.core.io.SVNRepository;
  32. import java.util.Map;
  33. import java.util.Random;
  34. /**
  35. * Created with IntelliJ IDEA.
  36. * User: Irina.Chernushina
  37. * Date: 8/3/12
  38. * Time: 5:37 PM
  39. */
  40. public class SvnCachingRepositoryPoolTest extends FileBasedTest {
  41. @Test
  42. public void testRepositoriesAreClosed() throws Exception {
  43. final SvnIdeaRepositoryPoolManager poolManager = new SvnIdeaRepositoryPoolManager(true, null, null);
  44. testBigFlow(poolManager, true);
  45. }
  46. @Test
  47. public void testCloseWorker() throws Exception {
  48. final SvnIdeaRepositoryPoolManager poolManager = new SvnIdeaRepositoryPoolManager(true, null, null);
  49. final ApplicationLevelNumberConnectionsGuardImpl guard = SvnIdeaRepositoryPoolManager.getOurGuard();
  50. guard.setDelay(20);
  51. ((CachingSvnRepositoryPool) poolManager.getPool()).setConnectionTimeout(20);
  52. testBigFlow(poolManager, false);
  53. try {
  54. Thread.sleep(50);
  55. } catch (InterruptedException e) {
  56. //
  57. }
  58. Assert.assertEquals(0, guard.getCurrentlyActiveConnections());
  59. final CachingSvnRepositoryPool pool = (CachingSvnRepositoryPool) poolManager.getPool();
  60. Map<String,CachingSvnRepositoryPool.RepoGroup> groups = pool.getGroups();
  61. Assert.assertEquals(1, groups.size());
  62. CachingSvnRepositoryPool.RepoGroup group = groups.values().iterator().next();
  63. Assert.assertEquals(0, group.getUsedSize());
  64. Assert.assertEquals(0, group.getInactiveSize()); // !!!
  65. poolManager.dispose();
  66. checkAfterDispose(poolManager);
  67. }
  68. @Test
  69. public void testCancel() throws Exception {
  70. final SvnIdeaRepositoryPoolManager poolManager = new SvnIdeaRepositoryPoolManager(true, null, null, 1, 1);
  71. final SVNURL url = SVNURL.parseURIEncoded("http://a.b.c");
  72. poolManager.setCreator(new ThrowableConvertor<SVNURL, SVNRepository, SVNException>() {
  73. @Override
  74. public SVNRepository convert(SVNURL svnurl) throws SVNException {
  75. return new MockSvnRepository(svnurl, ISVNSession.DEFAULT);
  76. }
  77. });
  78. final MockSvnRepository repository1 = (MockSvnRepository)poolManager.createRepository(url, true);
  79. final Semaphore semaphore = new Semaphore();
  80. semaphore.down();
  81. poolManager.setCreator(new ThrowableConvertor<SVNURL, SVNRepository, SVNException>() {
  82. @Override
  83. public SVNRepository convert(SVNURL svnurl) throws SVNException {
  84. semaphore.waitFor();
  85. return new MockSvnRepository(svnurl, ISVNSession.DEFAULT);
  86. }
  87. });
  88. final SVNException[] exc = new SVNException[1];
  89. final Runnable target = new Runnable() {
  90. @Override
  91. public void run() {
  92. try {
  93. final MockSvnRepository repository = (MockSvnRepository)poolManager.createRepository(url, true);
  94. repository.fireConnectionClosed();
  95. }
  96. catch (SVNException e) {
  97. e.printStackTrace();
  98. exc[0] = e;
  99. }
  100. }
  101. };
  102. final EmptyProgressIndicator indicator = new EmptyProgressIndicator() {
  103. @Override
  104. public void cancel() {
  105. super.cancel();
  106. ProgressManagerImpl.canceled();
  107. }
  108. };
  109. Thread thread = new Thread(new Runnable() {
  110. @Override
  111. public void run() {
  112. ((ProgressManagerImpl)ProgressManager.getInstance()).executeProcessUnderProgress(target, indicator);
  113. }
  114. });
  115. thread.start();
  116. try {
  117. Thread.sleep(10);
  118. } catch (InterruptedException e) {
  119. //
  120. }
  121. Assert.assertTrue(thread.isAlive());
  122. indicator.cancel();
  123. final Object obj = new Object();
  124. while (! timeout(System.currentTimeMillis()) && thread.isAlive()) {
  125. synchronized (obj) {
  126. try {
  127. obj.wait(300);
  128. } catch (InterruptedException e) {
  129. //
  130. }
  131. }
  132. }
  133. Assert.assertTrue(!thread.isAlive());
  134. Assert.assertNotNull(exc[0]);
  135. //repository1.fireConnectionClosed(); // also test that used are also closed.. in dispose
  136. poolManager.dispose();
  137. checkAfterDispose(poolManager);
  138. }
  139. private void checkAfterDispose(SvnIdeaRepositoryPoolManager poolManager) {
  140. final ApplicationLevelNumberConnectionsGuardImpl guard = SvnIdeaRepositoryPoolManager.getOurGuard();
  141. Assert.assertEquals(0, guard.getCurrentlyActiveConnections());
  142. final CachingSvnRepositoryPool pool = (CachingSvnRepositoryPool) poolManager.getPool();
  143. Map<String,CachingSvnRepositoryPool.RepoGroup> groups = pool.getGroups();
  144. Assert.assertEquals(1, groups.size());
  145. CachingSvnRepositoryPool.RepoGroup group = groups.values().iterator().next();
  146. Assert.assertEquals(0, group.getUsedSize());
  147. Assert.assertEquals(0, guard.getInstanceCount());
  148. Assert.assertEquals(0, guard.getCurrentlyOpenedCount());
  149. Assert.assertEquals(0, group.getUsedSize());
  150. Assert.assertEquals(0, group.getInactiveSize());
  151. }
  152. private void testBigFlow(final SvnIdeaRepositoryPoolManager poolManager, boolean disposeAfter) throws SVNException {
  153. poolManager.setCreator(new ThrowableConvertor<SVNURL, SVNRepository, SVNException>() {
  154. @Override
  155. public SVNRepository convert(SVNURL svnurl) throws SVNException {
  156. return new MockSvnRepository(svnurl, ISVNSession.DEFAULT);
  157. }
  158. });
  159. final SVNURL url = SVNURL.parseURIEncoded("http://a.b.c");
  160. final Random random = new Random(System.currentTimeMillis() & 0x00ff);
  161. final int[] cnt = new int[1];
  162. cnt[0] = 25;
  163. final SVNException[] exc = new SVNException[1];
  164. for (int i = 0; i < 25; i++) {
  165. Runnable target = new Runnable() {
  166. @Override
  167. public void run() {
  168. MockSvnRepository repository = null;
  169. try {
  170. repository = (MockSvnRepository)poolManager.createRepository(url, true);
  171. }
  172. catch (SVNException e) {
  173. e.printStackTrace();
  174. exc[0] = e;
  175. return;
  176. }
  177. repository.fireConnectionOpened();
  178. try {
  179. Thread.sleep(random.nextInt(10));
  180. }
  181. catch (InterruptedException e) {
  182. //
  183. }
  184. repository.fireConnectionClosed();
  185. synchronized (cnt) {
  186. -- cnt[0];
  187. }
  188. }
  189. };
  190. Thread thread = new Thread(target);
  191. thread.start();
  192. }
  193. final long start = System.currentTimeMillis();
  194. synchronized (cnt) {
  195. while (cnt[0] > 0 && ! timeout(start)) {
  196. try {
  197. cnt.wait(5);
  198. } catch (InterruptedException e) {
  199. //
  200. }
  201. }
  202. }
  203. Assert.assertEquals(0, cnt[0]);
  204. // test no open repositories, but may have inactive
  205. final ApplicationLevelNumberConnectionsGuardImpl guard = SvnIdeaRepositoryPoolManager.getOurGuard();
  206. Assert.assertEquals(0, guard.getCurrentlyActiveConnections());
  207. final CachingSvnRepositoryPool pool = (CachingSvnRepositoryPool) poolManager.getPool();
  208. Map<String,CachingSvnRepositoryPool.RepoGroup> groups = pool.getGroups();
  209. Assert.assertEquals(1, groups.size());
  210. CachingSvnRepositoryPool.RepoGroup group = groups.values().iterator().next();
  211. Assert.assertEquals(0, group.getUsedSize());
  212. if (disposeAfter) {
  213. poolManager.dispose();
  214. Assert.assertEquals(0, guard.getCurrentlyActiveConnections());
  215. Assert.assertEquals(0, guard.getInstanceCount());
  216. Assert.assertEquals(0, group.getUsedSize());
  217. Assert.assertEquals(0, group.getInactiveSize());
  218. }
  219. }
  220. private boolean timeout(long start) {
  221. return System.currentTimeMillis() - start > 10000;
  222. }
  223. }