Skip to content

Commit

Permalink
Test for 'async-batch' call
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcelo Chiaradia committed Feb 11, 2019
1 parent 6b9dcbb commit b600b89
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 0 deletions.
87 changes: 87 additions & 0 deletions src/test/java/com/suse/salt/netapi/calls/CallAsyncEventsTest.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package com.suse.salt.netapi.calls;

import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.any;
import static com.github.tomakehurst.wiremock.client.WireMock.equalTo;
import static com.github.tomakehurst.wiremock.client.WireMock.equalToJson;
import static com.github.tomakehurst.wiremock.client.WireMock.post;
import static com.github.tomakehurst.wiremock.client.WireMock.postRequestedFor;
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
import static com.github.tomakehurst.wiremock.client.WireMock.urlMatching;
import static com.github.tomakehurst.wiremock.client.WireMock.verify;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
Expand All @@ -13,8 +18,10 @@
import com.suse.salt.netapi.client.SaltClient;
import com.suse.salt.netapi.client.impl.HttpAsyncClientImpl;
import com.suse.salt.netapi.datatypes.AuthMethod;
import com.suse.salt.netapi.datatypes.Batch;
import com.suse.salt.netapi.datatypes.Token;
import com.suse.salt.netapi.datatypes.target.Glob;
import com.suse.salt.netapi.datatypes.target.Target;
import com.suse.salt.netapi.errors.GenericError;
import com.suse.salt.netapi.errors.JsonParsingError;
import com.suse.salt.netapi.event.WebSocketEventStream;
Expand All @@ -28,6 +35,7 @@
import org.junit.Rule;
import org.junit.Test;

import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URISyntaxException;
import java.time.Duration;
Expand Down Expand Up @@ -57,6 +65,7 @@ public class CallAsyncEventsTest extends AbstractEventsTest {

private SaltClient client;

@Override
@Before
public void init() throws URISyntaxException, DeploymentException {
super.init();
Expand Down Expand Up @@ -163,4 +172,82 @@ public void testAsyncCall() throws SaltException, InterruptedException {
assertEquals("canceled",
((GenericError) results.get("minion5").error().get()).getMessage());
}

@Test
public void testAsyncBatchCall() throws SaltException, InterruptedException {
stubFor(post(urlMatching("/"))
.withRequestBody(equalToJson(
json("/async_batch_via_event_test_ping_request.json")
))

.willReturn(aResponse()
.withStatus(200)
.withHeader("Content-Type", "application/json")
.withBody(json("/async_via_event_test_ping_response.json"))));

stubFor(post(urlMatching("/"))
.withRequestBody(equalToJson(
json("/async_via_event_list_job_request.json")
))
.willReturn(aResponse()
.withStatus(200)
.withHeader("Content-Type", "application/json")
.withBody(json("/async_via_event_list_job_response.json"))));

EventStream events = new WebSocketEventStream(uri, new Token("token"), 0, 0, 0);

Map<String, CompletionStage<Result<Boolean>>> call =
com.suse.salt.netapi.calls.modules.Test.ping().callAsync(
client,
Glob.ALL,
AUTH,
events,
completeAfter(
new GenericError("canceled"),
Duration.of(7, ChronoUnit.SECONDS)
),
Optional.of(Batch.asAmount(1))
).toCompletableFuture().join();

CountDownLatch countDownLatch = new CountDownLatch(5);
Map<String, Result<Boolean>> results = new HashMap<>();
Map<String, Long> times = new HashMap<>();
call.forEach((key, value) -> {
value.whenComplete((v, e) -> {
if (v != null) {
results.put(key, v);
}
times.put(key, System.currentTimeMillis());
countDownLatch.countDown();
});
});

countDownLatch.await(10, TimeUnit.SECONDS);
assertEquals(5, results.size());
assertTrue(results.get("minion1").result().get());

assertTrue(results.get("minion2").error().get() instanceof JsonParsingError);
assertEquals("Expected BOOLEAN but was STRING at path $",
((JsonParsingError) results.get("minion2").error().get())
.getThrowable().getMessage());

long delay12 = times.get("minion2") - times.get("minion1");
assertTrue(delay12 >= 1000);

assertTrue(results.get("minion3").result().get());

long delay23 = times.get("minion3") - times.get("minion2");
assertTrue(delay23 >= 1000);

assertTrue(results.get("minion4").error().get() instanceof JsonParsingError);
assertEquals("Expected BOOLEAN but was STRING at path $", ((JsonParsingError)
results.get("minion4").error().get()).getThrowable().getMessage());

long delay42 = times.get("minion4") - times.get("minion2");
assertTrue(delay42 >= 1000);

assertTrue(results.get("minion5").error().get() instanceof GenericError);
assertEquals("canceled",
((GenericError) results.get("minion5").error().get()).getMessage());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[
{
"tgt": "*",
"client": "local_async",
"batch": "1",
"fun": "test.ping",
"tgt_type": "glob"
}
]

0 comments on commit b600b89

Please sign in to comment.