PageRenderTime 52ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/src/test/java/com/atlassian/bamboo/plugins/git/timeouts/TimeoutsTest.java

https://bitbucket.org/atlassian/bamboo-git-plugin
Java | 130 lines | 111 code | 16 blank | 3 comment | 2 complexity | 6e5332f50e0ca5635ba69d2d9fb970c0 MD5 | raw file
Possible License(s): Apache-2.0
  1. package com.atlassian.bamboo.plugins.git.timeouts;
  2. import com.atlassian.bamboo.build.logger.NullBuildLogger;
  3. import com.atlassian.bamboo.plugins.git.GitAbstractTest;
  4. import com.atlassian.bamboo.plugins.git.GitOperationHelper;
  5. import com.atlassian.bamboo.plugins.git.JGitOperationHelper;
  6. import com.atlassian.bamboo.repository.RepositoryException;
  7. import com.atlassian.sal.api.message.I18nResolver;
  8. import org.testng.annotations.AfterClass;
  9. import org.testng.annotations.BeforeClass;
  10. import org.testng.annotations.DataProvider;
  11. import org.testng.annotations.Test;
  12. import java.io.File;
  13. import java.io.IOException;
  14. import java.lang.reflect.Field;
  15. import java.net.ServerSocket;
  16. import java.net.Socket;
  17. import java.util.ArrayList;
  18. import java.util.Collection;
  19. import java.util.Collections;
  20. import static org.mockito.Mockito.mock;
  21. /**
  22. * This test class is not intended to be run with other test classes - run it manually when solving timeout-related issues.
  23. */
  24. @Test(enabled = false, groups = "manual")
  25. public class TimeoutsTest extends GitAbstractTest
  26. {
  27. private Thread servingThread;
  28. private ServerSocket serverSocket;
  29. private Collection<Socket> connectedSockets = Collections.synchronizedCollection(new ArrayList<Socket>());
  30. @BeforeClass
  31. public void setUp() throws Exception
  32. {
  33. Field timeout = GitOperationHelper.class.getDeclaredField("DEFAULT_TRANSFER_TIMEOUT");
  34. timeout.setAccessible(true);
  35. timeout.setInt(null, 1);
  36. serverSocket = new ServerSocket(0);
  37. servingThread = new Thread()
  38. {
  39. @Override
  40. public void run()
  41. {
  42. try
  43. {
  44. for (;;)
  45. {
  46. connectedSockets.add(serverSocket.accept());
  47. }
  48. }
  49. catch (IOException e)
  50. {
  51. e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
  52. }
  53. }
  54. };
  55. servingThread.start();
  56. }
  57. @AfterClass
  58. public void tearDown() throws Exception
  59. {
  60. servingThread.stop();
  61. serverSocket.close();
  62. for (Socket connectedSocket : connectedSockets)
  63. {
  64. connectedSocket.close();
  65. }
  66. }
  67. public GitOperationHelper createGitOperationHelper()
  68. {
  69. I18nResolver i18nResolver = mock(I18nResolver.class);
  70. return new JGitOperationHelper(null,
  71. new NullBuildLogger()
  72. {
  73. @Override
  74. public String addBuildLogEntry(String logString)
  75. {
  76. System.out.println(logString);
  77. return null;
  78. }
  79. },
  80. i18nResolver);
  81. }
  82. @Test
  83. public void testTimeoutIsSufficientToCheckOutBigRepo() throws Exception
  84. {
  85. GitOperationHelper helper = createJGitOperationHelper(createAccessData("git://git.jetbrains.org/idea/community.git"));
  86. String targetRevision = helper.obtainLatestRevision();
  87. File directory = createTempDirectory();
  88. System.out.println(directory);
  89. helper.fetch(directory, targetRevision, false);
  90. helper.checkout(null, directory, targetRevision, null);
  91. }
  92. @DataProvider
  93. Object[][] urlsToHang()
  94. {
  95. return new String[][] {
  96. {"ssh://localhost:" + serverSocket.getLocalPort() + "/path/to/repo"},
  97. {"http://localhost:" + serverSocket.getLocalPort() + "/path/to/repo"},
  98. {"git://localhost:" + serverSocket.getLocalPort() + "/path/to/repo"},
  99. };
  100. }
  101. @Test(dataProvider = "urlsToHang", expectedExceptions = RepositoryException.class, timeOut = 5000)
  102. public void testTimeoutOnObtainingLatestRevision(String url) throws Exception
  103. {
  104. String rev = createJGitOperationHelper(createAccessData(url)).obtainLatestRevision();
  105. }
  106. @Test(dataProvider = "urlsToHang", expectedExceptions = RepositoryException.class, timeOut = 5000)
  107. public void testTimeoutOnFetch(String url) throws Exception
  108. {
  109. File directory = createTempDirectory();
  110. String targetRevision = createJGitOperationHelper(createAccessData(url)).obtainLatestRevision();
  111. createJGitOperationHelper(createAccessData(url)).fetch(directory, targetRevision, false);
  112. createJGitOperationHelper(createAccessData(url)).checkout(null, directory, targetRevision, null);
  113. }
  114. }