PageRenderTime 51ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/src/test/java/com/atlassian/bamboo/plugins/git/GitCacheDirectoryTest.java

https://bitbucket.org/atlassian/bamboo-git-plugin
Java | 193 lines | 164 code | 29 blank | 0 comment | 2 complexity | cd6c83916e70a11fa4b6c2b39ea08399 MD5 | raw file
Possible License(s): Apache-2.0
  1. package com.atlassian.bamboo.plugins.git;
  2. import com.atlassian.bamboo.agent.AgentType;
  3. import com.atlassian.bamboo.plan.branch.VcsBranch;
  4. import com.atlassian.bamboo.plan.branch.VcsBranchImpl;
  5. import org.testng.Assert;
  6. import org.testng.annotations.DataProvider;
  7. import org.testng.annotations.Test;
  8. import java.io.File;
  9. import java.lang.reflect.Field;
  10. import java.util.concurrent.ArrayBlockingQueue;
  11. import java.util.concurrent.Callable;
  12. import java.util.concurrent.CountDownLatch;
  13. import java.util.concurrent.TimeUnit;
  14. public class GitCacheDirectoryTest extends GitAbstractTest
  15. {
  16. @DataProvider
  17. Object[][] fieldInfluenceOnCacheLocationNonShallow()
  18. {
  19. return new Object[][] {
  20. {"repositoryUrl", true, null},
  21. {"username", true, null},
  22. {"branch", false, VcsBranch.class},
  23. {"password", false, null},
  24. {"sshKey", false, null},
  25. {"sshPassphrase", false, null},
  26. };
  27. }
  28. @Test(dataProvider = "fieldInfluenceOnCacheLocationNonShallow")
  29. public void testFieldInfluenceOnCacheLocatonNonShallow(final String field, final boolean different, final Class<?> clazz) throws Exception
  30. {
  31. doTestFieldInfluenceOnCacheLocaton(field, false, different, clazz);
  32. }
  33. @DataProvider
  34. Object[][] fieldInfluenceOnCacheLocationShallow()
  35. {
  36. return new Object[][] {
  37. {"repositoryUrl", true, null},
  38. {"username", true, null},
  39. {"branch", false, VcsBranch.class},
  40. {"password", false, null},
  41. {"sshKey", false, null},
  42. {"sshPassphrase", false, null},
  43. };
  44. }
  45. @Test(dataProvider = "fieldInfluenceOnCacheLocationShallow")
  46. public void testFieldInfluenceOnCacheLocatonShallow(String field, boolean different, final Class<?> clazz) throws Exception
  47. {
  48. doTestFieldInfluenceOnCacheLocaton(field, true, different, clazz);
  49. }
  50. private void doTestFieldInfluenceOnCacheLocaton(String field, boolean shallow, boolean different, Class<?> clazz) throws Exception
  51. {
  52. GitRepositoryAccessData accessData = createSampleAccessData(shallow);
  53. GitRepositoryAccessData accessData2 = createSampleAccessData(shallow);
  54. Field f = GitRepositoryAccessData.class.getDeclaredField(field);
  55. f.setAccessible(true);
  56. String val = f.get(accessData2).toString();
  57. if (clazz!=null && VcsBranch.class.isAssignableFrom(clazz))
  58. {
  59. f.set(accessData2, new VcsBranchImpl(val + "chg"));
  60. }
  61. else
  62. {
  63. f.set(accessData2, val + "chg");
  64. }
  65. File baseDir = createTempDirectory();
  66. File cache1 = GitCacheDirectory.getCacheDirectory(baseDir, accessData);
  67. File cache2 = GitCacheDirectory.getCacheDirectory(baseDir, accessData2);
  68. Assert.assertEquals(cache1.equals(cache2), !different);
  69. }
  70. @Test
  71. public void testShallowGetsTheSameCache() throws Exception
  72. {
  73. GitRepositoryAccessData accessData = createSampleAccessData(false);
  74. GitRepositoryAccessData accessData2 = createSampleAccessData(true);
  75. File baseDir = createTempDirectory();
  76. File cache1 = GitCacheDirectory.getCacheDirectory(baseDir, accessData);
  77. File cache2 = GitCacheDirectory.getCacheDirectory(baseDir, accessData2);
  78. Assert.assertTrue(cache1.equals(cache2));
  79. }
  80. private static GitRepositoryAccessData createSampleAccessData(boolean shallow)
  81. {
  82. GitRepositoryAccessData accessData = createAccessData(
  83. "someUrl",
  84. "branch",
  85. "username",
  86. "password",
  87. "sshKey",
  88. "sshPass"
  89. );
  90. return GitRepositoryAccessData.builder(accessData).useShallowClones(shallow).build();
  91. }
  92. @Test(timeOut = 5000)
  93. public void testCallOnSameDirectoryBlocks() throws Exception
  94. {
  95. verifySecondThreadBlocks("repository.url", "repository.url", true);
  96. }
  97. @Test(timeOut = 5000)
  98. public void testCallOnDifferentDirectoryDoesNotBlock() throws Exception
  99. {
  100. verifySecondThreadBlocks("repository.url", "different.url", false);
  101. }
  102. private void verifySecondThreadBlocks(String firstUrl, String secondUrl, boolean blockExpected) throws Exception
  103. {
  104. final GitRepository repository1 = createGitRepository(AgentType.LOCAL);
  105. setRepositoryProperties(repository1, firstUrl, "");
  106. final GitRepository repository2 = createGitRepository(AgentType.LOCAL);
  107. repository2.setWorkingDir(repository1.getWorkingDirectory());
  108. setRepositoryProperties(repository2, secondUrl, "");
  109. final ArrayBlockingQueue<Boolean> hasBlocked = new ArrayBlockingQueue<Boolean>(1);
  110. final CountDownLatch firstCalled = new CountDownLatch(1);
  111. final CountDownLatch secondCalled = new CountDownLatch(1);
  112. Thread firstThread = new Thread("First thread") {
  113. @Override
  114. public void run()
  115. {
  116. try
  117. {
  118. File cacheDirectory = repository1.getCacheDirectory();
  119. GitCacheDirectory.getCacheLock(cacheDirectory).withLock(new Callable<Void>()
  120. {
  121. public Void call() throws Exception
  122. {
  123. firstCalled.countDown();
  124. boolean await = secondCalled.await(1000, TimeUnit.MILLISECONDS);
  125. hasBlocked.put(!await); // await false = timeout
  126. return null;
  127. }
  128. });
  129. }
  130. catch (Exception e)
  131. {
  132. throw new RuntimeException(e);
  133. }
  134. }
  135. };
  136. firstThread.start();
  137. Assert.assertTrue(firstCalled.await(1000, TimeUnit.MILLISECONDS), "First thread should be let in promptly");
  138. Thread secondThread = new Thread("Second thread") {
  139. @Override
  140. public void run()
  141. {
  142. try
  143. {
  144. File cacheDirectory = repository2.getCacheDirectory();
  145. GitCacheDirectory.getCacheLock(cacheDirectory).withLock(new Callable<Void>()
  146. {
  147. public Void call() throws Exception
  148. {
  149. secondCalled.countDown();
  150. return null;
  151. }
  152. });
  153. }
  154. catch (Exception e)
  155. {
  156. throw new RuntimeException(e);
  157. }
  158. }
  159. };
  160. secondThread.start();
  161. Assert.assertEquals(hasBlocked.take(), Boolean.valueOf(blockExpected), "Second thread blocking");
  162. Assert.assertTrue(secondCalled.await(2000, TimeUnit.MILLISECONDS), "Second thread should be eventually let in");
  163. }
  164. }