mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-05-30 02:22:11 +00:00
deps: Refactor dependencies (#3224)
* remove spring dep move junit, logging, mockito under dep mgmt * upgrade anti-corruption-layer deps * async method invocation * balking, bloc * bridge to bytecode * caching * callback - cqrs * component - health check * hexagonal - metadata mapping * rest of the patterns * remove checkstyle, take spotless into use
This commit is contained in:
@@ -34,6 +34,14 @@
|
||||
</parent>
|
||||
<artifactId>object-pool</artifactId>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-api</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>ch.qos.logback</groupId>
|
||||
<artifactId>logback-classic</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-engine</artifactId>
|
||||
|
||||
@@ -39,9 +39,7 @@ public abstract class ObjectPool<T> {
|
||||
|
||||
protected abstract T create();
|
||||
|
||||
/**
|
||||
* Checkout object from pool.
|
||||
*/
|
||||
/** Checkout object from pool. */
|
||||
public synchronized T checkOut() {
|
||||
if (available.isEmpty()) {
|
||||
available.add(create());
|
||||
|
||||
@@ -28,20 +28,15 @@ import java.util.concurrent.atomic.AtomicInteger;
|
||||
import lombok.Getter;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* Oliphaunts are expensive to create.
|
||||
*/
|
||||
/** Oliphaunts are expensive to create. */
|
||||
@Slf4j
|
||||
public class Oliphaunt {
|
||||
|
||||
private static final AtomicInteger counter = new AtomicInteger(0);
|
||||
|
||||
@Getter
|
||||
private final int id;
|
||||
@Getter private final int id;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*/
|
||||
/** Constructor. */
|
||||
public Oliphaunt() {
|
||||
id = counter.incrementAndGet();
|
||||
try {
|
||||
|
||||
@@ -24,9 +24,7 @@
|
||||
*/
|
||||
package com.iluwatar.object.pool;
|
||||
|
||||
/**
|
||||
* Oliphaunt object pool.
|
||||
*/
|
||||
/** Oliphaunt object pool. */
|
||||
public class OliphauntPool extends ObjectPool<Oliphaunt> {
|
||||
|
||||
@Override
|
||||
|
||||
@@ -28,13 +28,11 @@ import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* Application test.
|
||||
*/
|
||||
/** Application test. */
|
||||
class AppTest {
|
||||
|
||||
@Test
|
||||
void shouldExecuteApplicationWithoutException() {
|
||||
assertDoesNotThrow(() -> App.main(new String[]{}));
|
||||
assertDoesNotThrow(() -> App.main(new String[] {}));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,9 +34,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import java.util.List;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* OliphauntPoolTest.
|
||||
*/
|
||||
/** OliphauntPoolTest. */
|
||||
class OliphauntPoolTest {
|
||||
|
||||
/**
|
||||
@@ -45,27 +43,29 @@ class OliphauntPoolTest {
|
||||
*/
|
||||
@Test
|
||||
void testSubsequentCheckinCheckout() {
|
||||
assertTimeout(ofMillis(5000), () -> {
|
||||
final var pool = new OliphauntPool();
|
||||
assertEquals("Pool available=0 inUse=0", pool.toString());
|
||||
assertTimeout(
|
||||
ofMillis(5000),
|
||||
() -> {
|
||||
final var pool = new OliphauntPool();
|
||||
assertEquals("Pool available=0 inUse=0", pool.toString());
|
||||
|
||||
final var expectedOliphaunt = pool.checkOut();
|
||||
assertEquals("Pool available=0 inUse=1", pool.toString());
|
||||
final var expectedOliphaunt = pool.checkOut();
|
||||
assertEquals("Pool available=0 inUse=1", pool.toString());
|
||||
|
||||
pool.checkIn(expectedOliphaunt);
|
||||
assertEquals("Pool available=1 inUse=0", pool.toString());
|
||||
pool.checkIn(expectedOliphaunt);
|
||||
assertEquals("Pool available=1 inUse=0", pool.toString());
|
||||
|
||||
for (int i = 0; i < 100; i++) {
|
||||
final var oliphaunt = pool.checkOut();
|
||||
assertEquals("Pool available=0 inUse=1", pool.toString());
|
||||
assertSame(expectedOliphaunt, oliphaunt);
|
||||
assertEquals(expectedOliphaunt.getId(), oliphaunt.getId());
|
||||
assertEquals(expectedOliphaunt.toString(), oliphaunt.toString());
|
||||
for (int i = 0; i < 100; i++) {
|
||||
final var oliphaunt = pool.checkOut();
|
||||
assertEquals("Pool available=0 inUse=1", pool.toString());
|
||||
assertSame(expectedOliphaunt, oliphaunt);
|
||||
assertEquals(expectedOliphaunt.getId(), oliphaunt.getId());
|
||||
assertEquals(expectedOliphaunt.toString(), oliphaunt.toString());
|
||||
|
||||
pool.checkIn(oliphaunt);
|
||||
assertEquals("Pool available=1 inUse=0", pool.toString());
|
||||
}
|
||||
});
|
||||
pool.checkIn(oliphaunt);
|
||||
assertEquals("Pool available=1 inUse=0", pool.toString());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -74,50 +74,51 @@ class OliphauntPoolTest {
|
||||
*/
|
||||
@Test
|
||||
void testConcurrentCheckinCheckout() {
|
||||
assertTimeout(ofMillis(5000), () -> {
|
||||
final var pool = new OliphauntPool();
|
||||
assertEquals(pool.toString(), "Pool available=0 inUse=0");
|
||||
assertTimeout(
|
||||
ofMillis(5000),
|
||||
() -> {
|
||||
final var pool = new OliphauntPool();
|
||||
assertEquals(pool.toString(), "Pool available=0 inUse=0");
|
||||
|
||||
final var firstOliphaunt = pool.checkOut();
|
||||
assertEquals(pool.toString(), "Pool available=0 inUse=1");
|
||||
final var firstOliphaunt = pool.checkOut();
|
||||
assertEquals(pool.toString(), "Pool available=0 inUse=1");
|
||||
|
||||
final var secondOliphaunt = pool.checkOut();
|
||||
assertEquals(pool.toString(), "Pool available=0 inUse=2");
|
||||
final var secondOliphaunt = pool.checkOut();
|
||||
assertEquals(pool.toString(), "Pool available=0 inUse=2");
|
||||
|
||||
assertNotSame(firstOliphaunt, secondOliphaunt);
|
||||
assertEquals(firstOliphaunt.getId() + 1, secondOliphaunt.getId());
|
||||
assertNotSame(firstOliphaunt, secondOliphaunt);
|
||||
assertEquals(firstOliphaunt.getId() + 1, secondOliphaunt.getId());
|
||||
|
||||
// After checking in the second, we should get the same when checking out a new oliphaunt ...
|
||||
pool.checkIn(secondOliphaunt);
|
||||
assertEquals(pool.toString(), "Pool available=1 inUse=1");
|
||||
// After checking in the second, we should get the same when checking out a new oliphaunt
|
||||
// ...
|
||||
pool.checkIn(secondOliphaunt);
|
||||
assertEquals(pool.toString(), "Pool available=1 inUse=1");
|
||||
|
||||
final var oliphaunt3 = pool.checkOut();
|
||||
assertEquals(pool.toString(), "Pool available=0 inUse=2");
|
||||
assertSame(secondOliphaunt, oliphaunt3);
|
||||
final var oliphaunt3 = pool.checkOut();
|
||||
assertEquals(pool.toString(), "Pool available=0 inUse=2");
|
||||
assertSame(secondOliphaunt, oliphaunt3);
|
||||
|
||||
// ... and the same applies for the first one
|
||||
pool.checkIn(firstOliphaunt);
|
||||
assertEquals(pool.toString(), "Pool available=1 inUse=1");
|
||||
// ... and the same applies for the first one
|
||||
pool.checkIn(firstOliphaunt);
|
||||
assertEquals(pool.toString(), "Pool available=1 inUse=1");
|
||||
|
||||
final var oliphaunt4 = pool.checkOut();
|
||||
assertEquals(pool.toString(), "Pool available=0 inUse=2");
|
||||
assertSame(firstOliphaunt, oliphaunt4);
|
||||
final var oliphaunt4 = pool.checkOut();
|
||||
assertEquals(pool.toString(), "Pool available=0 inUse=2");
|
||||
assertSame(firstOliphaunt, oliphaunt4);
|
||||
|
||||
// When both oliphaunt return to the pool, we should still get the same instances
|
||||
pool.checkIn(firstOliphaunt);
|
||||
assertEquals(pool.toString(), "Pool available=1 inUse=1");
|
||||
// When both oliphaunt return to the pool, we should still get the same instances
|
||||
pool.checkIn(firstOliphaunt);
|
||||
assertEquals(pool.toString(), "Pool available=1 inUse=1");
|
||||
|
||||
pool.checkIn(secondOliphaunt);
|
||||
assertEquals(pool.toString(), "Pool available=2 inUse=0");
|
||||
pool.checkIn(secondOliphaunt);
|
||||
assertEquals(pool.toString(), "Pool available=2 inUse=0");
|
||||
|
||||
// The order of the returned instances is not determined, so just put them in a list
|
||||
// and verify if both expected instances are in there.
|
||||
final var oliphaunts = List.of(pool.checkOut(), pool.checkOut());
|
||||
assertEquals(pool.toString(), "Pool available=0 inUse=2");
|
||||
assertTrue(oliphaunts.contains(firstOliphaunt));
|
||||
assertTrue(oliphaunts.contains(secondOliphaunt));
|
||||
});
|
||||
// The order of the returned instances is not determined, so just put them in a list
|
||||
// and verify if both expected instances are in there.
|
||||
final var oliphaunts = List.of(pool.checkOut(), pool.checkOut());
|
||||
assertEquals(pool.toString(), "Pool available=0 inUse=2");
|
||||
assertTrue(oliphaunts.contains(firstOliphaunt));
|
||||
assertTrue(oliphaunts.contains(secondOliphaunt));
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user