Skip to content
Permalink
Browse files
Refactor so much duplication of the same pattern.
  • Loading branch information
Gary Gregory committed Aug 13, 2021
1 parent 51f63c9 commit 9bdf2ee689fe7458f98b4221f2a78767a456edb7
Show file tree
Hide file tree
Showing 9 changed files with 137 additions and 304 deletions.
@@ -45,14 +45,10 @@ public Waiter(final boolean active, final boolean valid, final long latency) {
}

/**
* Wait for {@link #getLatency()} ms.
* Wait for {@link #getLatency()} milliseconds.
*/
public void doWait() {
try {
Thread.sleep(latency);
} catch (final InterruptedException ex) {
// ignore
}
sleepQuietly(latency);
}

@Override
@@ -175,4 +171,13 @@ public String toString() {
buff.append("latency = " + latency + '\n');
return buff.toString();
}

/** TODO Reuse Apache Commons Lang ThreadUtils */
public static void sleepQuietly(final long millis) {
try {
Thread.sleep(millis);
} catch (final InterruptedException e) {
// be quiet
}
}
}
@@ -131,11 +131,7 @@ protected void doWait(final long latency) {
if (latency == 0) {
return;
}
try {
Thread.sleep(latency);
} catch (final InterruptedException ex) {
// ignore
}
Waiter.sleepQuietly(latency);
}

/**
@@ -20,6 +20,7 @@

import org.apache.commons.pool2.BasePooledObjectFactory;
import org.apache.commons.pool2.PooledObject;
import org.apache.commons.pool2.Waiter;

/**
* Factory that sources PooledObjects that wrap AtomicIntegers.
@@ -40,40 +41,24 @@
@Override
public void activateObject(final PooledObject<AtomicInteger> p) {
p.getObject().incrementAndGet();
try {
Thread.sleep(activateLatency);
} catch (final InterruptedException ex) {
// ignore
}
Waiter.sleepQuietly(activateLatency);
}

@Override
public AtomicInteger create() {
try {
Thread.sleep(createLatency);
} catch (final InterruptedException ex) {
// ignore
}
Waiter.sleepQuietly(createLatency);
return new AtomicInteger(0);
}

@Override
public void destroyObject(final PooledObject<AtomicInteger> p) {
try {
Thread.sleep(destroyLatency);
} catch (final InterruptedException ex) {
// ignore
}
Waiter.sleepQuietly(destroyLatency);
}

@Override
public void passivateObject(final PooledObject<AtomicInteger> p) {
p.getObject().decrementAndGet();
try {
Thread.sleep(passivateLatency);
} catch (final InterruptedException ex) {
// ignore
}
Waiter.sleepQuietly(passivateLatency);
}

/**
@@ -117,11 +102,7 @@ public void setValidateLatency(final long validateLatency) {

@Override
public boolean validateObject(final PooledObject<AtomicInteger> instance) {
try {
Thread.sleep(validateLatency);
} catch (final InterruptedException ex) {
// ignore
}
Waiter.sleepQuietly(validateLatency);
return instance.getObject().intValue() == 1;
}

@@ -36,6 +36,7 @@
import org.apache.commons.pool2.DestroyMode;
import org.apache.commons.pool2.KeyedPooledObjectFactory;
import org.apache.commons.pool2.PooledObject;
import org.apache.commons.pool2.Waiter;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@@ -126,11 +127,7 @@ public void passivateObject(final Integer key, final PooledObject<PooledTestObje

@Override
public boolean validateObject(final Integer key, final PooledObject<PooledTestObject> obj) {
try {
Thread.sleep(validateLatency);
} catch (final Exception ex) {
// ignore
}
Waiter.sleepQuietly(validateLatency);
return true;
}
}
@@ -39,6 +39,7 @@
import org.apache.commons.pool2.PooledObject;
import org.apache.commons.pool2.PooledObjectFactory;
import org.apache.commons.pool2.TrackedUse;
import org.apache.commons.pool2.Waiter;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@@ -206,11 +207,7 @@ public void passivateObject(final PooledObject<PooledTestObject> obj) {

@Override
public boolean validateObject(final PooledObject<PooledTestObject> obj) {
try {
Thread.sleep(validateLatency);
} catch (final Exception ex) {
// ignore
}
Waiter.sleepQuietly(validateLatency);
return true;
}
}

0 comments on commit 9bdf2ee

Please sign in to comment.