Migrate gcm-sender-async tests to JUnit 5

This commit is contained in:
Chris Eager 2021-12-20 17:41:48 -08:00 committed by Chris Eager
parent 5e0cca0702
commit c488c14d25
4 changed files with 61 additions and 63 deletions

View File

@ -49,4 +49,3 @@
</dependencies> </dependencies>
</project> </project>

View File

@ -4,17 +4,16 @@
*/ */
package org.whispersystems.gcm.server; package org.whispersystems.gcm.server;
import org.junit.Test; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.whispersystems.gcm.server.util.JsonHelpers.jsonFixture;
import java.io.IOException; import java.io.IOException;
import org.junit.jupiter.api.Test;
import static org.junit.Assert.assertEquals;
import static org.whispersystems.gcm.server.util.JsonHelpers.jsonFixture;
public class MessageTest { public class MessageTest {
@Test @Test
public void testMinimal() throws IOException { void testMinimal() throws IOException {
Message message = Message.newBuilder() Message message = Message.newBuilder()
.withDestination("1") .withDestination("1")
.build(); .build();
@ -23,7 +22,7 @@ public class MessageTest {
} }
@Test @Test
public void testComplete() throws IOException { void testComplete() throws IOException {
Message message = Message.newBuilder() Message message = Message.newBuilder()
.withDestination("1") .withDestination("1")
.withCollapseKey("collapse") .withCollapseKey("collapse")
@ -36,7 +35,7 @@ public class MessageTest {
} }
@Test @Test
public void testWithData() throws IOException { void testWithData() throws IOException {
Message message = Message.newBuilder() Message message = Message.newBuilder()
.withDestination("2") .withDestination("2")
.withDataPart("key1", "value1") .withDataPart("key1", "value1")

View File

@ -12,12 +12,11 @@ import static com.github.tomakehurst.wiremock.client.WireMock.equalTo;
import static com.github.tomakehurst.wiremock.client.WireMock.ok; import static com.github.tomakehurst.wiremock.client.WireMock.ok;
import static com.github.tomakehurst.wiremock.client.WireMock.postRequestedFor; import static com.github.tomakehurst.wiremock.client.WireMock.postRequestedFor;
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo; import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
import static com.github.tomakehurst.wiremock.client.WireMock.verify; import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.Assert.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.Assert.assertFalse; import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.Assert.assertNull; import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.Assert.assertTrue;
import static org.whispersystems.gcm.server.util.FixtureHelpers.fixture; import static org.whispersystems.gcm.server.util.FixtureHelpers.fixture;
import static org.whispersystems.gcm.server.util.JsonHelpers.jsonFixture; import static org.whispersystems.gcm.server.util.JsonHelpers.jsonFixture;
@ -26,19 +25,21 @@ import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.tomakehurst.wiremock.client.CountMatchingStrategy; import com.github.tomakehurst.wiremock.client.CountMatchingStrategy;
import com.github.tomakehurst.wiremock.junit.WireMockRule; import com.github.tomakehurst.wiremock.junit5.WireMockExtension;
import java.io.IOException; import java.io.IOException;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException; import java.util.concurrent.TimeoutException;
import org.junit.Rule; import org.junit.jupiter.api.Test;
import org.junit.Test; import org.junit.jupiter.api.extension.RegisterExtension;
public class SenderTest { class SenderTest {
@Rule @RegisterExtension
public WireMockRule wireMock = new WireMockRule(options().dynamicPort().dynamicHttpsPort()); private final WireMockExtension wireMock = WireMockExtension.newInstance()
.options(wireMockConfig().dynamicPort().dynamicHttpsPort())
.build();
private static final ObjectMapper mapper = new ObjectMapper(); private static final ObjectMapper mapper = new ObjectMapper();
@ -49,14 +50,14 @@ public class SenderTest {
} }
@Test @Test
public void testSuccess() throws InterruptedException, ExecutionException, TimeoutException, IOException { void testSuccess() throws InterruptedException, ExecutionException, TimeoutException, IOException {
wireMock.stubFor(any(anyUrl()) wireMock.stubFor(any(anyUrl())
.willReturn(aResponse() .willReturn(aResponse()
.withStatus(200) .withStatus(200)
.withBody(fixture("fixtures/response-success.json")))); .withBody(fixture("fixtures/response-success.json"))));
Sender sender = new Sender("foobarbaz", mapper, 10, "http://localhost:" + wireMock.port() + "/gcm/send"); Sender sender = new Sender("foobarbaz", mapper, 10, "http://localhost:" + wireMock.getPort() + "/gcm/send");
CompletableFuture<Result> future = sender.send(Message.newBuilder().withDestination("1").build()); CompletableFuture<Result> future = sender.send(Message.newBuilder().withDestination("1").build());
Result result = future.get(10, TimeUnit.SECONDS); Result result = future.get(10, TimeUnit.SECONDS);
@ -68,19 +69,19 @@ public class SenderTest {
assertNull(result.getError()); assertNull(result.getError());
assertNull(result.getCanonicalRegistrationId()); assertNull(result.getCanonicalRegistrationId());
verify(1, postRequestedFor(urlEqualTo("/gcm/send")) wireMock.verify(1, postRequestedFor(urlEqualTo("/gcm/send"))
.withHeader("Authorization", equalTo("key=foobarbaz")) .withHeader("Authorization", equalTo("key=foobarbaz"))
.withHeader("Content-Type", equalTo("application/json")) .withHeader("Content-Type", equalTo("application/json"))
.withRequestBody(equalTo(jsonFixture("fixtures/message-minimal.json")))); .withRequestBody(equalTo(jsonFixture("fixtures/message-minimal.json"))));
} }
@Test @Test
public void testBadApiKey() throws InterruptedException, TimeoutException { void testBadApiKey() throws InterruptedException, TimeoutException {
wireMock.stubFor(any(anyUrl()) wireMock.stubFor(any(anyUrl())
.willReturn(aResponse() .willReturn(aResponse()
.withStatus(401))); .withStatus(401)));
Sender sender = new Sender("foobar", mapper, 10, "http://localhost:" + wireMock.port() + "/gcm/send"); Sender sender = new Sender("foobar", mapper, 10, "http://localhost:" + wireMock.getPort() + "/gcm/send");
CompletableFuture<Result> future = sender.send(Message.newBuilder().withDestination("1").build()); CompletableFuture<Result> future = sender.send(Message.newBuilder().withDestination("1").build());
try { try {
@ -90,16 +91,16 @@ public class SenderTest {
assertTrue(ee.getCause() instanceof AuthenticationFailedException); assertTrue(ee.getCause() instanceof AuthenticationFailedException);
} }
verify(1, anyRequestedFor(anyUrl())); wireMock.verify(1, anyRequestedFor(anyUrl()));
} }
@Test @Test
public void testBadRequest() throws TimeoutException, InterruptedException { void testBadRequest() throws TimeoutException, InterruptedException {
wireMock.stubFor(any(anyUrl()) wireMock.stubFor(any(anyUrl())
.willReturn(aResponse() .willReturn(aResponse()
.withStatus(400))); .withStatus(400)));
Sender sender = new Sender("foobarbaz", mapper, 10, "http://localhost:" + wireMock.port() + "/gcm/send"); Sender sender = new Sender("foobarbaz", mapper, 10, "http://localhost:" + wireMock.getPort() + "/gcm/send");
CompletableFuture<Result> future = sender.send(Message.newBuilder().withDestination("1").build()); CompletableFuture<Result> future = sender.send(Message.newBuilder().withDestination("1").build());
try { try {
@ -109,16 +110,16 @@ public class SenderTest {
assertTrue(e.getCause() instanceof InvalidRequestException); assertTrue(e.getCause() instanceof InvalidRequestException);
} }
verify(1, anyRequestedFor(anyUrl())); wireMock.verify(1, anyRequestedFor(anyUrl()));
} }
@Test @Test
public void testServerError() throws TimeoutException, InterruptedException { void testServerError() throws TimeoutException, InterruptedException {
wireMock.stubFor(any(anyUrl()) wireMock.stubFor(any(anyUrl())
.willReturn(aResponse() .willReturn(aResponse()
.withStatus(503))); .withStatus(503)));
Sender sender = new Sender("foobarbaz", mapper, 3, "http://localhost:" + wireMock.port() + "/gcm/send"); Sender sender = new Sender("foobarbaz", mapper, 3, "http://localhost:" + wireMock.getPort() + "/gcm/send");
CompletableFuture<Result> future = sender.send(Message.newBuilder().withDestination("1").build()); CompletableFuture<Result> future = sender.send(Message.newBuilder().withDestination("1").build());
try { try {
@ -128,15 +129,15 @@ public class SenderTest {
assertTrue(ee.getCause() instanceof ServerFailedException); assertTrue(ee.getCause() instanceof ServerFailedException);
} }
verify(3, anyRequestedFor(anyUrl())); wireMock.verify(3, anyRequestedFor(anyUrl()));
} }
@Test @Test
public void testServerErrorRecovery() throws InterruptedException, ExecutionException, TimeoutException { void testServerErrorRecovery() throws InterruptedException, ExecutionException, TimeoutException {
wireMock.stubFor(any(anyUrl()).willReturn(aResponse().withStatus(503))); wireMock.stubFor(any(anyUrl()).willReturn(aResponse().withStatus(503)));
Sender sender = new Sender("foobarbaz", mapper, 4, "http://localhost:" + wireMock.port() + "/gcm/send"); Sender sender = new Sender("foobarbaz", mapper, 4, "http://localhost:" + wireMock.getPort() + "/gcm/send");
CompletableFuture<Result> future = sender.send(Message.newBuilder().withDestination("1").build()); CompletableFuture<Result> future = sender.send(Message.newBuilder().withDestination("1").build());
// up to three failures can happen, with 100ms exponential backoff // up to three failures can happen, with 100ms exponential backoff
@ -150,7 +151,7 @@ public class SenderTest {
Result result = future.get(10, TimeUnit.SECONDS); Result result = future.get(10, TimeUnit.SECONDS);
verify(new CountMatchingStrategy(CountMatchingStrategy.GREATER_THAN, 1), anyRequestedFor(anyUrl())); wireMock.verify(new CountMatchingStrategy(CountMatchingStrategy.GREATER_THAN, 1), anyRequestedFor(anyUrl()));
assertTrue(result.isSuccess()); assertTrue(result.isSuccess());
assertFalse(result.isThrottled()); assertFalse(result.isThrottled());
assertFalse(result.isUnregistered()); assertFalse(result.isUnregistered());
@ -160,14 +161,14 @@ public class SenderTest {
} }
@Test @Test
public void testNetworkError() throws TimeoutException, InterruptedException { void testNetworkError() throws TimeoutException, InterruptedException {
wireMock.stubFor(any(anyUrl()) wireMock.stubFor(any(anyUrl())
.willReturn(ok())); .willReturn(ok()));
Sender sender = new Sender("foobarbaz", mapper ,2, "http://localhost:" + wireMock.port() + "/gcm/send"); Sender sender = new Sender("foobarbaz", mapper ,2, "http://localhost:" + wireMock.getPort() + "/gcm/send");
wireMock.stop(); wireMock.getRuntimeInfo().getWireMock().shutdown();
CompletableFuture<Result> future = sender.send(Message.newBuilder().withDestination("1").build()); CompletableFuture<Result> future = sender.send(Message.newBuilder().withDestination("1").build());
@ -179,12 +180,12 @@ public class SenderTest {
} }
@Test @Test
public void testNotRegistered() throws InterruptedException, ExecutionException, TimeoutException { void testNotRegistered() throws InterruptedException, ExecutionException, TimeoutException {
wireMock.stubFor(any(anyUrl()).willReturn(aResponse().withStatus(200) wireMock.stubFor(any(anyUrl()).willReturn(aResponse().withStatus(200)
.withBody(fixture("fixtures/response-not-registered.json")))); .withBody(fixture("fixtures/response-not-registered.json"))));
Sender sender = new Sender("foobarbaz", mapper,2, "http://localhost:" + wireMock.port() + "/gcm/send"); Sender sender = new Sender("foobarbaz", mapper,2, "http://localhost:" + wireMock.getPort() + "/gcm/send");
CompletableFuture<Result> future = sender.send(Message.newBuilder() CompletableFuture<Result> future = sender.send(Message.newBuilder()
.withDestination("2") .withDestination("2")
.withDataPart("message", "new message!") .withDataPart("message", "new message!")

View File

@ -6,32 +6,33 @@ package org.whispersystems.gcm.server;
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.post; import static com.github.tomakehurst.wiremock.client.WireMock.post;
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo; import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;
import static junit.framework.TestCase.assertTrue; import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.whispersystems.gcm.server.util.FixtureHelpers.fixture; import static org.whispersystems.gcm.server.util.FixtureHelpers.fixture;
import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor; import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.tomakehurst.wiremock.core.WireMockConfiguration; import com.github.tomakehurst.wiremock.junit5.WireMockExtension;
import com.github.tomakehurst.wiremock.junit.WireMockRule;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException; import java.util.concurrent.TimeoutException;
import org.junit.Ignore; import org.junit.jupiter.api.Disabled;
import org.junit.Rule; import org.junit.jupiter.api.Test;
import org.junit.Test; import org.junit.jupiter.api.extension.RegisterExtension;
public class SimultaneousSenderTest { class SimultaneousSenderTest {
@Rule @RegisterExtension
public WireMockRule wireMock = new WireMockRule(WireMockConfiguration.options().dynamicPort().dynamicHttpsPort()); private final WireMockExtension wireMock = WireMockExtension.newInstance()
.options(wireMockConfig().dynamicPort().dynamicHttpsPort())
.build();
private static final ObjectMapper mapper = new ObjectMapper(); private static final ObjectMapper mapper = new ObjectMapper();
@ -42,13 +43,13 @@ public class SimultaneousSenderTest {
} }
@Test @Test
public void testSimultaneousSuccess() throws TimeoutException, InterruptedException, ExecutionException, JsonProcessingException { void testSimultaneousSuccess() throws TimeoutException, InterruptedException, ExecutionException {
stubFor(post(urlPathEqualTo("/gcm/send")) wireMock.stubFor(post(urlPathEqualTo("/gcm/send"))
.willReturn(aResponse() .willReturn(aResponse()
.withStatus(200) .withStatus(200)
.withBody(fixture("fixtures/response-success.json")))); .withBody(fixture("fixtures/response-success.json"))));
Sender sender = new Sender("foobarbaz", mapper, 2, "http://localhost:" + wireMock.port() + "/gcm/send"); Sender sender = new Sender("foobarbaz", mapper, 2, "http://localhost:" + wireMock.getPort() + "/gcm/send");
List<CompletableFuture<Result>> results = new LinkedList<>(); List<CompletableFuture<Result>> results = new LinkedList<>();
for (int i=0;i<1000;i++) { for (int i=0;i<1000;i++) {
@ -65,13 +66,13 @@ public class SimultaneousSenderTest {
} }
@Test @Test
@Ignore @Disabled
public void testSimultaneousFailure() throws TimeoutException, InterruptedException { void testSimultaneousFailure() {
stubFor(post(urlPathEqualTo("/gcm/send")) wireMock.stubFor(post(urlPathEqualTo("/gcm/send"))
.willReturn(aResponse() .willReturn(aResponse()
.withStatus(503))); .withStatus(503)));
Sender sender = new Sender("foobarbaz", mapper, 2, "http://localhost:" + wireMock.port() + "/gcm/send"); Sender sender = new Sender("foobarbaz", mapper, 2, "http://localhost:" + wireMock.getPort() + "/gcm/send");
List<CompletableFuture<Result>> futures = new LinkedList<>(); List<CompletableFuture<Result>> futures = new LinkedList<>();
for (int i=0;i<1000;i++) { for (int i=0;i<1000;i++) {
@ -79,11 +80,9 @@ public class SimultaneousSenderTest {
} }
for (CompletableFuture<Result> future : futures) { for (CompletableFuture<Result> future : futures) {
try { final ExecutionException e = assertThrows(ExecutionException.class, () -> future.get(60, TimeUnit.SECONDS));
Result result = future.get(60, TimeUnit.SECONDS);
} catch (ExecutionException e) { assertTrue(e.getCause() instanceof ServerFailedException, e.getCause().toString());
assertTrue(e.getCause().toString(), e.getCause() instanceof ServerFailedException);
}
} }
} }
} }