Browse Source

Migrates some tests to junit jupiter.

Also replaces custom redis rule with Testcontainers
pull/1841/head
spencergibb 4 years ago
parent
commit
f997ea0ad2
No known key found for this signature in database
GPG Key ID: 7788A47380690861
  1. 14
      pom.xml
  2. 4
      spring-cloud-gateway-core/pom.xml
  3. 10
      spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/filter/factory/rewrite/ModifyRequestBodyGatewayFilterFactorySslTimeoutTests.java
  4. 25
      spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/filter/ratelimit/RedisRateLimiterTests.java
  5. 105
      spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/test/support/redis/RedisRule.java

14
pom.xml

@ -51,12 +51,12 @@ @@ -51,12 +51,12 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<embedded-redis.version>0.6</embedded-redis.version>
<blockhound.version>1.0.3.RELEASE</blockhound.version>
<java.version>1.8</java.version>
<junit-pioneer.version>0.6.0</junit-pioneer.version>
<spring-cloud-circuitbreaker.version>2.0.0-SNAPSHOT</spring-cloud-circuitbreaker.version>
<spring-cloud-commons.version>3.0.0-SNAPSHOT</spring-cloud-commons.version>
<testcontainers.version>1.14.3</testcontainers.version>
</properties>
<dependencyManagement>
@ -98,11 +98,6 @@ @@ -98,11 +98,6 @@
<artifactId>spring-boot-devtools</artifactId>
<version>${spring-boot.version}</version>
</dependency>
<dependency>
<groupId>com.github.kstyrc</groupId>
<artifactId>embedded-redis</artifactId>
<version>${embedded-redis.version}</version>
</dependency>
<dependency>
<groupId>io.projectreactor.tools</groupId>
<artifactId>blockhound-junit-platform</artifactId>
@ -113,6 +108,13 @@ @@ -113,6 +108,13 @@
<artifactId>junit-pioneer</artifactId>
<version>0.6.0</version>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers-bom</artifactId>
<version>${testcontainers.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

4
spring-cloud-gateway-core/pom.xml

@ -127,8 +127,8 @@ @@ -127,8 +127,8 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.kstyrc</groupId>
<artifactId>embedded-redis</artifactId>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

10
spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/filter/factory/rewrite/ModifyRequestBodyGatewayFilterFactorySslTimeoutTests.java

@ -24,9 +24,8 @@ import io.netty.handler.ssl.SslContext; @@ -24,9 +24,8 @@ import io.netty.handler.ssl.SslContext;
import io.netty.handler.ssl.SslContextBuilder;
import io.netty.handler.ssl.util.InsecureTrustManagerFactory;
import io.netty.util.internal.PlatformDependent;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junitpioneer.jupiter.RepeatFailedTest;
import reactor.core.publisher.Mono;
import reactor.netty.http.client.HttpClient;
@ -50,7 +49,6 @@ import org.springframework.http.client.reactive.ReactorClientHttpConnector; @@ -50,7 +49,6 @@ import org.springframework.http.client.reactive.ReactorClientHttpConnector;
import org.springframework.http.codec.ServerCodecConfigurer;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.server.ServerWebExchange;
@ -60,7 +58,6 @@ import static org.springframework.boot.test.context.SpringBootTest.WebEnvironmen @@ -60,7 +58,6 @@ import static org.springframework.boot.test.context.SpringBootTest.WebEnvironmen
/**
* @author fangfeikun
*/
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = RANDOM_PORT,
properties = { "spring.cloud.gateway.httpclient.ssl.handshake-timeout=1ms",
"spring.main.allow-bean-definition-overriding=true" })
@ -72,7 +69,7 @@ public class ModifyRequestBodyGatewayFilterFactorySslTimeoutTests @@ -72,7 +69,7 @@ public class ModifyRequestBodyGatewayFilterFactorySslTimeoutTests
@Autowired
AtomicInteger releaseCount;
@Before
@BeforeEach
public void setup() {
try {
SslContext sslContext = SslContextBuilder.forClient()
@ -97,7 +94,6 @@ public class ModifyRequestBodyGatewayFilterFactorySslTimeoutTests @@ -97,7 +94,6 @@ public class ModifyRequestBodyGatewayFilterFactorySslTimeoutTests
.jsonPath("message").isEqualTo("handshake timed out after 1ms");
}
@Test
@RepeatFailedTest(3)
public void modifyRequestBodyRelease() {
releaseCount.set(0);

25
spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/filter/ratelimit/RedisRateLimiterTests.java

@ -18,12 +18,13 @@ package org.springframework.cloud.gateway.filter.ratelimit; @@ -18,12 +18,13 @@ package org.springframework.cloud.gateway.filter.ratelimit;
import java.util.UUID;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junitpioneer.jupiter.RepeatFailedTest;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringBootConfiguration;
@ -31,10 +32,8 @@ import org.springframework.boot.autoconfigure.EnableAutoConfiguration; @@ -31,10 +32,8 @@ import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cloud.gateway.filter.ratelimit.RateLimiter.Response;
import org.springframework.cloud.gateway.test.BaseWebClientTests;
import org.springframework.cloud.gateway.test.support.redis.RedisRule;
import org.springframework.context.annotation.Import;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringRunner;
import static org.assertj.core.api.Assertions.assertThat;
import static org.hamcrest.Matchers.is;
@ -50,28 +49,28 @@ import static org.springframework.boot.test.context.SpringBootTest.WebEnvironmen @@ -50,28 +49,28 @@ import static org.springframework.boot.test.context.SpringBootTest.WebEnvironmen
* @author Ronny Bräunlich
* @author Denis Cutic
*/
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = RANDOM_PORT)
@DirtiesContext
@Testcontainers
public class RedisRateLimiterTests extends BaseWebClientTests {
@Rule
public final RedisRule redis = RedisRule.bindToDefaultPort();
@Container
public GenericContainer redis = new GenericContainer<>("redis:5.0.9-alpine")
.withExposedPorts(6379);
@Autowired
private RedisRateLimiter rateLimiter;
@Before
@BeforeEach
public void setUp() throws Exception {
assumeThat("Ignore on Circle", System.getenv("CIRCLECI"), is(nullValue()));
}
@After
@AfterEach
public void tearDown() throws Exception {
rateLimiter.setIncludeHeaders(true);
}
@Test
@RepeatFailedTest(3)
public void redisRateLimiterWorks() throws Exception {
String id = UUID.randomUUID().toString();

105
spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/test/support/redis/RedisRule.java

@ -1,105 +0,0 @@ @@ -1,105 +0,0 @@
/*
* Copyright 2018-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.gateway.test.support.redis;
import java.io.IOException;
import java.net.ServerSocket;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.rules.ExternalResource;
import redis.embedded.RedisServer;
import static java.lang.String.format;
import static java.util.stream.IntStream.range;
public final class RedisRule extends ExternalResource {
public static final int DEFAULT_REDIS_PORT = 6379;
private final int port;
private final boolean ignoreDefaultPortFailure;
private Log log = LogFactory.getLog(getClass());
private RedisServer redisServer;
private RedisRule(int port) {
this(port, false);
}
private RedisRule(int port, boolean ignoreDefaultPortFailure) {
this.port = port;
this.ignoreDefaultPortFailure = ignoreDefaultPortFailure;
}
public static RedisRule bindToDefaultPort() {
return new RedisRule(DEFAULT_REDIS_PORT, true);
}
public static RedisRule bindToDefaultPort(int port) {
return new RedisRule(port);
}
public static RedisRule bindToFirstOpenPort(int startInclusive, int endExclusive) {
return new RedisRule(findOpenPort(startInclusive, endExclusive));
}
private static int findOpenPort(final int startInclusive, final int endExclusive) {
return range(startInclusive, endExclusive).filter(RedisRule::testPort).findFirst()
.orElseThrow(() -> new IllegalStateException(
format("No open port found in the range [%d, %d]", startInclusive,
endExclusive)));
}
private static boolean testPort(int port) {
try {
new ServerSocket(port).close();
return true;
}
catch (final IOException ex) {
return false;
}
}
@Override
protected void before() {
try {
redisServer = RedisServer.builder().port(port).setting("maxmemory 16MB")
.build();
redisServer.start();
}
catch (final Exception e) {
if (port == DEFAULT_REDIS_PORT && ignoreDefaultPortFailure) {
log.info(
"Unable to start embedded Redis on default port. Ignoring error. Assuming redis is already running.");
}
else {
throw new RuntimeException(format(
"Error while initializing the Redis server" + " on port %d",
port), e);
}
}
}
@Override
protected void after() {
redisServer.stop();
}
}
Loading…
Cancel
Save