Skip to content

Commit

Permalink
Merge pull request #534 from kennethshackleton/replace-scheduleAtFixe…
Browse files Browse the repository at this point in the history
…dRate

Replace scheduleAtFixedRate when evicting with scheduleWithFixedDelay from the connection pools.
  • Loading branch information
kennethshackleton authored Sep 1, 2024
2 parents edb1d95 + be4a696 commit 2204e07
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ class CommonObjectPool<K : Any, T : IPooledObject<K>>(
if (future?.isCancelled == false || configuration.evictionIntervalMillis < 0L || isClosed.get()) {
return
}
future = executor.scheduleAtFixedRate(
future = executor.scheduleWithFixedDelay(
::evict,
configuration.evictionDelayMillis,
configuration.evictionIntervalMillis,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ class SingleObjectPool<K : Any, T : IPooledObject<K>>(
if (evictionIntervalMillis < 0L || isClosed) {
return
}
future = executor.scheduleAtFixedRate(
future = executor.scheduleWithFixedDelay(
::evict,
evictionDelayMillis,
evictionIntervalMillis,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -360,17 +360,16 @@ internal class CommonObjectPoolTest {

@Test
fun interleavedBorrowSchedulesEvictionIfCancelled() {
@Suppress("JoinDeclarationAndAssignment")
lateinit var pool: CommonObjectPool<String, PooledObject>
val executor = object : ScheduledExecutorService by this@CommonObjectPoolTest.executor {
var count = 0

override fun scheduleAtFixedRate(
override fun scheduleWithFixedDelay(
command: Runnable,
initialDelay: Long,
period: Long,
delay: Long,
unit: TimeUnit
) = this@CommonObjectPoolTest.executor.scheduleAtFixedRate(command, initialDelay, period, unit).also {
) = this@CommonObjectPoolTest.executor.scheduleWithFixedDelay(command, initialDelay, delay, unit).also {
if (count++ == 0) {
it.cancel(false)
}
Expand Down Expand Up @@ -459,12 +458,12 @@ internal class CommonObjectPoolTest {
@Test
fun evictionFailsIfCancelled() {
val executor = object : ScheduledExecutorService by this@CommonObjectPoolTest.executor {
override fun scheduleAtFixedRate(
override fun scheduleWithFixedDelay(
command: Runnable,
initialDelay: Long,
period: Long,
delay: Long,
unit: TimeUnit
) = this@CommonObjectPoolTest.executor.scheduleAtFixedRate(command, initialDelay, period, unit).apply {
) = this@CommonObjectPoolTest.executor.scheduleWithFixedDelay(command, initialDelay, delay, unit).apply {
cancel(false)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -349,12 +349,12 @@ internal class SingleObjectPoolTest {
@Test
fun evictionFailsIfCancelled() {
val executor = object : ScheduledExecutorService by this@SingleObjectPoolTest.executor {
override fun scheduleAtFixedRate(
override fun scheduleWithFixedDelay(
command: Runnable,
initialDelay: Long,
period: Long,
delay: Long,
unit: TimeUnit
) = this@SingleObjectPoolTest.executor.scheduleAtFixedRate(command, initialDelay, period, unit).apply {
) = this@SingleObjectPoolTest.executor.scheduleAtFixedRate(command, initialDelay, delay, unit).apply {
cancel(false)
}
}
Expand Down Expand Up @@ -382,10 +382,8 @@ internal class SingleObjectPoolTest {

@Test
fun borrowAsClosingDoesNotScheduleEviction() {
@Suppress("JoinDeclarationAndAssignment")
lateinit var pool: SingleObjectPool<String, PooledObject>
val executor = mock<ScheduledExecutorService>()
pool = SingleObjectPool(object : IObjectFactory<PooledObject> {
val pool = SingleObjectPool(object : IObjectFactory<PooledObject> {
override fun close() = Unit

override fun destroyObject(obj: PooledObject) = Unit
Expand Down

0 comments on commit 2204e07

Please sign in to comment.