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);
}
@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")),
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++) {
* 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());
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);
}
}
+ /**
+ * 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);
+ }
+ }
+
}