You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MultiBrowserTestWithProxy.java 2.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. package com.vaadin.tests.tb3;
  2. import com.vaadin.testbench.parallel.TestCategory;
  3. import org.junit.After;
  4. import java.io.IOException;
  5. import java.util.concurrent.atomic.AtomicInteger;
  6. @TestCategory("push")
  7. public abstract class MultiBrowserTestWithProxy extends MultiBrowserTest {
  8. private static AtomicInteger availablePort = new AtomicInteger(2000);
  9. private SimpleProxy proxySession;
  10. private Integer proxyPort = null;
  11. @Override
  12. public void setup() throws Exception {
  13. super.setup();
  14. connectProxy();
  15. }
  16. @After
  17. public void teardownProxy() {
  18. disconnectProxy();
  19. }
  20. protected Integer getProxyPort() {
  21. if (proxyPort == null) {
  22. // Assumes we can use any port >= 2000,
  23. // except for 2049 in Firefox...
  24. proxyPort = availablePort.addAndGet(1);
  25. if (proxyPort == 2049) {
  26. // Restricted in Firefox, see
  27. // http://www-archive.mozilla.org/projects/netlib/PortBanning.html#portlist
  28. proxyPort = availablePort.addAndGet(1);
  29. }
  30. }
  31. return proxyPort;
  32. }
  33. /**
  34. * Disconnects the proxy if active
  35. */
  36. protected void disconnectProxy() {
  37. if (proxySession == null) {
  38. return;
  39. }
  40. proxySession.disconnect();
  41. proxySession = null;
  42. }
  43. /**
  44. * Ensure the proxy is active. Does nothing if the proxy is already active.
  45. */
  46. protected void connectProxy() throws IOException {
  47. if (proxySession != null) {
  48. return;
  49. }
  50. for (int i = 0; i < 10; i++) {
  51. // Potential workaround for problem with establishing many ssh
  52. // connections at the same time
  53. try {
  54. createProxy(getProxyPort());
  55. break;
  56. } catch (IOException e) {
  57. sleep(500);
  58. if (i == 9) {
  59. throw new RuntimeException(
  60. "All 10 attempts to connect a proxy failed", e);
  61. }
  62. }
  63. }
  64. }
  65. private void createProxy(int proxyPort) throws IOException {
  66. proxySession = new SimpleProxy(proxyPort, getDeploymentHostname(),
  67. getDeploymentPort());
  68. proxySession.start();
  69. }
  70. @Override
  71. protected String getBaseURL() {
  72. return "http://" + getDeploymentHostname() + ":" + getProxyPort();
  73. }
  74. }