Skip to content

Commit

Permalink
#38 Test enhanecements
Browse files Browse the repository at this point in the history
  • Loading branch information
vladimir-bukhtoyarov committed Feb 8, 2022
1 parent e84a76a commit de2eceb
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 32 deletions.
Expand Up @@ -11,10 +11,7 @@

import java.time.Duration;
import java.util.Optional;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.*;
import java.util.function.Function;
import java.util.function.Supplier;

Expand Down Expand Up @@ -114,6 +111,76 @@ public void testBucketRemoval() {
assertFalse(proxyManager.getProxyConfiguration(key).isPresent());
}

@Test
public void testParallelInitialization() throws InterruptedException {
BucketConfiguration configuration = BucketConfiguration.builder()
.addLimit(Bandwidth.classic(10, Refill.intervally(1, Duration.ofMinutes(1))))
.build();

int PARALLELISM = 4;
CountDownLatch startLatch = new CountDownLatch(PARALLELISM);
CountDownLatch stopLatch = new CountDownLatch(PARALLELISM);
for (int i = 0; i < PARALLELISM; i++) {
new Thread(() -> {
startLatch.countDown();
try {
startLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
try {
proxyManager.builder().build(key, () -> configuration).tryConsume(1);
} finally {
stopLatch.countDown();
}
}).start();
}
stopLatch.await();

BucketProxy bucket = proxyManager.builder().build(key, () -> configuration);
assertEquals(10 - PARALLELISM, bucket.getAvailableTokens());
}

@Test
public void testAsyncParallelInitialization() throws InterruptedException {
if (!proxyManager.isAsyncModeSupported()) {
return;
}

final BucketConfiguration configuration = BucketConfiguration.builder()
.addLimit(Bandwidth.classic(10, Refill.intervally(1, Duration.ofMinutes(1))))
.build();

int PARALLELISM = 4;
CountDownLatch startLatch = new CountDownLatch(PARALLELISM);
CountDownLatch stopLatch = new CountDownLatch(PARALLELISM);
for (int i = 0; i < PARALLELISM; i++) {
new Thread(() -> {
startLatch.countDown();
try {
startLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
try {
try {
proxyManager.asAsync().builder().build(key, () -> CompletableFuture.completedFuture(configuration)).tryConsume(1).get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
} finally {
stopLatch.countDown();
}
}).start();
}
stopLatch.await();

BucketProxy bucket = proxyManager.builder().build(key, () -> configuration);
assertEquals(10 - PARALLELISM, bucket.getAvailableTokens());
}

@Test
public void testUnconditionalConsume() throws Exception {
BucketConfiguration configuration = BucketConfiguration.builder()
Expand Down
Expand Up @@ -51,20 +51,6 @@ public static void initializeInstance() throws SQLException {
proxyManager = new PostgreSQLProxyManager(configuration);
}

@Test
public void testBucketRemoval() {
Long key = 1L;
BucketConfiguration configuration = BucketConfiguration.builder()
.addLimit(Bandwidth.simple(4, Duration.ofHours(1)))
.build();
BucketProxy bucket = proxyManager.builder().build(key, configuration);
bucket.getAvailableTokens();

assertTrue(proxyManager.getProxyConfiguration(key).isPresent());
proxyManager.removeProxy(key);
assertFalse(proxyManager.getProxyConfiguration(key).isPresent());
}

@Override
protected ProxyManager<Long> getProxyManager() {
return proxyManager;
Expand Down
Expand Up @@ -61,20 +61,6 @@ protected Long generateRandomKey() {
return ThreadLocalRandom.current().nextLong(1_000_000_000);
}

@Test
public void testBucketRemoval() {
Long key = 1L;
BucketConfiguration configuration = BucketConfiguration.builder()
.addLimit(Bandwidth.simple(4, Duration.ofHours(1)))
.build();
BucketProxy bucket = proxyManager.builder().build(key, configuration);
bucket.getAvailableTokens();

assertTrue(proxyManager.getProxyConfiguration(key).isPresent());
proxyManager.removeProxy(key);
assertFalse(proxyManager.getProxyConfiguration(key).isPresent());
}

@AfterClass
public static void shutdown() {
if (container != null) {
Expand Down

0 comments on commit de2eceb

Please sign in to comment.