]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-10104 update IT to support concurrent and async webhook calls
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Tue, 21 Nov 2017 16:09:45 +0000 (17:09 +0100)
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Fri, 24 Nov 2017 08:23:58 +0000 (09:23 +0100)
tests/src/test/java/org/sonarqube/tests/webhook/ExternalServer.java
tests/src/test/java/org/sonarqube/tests/webhook/WebhooksTest.java

index 33301c85e3b7f82d664eb366cb1d8d12ca522f95..988a308b6fc21f11b15bb4415715545132e83753 100644 (file)
@@ -34,12 +34,14 @@ import org.eclipse.jetty.server.Server;
 import org.eclipse.jetty.server.handler.AbstractHandler;
 import org.junit.rules.ExternalResource;
 
+import static java.util.Collections.synchronizedList;
+
 /**
  * This web server listens to requests sent by webhooks
  */
 class ExternalServer extends ExternalResource {
   private final Server jetty;
-  private final List<PayloadRequest> payloads = new ArrayList<>();
+  private final List<PayloadRequest> payloads = synchronizedList(new ArrayList<>());
 
   ExternalServer() {
     jetty = new Server(0);
index 16d2ec83f881b08eef9d6341f7e824c8e64b581f..003c3eba941a03f3ac22347193f3f49ff7fbecd5 100644 (file)
@@ -79,7 +79,7 @@ public class WebhooksTest {
   }
 
   @Test
-  public void call_multiple_global_and_project_webhooks_when_analysis_is_done() {
+  public void call_multiple_global_and_project_webhooks_when_analysis_is_done() throws InterruptedException {
     orchestrator.getServer().provisionProject(PROJECT_KEY, PROJECT_NAME);
     enableGlobalWebhooks(
       new Webhook("Jenkins", externalServer.urlFor("/jenkins")),
@@ -90,6 +90,7 @@ public class WebhooksTest {
     analyseProject();
 
     // the same payload has been sent to three servers
+    waitUntilAllWebHooksCalled(3);
     assertThat(externalServer.getPayloadRequests()).hasSize(3);
     PayloadRequest request = externalServer.getPayloadRequests().get(0);
     for (int i = 1; i < 3; i++) {
@@ -159,7 +160,7 @@ public class WebhooksTest {
    * Restrict calls to ten webhooks per type (global or project)
    */
   @Test
-  public void do_not_become_a_denial_of_service_attacker() {
+  public void do_not_become_a_denial_of_service_attacker() throws InterruptedException {
     orchestrator.getServer().provisionProject(PROJECT_KEY, PROJECT_NAME);
 
     List<Webhook> globalWebhooks = range(0, 15).mapToObj(i -> new Webhook("G" + i, externalServer.urlFor("/global"))).collect(Collectors.toList());
@@ -170,6 +171,7 @@ public class WebhooksTest {
     analyseProject();
 
     // only the first ten global webhooks and ten project webhooks are called
+    waitUntilAllWebHooksCalled(10 + 10);
     assertThat(externalServer.getPayloadRequests()).hasSize(10 + 10);
     assertThat(externalServer.getPayloadRequestsOnPath("/global")).hasSize(10);
     assertThat(externalServer.getPayloadRequestsOnPath("/project")).hasSize(10);
@@ -287,4 +289,16 @@ public class WebhooksTest {
     }
   }
 
+  /**
+   * Wait up to 30 seconds
+   */
+  private static void waitUntilAllWebHooksCalled(int expectedNumberOfRequests) throws InterruptedException {
+    for (int i = 0; i < 60; i++) {
+      if (externalServer.getPayloadRequests().size() == expectedNumberOfRequests) {
+        break;
+      }
+      Thread.sleep(500);
+    }
+  }
+
 }