Skip to content

Commit

Permalink
Added Resilience4jRateLimiterTest
Browse files Browse the repository at this point in the history
  • Loading branch information
patwlan committed Feb 10, 2018
1 parent a8b49b1 commit d3ea875
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public void setUp() {
}

@Test
public void testSuccessfulCalls() throws Exception {
public void testSuccessfulCall() throws Exception {
final CircuitBreaker.Metrics metrics = circuitBreaker.getMetrics();

setupStub(200);
Expand All @@ -76,7 +76,7 @@ public void testSuccessfulCalls() throws Exception {
}

@Test
public void testFailedCalls() throws Exception {
public void testFailedCall() throws Exception {
final CircuitBreaker.Metrics metrics = circuitBreaker.getMetrics();
boolean exceptionThrown = false;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
*
* Copyright 2018
*
* 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
*
* http://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 io.github.resilience4j.feign;

import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static com.github.tomakehurst.wiremock.client.WireMock.getRequestedFor;
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.verify;
import static java.time.Duration.ofMillis;
import static java.time.Duration.ofSeconds;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

import com.github.tomakehurst.wiremock.junit.WireMockRule;

import feign.FeignException;
import io.github.resilience4j.feign.test.TestService;
import io.github.resilience4j.ratelimiter.RateLimiter;
import io.github.resilience4j.ratelimiter.RateLimiterConfig;
import io.github.resilience4j.ratelimiter.RequestNotPermitted;

/**
* Tests the integration of the {@link Resilience4jFeign} with {@link RateLimiter}
*/
public class Resilience4jRateLimiterTest {

@Rule
public WireMockRule wireMockRule = new WireMockRule();

private static final RateLimiterConfig config = RateLimiterConfig.custom()
.timeoutDuration(ofMillis(50))
.limitRefreshPeriod(ofSeconds(5))
.limitForPeriod(1)
.build();

private TestService testService;
private RateLimiter rateLimiter;

@Before
public void setUp() {
rateLimiter = RateLimiter.of("backendName", config);
final FeignDecorators decorators = FeignDecorators.builder().withRateLimiter(rateLimiter).build();
testService = Resilience4jFeign.builder(decorators).target(TestService.class, "http://localhost:8080/");
}

@Test
public void testSuccessfulCall() throws Exception {
setupStub(200);

testService.greeting();

verify(1, getRequestedFor(urlPathEqualTo("/greeting")));
}

@Test(expected = RequestNotPermitted.class)
public void testRatelimterLimiting() throws Exception {
setupStub(200);

testService.greeting();
testService.greeting();

verify(1, getRequestedFor(urlPathEqualTo("/greeting")));
}

@Test(expected = RequestNotPermitted.class)
public void testRatelimterNotLimiting() throws Exception {
setupStub(200);

testService.greeting();
Thread.sleep(1_000);
testService.greeting();
Thread.sleep(1_000);
testService.greeting();

verify(1, getRequestedFor(urlPathEqualTo("/greeting")));
}

@Test(expected = FeignException.class)
public void testFailedHttpCall() throws Exception {
setupStub(400);

testService.greeting();
}


private void setupStub(int responseCode) {
stubFor(get(urlPathEqualTo("/greeting"))
.willReturn(aResponse()
.withStatus(responseCode)
.withHeader("Content-Type", "text/plain")
.withBody("hello world")));
}
}

0 comments on commit d3ea875

Please sign in to comment.