-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4966 from ghubstan/10-callrate-interceptor
Prevent excessive api calls
- Loading branch information
Showing
41 changed files
with
2,027 additions
and
324 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
129 changes: 129 additions & 0 deletions
129
apitest/src/test/java/bisq/apitest/method/CallRateMeteringInterceptorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
/* | ||
* This file is part of Bisq. | ||
* | ||
* Bisq is free software: you can redistribute it and/or modify it | ||
* under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or (at | ||
* your option) any later version. | ||
* | ||
* Bisq is distributed in the hope that it will be useful, but WITHOUT | ||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public | ||
* License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with Bisq. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package bisq.apitest.method; | ||
|
||
import io.grpc.StatusRuntimeException; | ||
|
||
import java.io.File; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
|
||
import org.junit.jupiter.api.AfterAll; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Disabled; | ||
import org.junit.jupiter.api.MethodOrderer; | ||
import org.junit.jupiter.api.Order; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.TestMethodOrder; | ||
|
||
import static bisq.apitest.Scaffold.BitcoinCoreApp.bitcoind; | ||
import static bisq.apitest.config.BisqAppConfig.alicedaemon; | ||
import static java.util.concurrent.TimeUnit.DAYS; | ||
import static java.util.concurrent.TimeUnit.HOURS; | ||
import static java.util.concurrent.TimeUnit.MINUTES; | ||
import static java.util.concurrent.TimeUnit.SECONDS; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
|
||
|
||
import bisq.daemon.grpc.GrpcVersionService; | ||
import bisq.daemon.grpc.interceptor.GrpcServiceRateMeteringConfig; | ||
|
||
@Disabled | ||
@Slf4j | ||
@TestMethodOrder(MethodOrderer.OrderAnnotation.class) | ||
public class CallRateMeteringInterceptorTest extends MethodTest { | ||
|
||
private static final GetVersionTest getVersionTest = new GetVersionTest(); | ||
|
||
@BeforeAll | ||
public static void setUp() { | ||
File callRateMeteringConfigFile = buildInterceptorConfigFile(); | ||
startSupportingApps(callRateMeteringConfigFile, | ||
false, | ||
false, | ||
bitcoind, alicedaemon); | ||
} | ||
|
||
@BeforeEach | ||
public void sleep200Milliseconds() { | ||
sleep(200); | ||
} | ||
|
||
@Test | ||
@Order(1) | ||
public void testGetVersionCall1IsAllowed() { | ||
getVersionTest.testGetVersion(); | ||
} | ||
|
||
@Test | ||
@Order(2) | ||
public void testGetVersionCall2ShouldThrowException() { | ||
Throwable exception = assertThrows(StatusRuntimeException.class, getVersionTest::testGetVersion); | ||
assertEquals("PERMISSION_DENIED: the maximum allowed number of getversion calls (1/second) has been exceeded", | ||
exception.getMessage()); | ||
} | ||
|
||
@Test | ||
@Order(3) | ||
public void testGetVersionCall3ShouldThrowException() { | ||
Throwable exception = assertThrows(StatusRuntimeException.class, getVersionTest::testGetVersion); | ||
assertEquals("PERMISSION_DENIED: the maximum allowed number of getversion calls (1/second) has been exceeded", | ||
exception.getMessage()); | ||
} | ||
|
||
@Test | ||
@Order(4) | ||
public void testGetVersionCall4IsAllowed() { | ||
sleep(1100); // Let the server's rate meter reset the call count. | ||
getVersionTest.testGetVersion(); | ||
} | ||
|
||
@AfterAll | ||
public static void tearDown() { | ||
tearDownScaffold(); | ||
} | ||
|
||
public static File buildInterceptorConfigFile() { | ||
GrpcServiceRateMeteringConfig.Builder builder = new GrpcServiceRateMeteringConfig.Builder(); | ||
builder.addCallRateMeter(GrpcVersionService.class.getSimpleName(), | ||
"getVersion", | ||
1, | ||
SECONDS); | ||
builder.addCallRateMeter(GrpcVersionService.class.getSimpleName(), | ||
"shouldNotBreakAnything", | ||
1000, | ||
DAYS); | ||
// Only GrpcVersionService is @VisibleForTesting, so we hardcode the class names. | ||
builder.addCallRateMeter("GrpcOffersService", | ||
"createOffer", | ||
5, | ||
MINUTES); | ||
builder.addCallRateMeter("GrpcOffersService", | ||
"takeOffer", | ||
10, | ||
DAYS); | ||
builder.addCallRateMeter("GrpcTradesService", | ||
"withdrawFunds", | ||
3, | ||
HOURS); | ||
return builder.build(); | ||
} | ||
} |
Oops, something went wrong.