Skip to content

Commit

Permalink
releaseb fixing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Sammers21 committed Jan 8, 2025
1 parent 13712b7 commit e8e9a47
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 17 deletions.
8 changes: 7 additions & 1 deletion src/io/github/sammers/pla/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import io.github.sammers.pla.logic.CharacterCache;
import io.github.sammers.pla.logic.Ladder;
import io.github.sammers.pla.logic.Refs;
import io.prometheus.metrics.core.metrics.Gauge;
import io.prometheus.metrics.exporter.httpserver.HTTPServer;
import io.prometheus.metrics.instrumentation.jvm.JvmMetrics;
import io.reactivex.Scheduler;
Expand Down Expand Up @@ -81,7 +82,12 @@ public static void main(String[] args) throws IOException {
final Refs refs = new Refs();
final Map<String, Cutoffs> cutoffsMap = new ConcurrentHashMap<>();
final ExtCharacterSearcher extSearch = new ExtCharacterSearcher(webClient);
final BlizzardAPI blizzardAPI = new BlizzardAPI(clientId, clientSecret, webClient, refs, characterCache, cutoffsMap);
Gauge permitsMetric = Gauge.builder()
.name("RateLimiterPermits")
.help("How many permits are left in the rate limiter")
.labelNames("name")
.register();
final BlizzardAPI blizzardAPI = new BlizzardAPI(permitsMetric, clientId, clientSecret, webClient, refs, characterCache, cutoffsMap);
DB db = new DB(mongoClient);
Ladder ladder = new Ladder(webClient, db, blizzardAPI, characterCache, refs, cutoffsMap);
ladder.start();
Expand Down
10 changes: 6 additions & 4 deletions src/io/github/sammers/pla/blizzard/BlizzardAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import io.github.sammers.pla.logic.CharacterCache;
import io.github.sammers.pla.logic.RateLimiter;
import io.github.sammers.pla.logic.Refs;
import io.prometheus.metrics.core.metrics.Gauge;
import io.reactivex.Completable;
import io.reactivex.Maybe;
import io.reactivex.Single;
Expand Down Expand Up @@ -46,17 +47,18 @@ public class BlizzardAPI {
private final Map<String, Cutoffs> cutoffs;
private final String clientId;
private final AtomicReference<BlizzardAuthToken> token = new AtomicReference<>();
private final RateLimiter rateLimiter = new RateLimiter("100 per sec", 100, TimeUnit.SECONDS, 1000,
Optional.of(new RateLimiter("36000 per hr", 36000, TimeUnit.HOURS, 1000, Optional.empty(), Main.VTHREAD_SCHEDULER)),
Main.VTHREAD_SCHEDULER);
private final RateLimiter rateLimiter;

public BlizzardAPI(String clientId, String clientSecret, WebClient webClient, Refs refs, CharacterCache characterCache, Map<String, Cutoffs> cutoffs) {
public BlizzardAPI(Gauge permits, String clientId, String clientSecret, WebClient webClient, Refs refs, CharacterCache characterCache, Map<String, Cutoffs> cutoffs) {
this.clientId = clientId;
this.clientSecret = clientSecret;
this.webClient = webClient;
this.refs = refs;
this.characterCache = characterCache;
this.cutoffs = cutoffs;
rateLimiter = new RateLimiter("100 per sec", permits, 100, TimeUnit.SECONDS, 1000,
Optional.of(new RateLimiter("36000 per hr", permits, 36000, TimeUnit.HOURS, 1000, Optional.empty(), Main.VTHREAD_SCHEDULER)),
Main.VTHREAD_SCHEDULER);
}

public Completable rpsToken() {
Expand Down
9 changes: 3 additions & 6 deletions src/io/github/sammers/pla/logic/RateLimiter.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,10 @@ public class RateLimiter {
private final ConcurrentLinkedQueue<CompletableEmitter> requestQes = new ConcurrentLinkedQueue<>();
private final int maxRequestsTotal;
private final Optional<RateLimiter> parent;
private final Gauge permitsMetric = Gauge.builder()
.name("RateLimiterPermits")
.help("How many permits are left in the rate limiter")
.labelNames("name")
.register();
private final Gauge permitsMetric;

public RateLimiter(String name, int permits, TimeUnit per, int maxRequestsTotal, Optional<RateLimiter> parent, Scheduler scheduler) {
public RateLimiter(String name, Gauge permitsMetric, int permits, TimeUnit per, int maxRequestsTotal, Optional<RateLimiter> parent, Scheduler scheduler) {
this.permitsMetric = permitsMetric;
this.maxRequestsTotal = maxRequestsTotal;
this.parent = parent;
long now = System.currentTimeMillis();
Expand Down
18 changes: 12 additions & 6 deletions test/io/github/sammers/pla/logic/RateLimiterTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.github.sammers.pla.logic;

import io.prometheus.metrics.core.metrics.Gauge;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import io.reactivex.Completable;
Expand All @@ -13,13 +14,18 @@
import java.util.ArrayList;

public class RateLimiterTest {

public static final Scheduler VTHREAD_EXECUTOR = io.reactivex.schedulers.Schedulers.from(Executors.newVirtualThreadPerTaskExecutor());
public static final Gauge permitsMetric = Gauge.builder()
.name("RateLimiterPermits")
.help("How many permits are left in the rate limiter")
.labelNames("name")
.register();

@Test
public void basic() {
Long start = System.currentTimeMillis();
RateLimiter woWAPIRateLimiter = new RateLimiter(2, TimeUnit.SECONDS, 100, Optional.empty(), VTHREAD_EXECUTOR);
RateLimiter woWAPIRateLimiter = new RateLimiter("Test", permitsMetric, 2, TimeUnit.SECONDS, 100, Optional.empty(), VTHREAD_EXECUTOR);
Completable.merge(
List.of(
woWAPIRateLimiter.request(),
Expand All @@ -40,7 +46,7 @@ public void basic() {
@Test
public void basicWithHr() {
Long start = System.currentTimeMillis();
RateLimiter woWAPIRateLimiter = new RateLimiter(36000, TimeUnit.HOURS, 100, Optional.empty(), VTHREAD_EXECUTOR);
RateLimiter woWAPIRateLimiter = new RateLimiter("Test", permitsMetric, 36000, TimeUnit.HOURS, 100, Optional.empty(), VTHREAD_EXECUTOR);
Completable.merge(
List.of(
woWAPIRateLimiter.request(),
Expand All @@ -61,9 +67,9 @@ public void basicWithHr() {
@Test
public void complexBlizzardLike() {
Long start = System.currentTimeMillis();
RateLimiter woWAPIRateLimiter = new RateLimiter(10, TimeUnit.SECONDS, 100,
Optional.of(new RateLimiter(10, TimeUnit.SECONDS, 100, Optional.empty(), VTHREAD_EXECUTOR)),
VTHREAD_EXECUTOR);
RateLimiter woWAPIRateLimiter = new RateLimiter("Test", permitsMetric, 10, TimeUnit.SECONDS, 100,
Optional.of(new RateLimiter("Test", permitsMetric, 10, TimeUnit.SECONDS, 100, Optional.empty(), VTHREAD_EXECUTOR)),
VTHREAD_EXECUTOR);
List<Completable> x = new ArrayList<>();
for (int i = 0; i < 10; i++) {
x.add(woWAPIRateLimiter.request());
Expand Down

0 comments on commit e8e9a47

Please sign in to comment.