|
|
|
@@ -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));
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|