Skip to content

Commit

Permalink
Add tests for dispatcher logic
Browse files Browse the repository at this point in the history
  • Loading branch information
tonidero committed Jul 19, 2023
1 parent bf68f8d commit 6d96a29
Showing 1 changed file with 36 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import com.revenuecat.purchases.common.networking.HTTPResult
import com.revenuecat.purchases.common.verification.SignatureVerificationException
import io.mockk.every
import io.mockk.mockk
import io.mockk.spyk
import io.mockk.verify
import org.assertj.core.api.Assertions.assertThat
import org.assertj.core.api.Assertions.fail
Expand All @@ -21,6 +22,7 @@ import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.annotation.Config
import java.util.concurrent.ExecutorService
import java.util.concurrent.ScheduledExecutorService
import java.util.concurrent.SynchronousQueue
import java.util.concurrent.ThreadPoolExecutor
import java.util.concurrent.TimeUnit
Expand Down Expand Up @@ -184,6 +186,40 @@ class DispatcherTest {
assertThat(currentThreadExecutorService.executeCalled).isTrue
}

@Test
fun `executes with correct delay when not in integration tests`() {
val executorService = mockk<ScheduledExecutorService>(relaxed = true)
val dispatcher = Dispatcher(executorService, runningIntegrationTests = false)
dispatcher.enqueue({ }, delay = Delay.LONG)
verify(exactly = 1) {
executorService.schedule(
any(),
withArg { delay ->
assertThat(delay).isGreaterThanOrEqualTo(5000L)
assertThat(delay).isLessThan(10000L)
},
TimeUnit.MILLISECONDS,
)
}
}

@Test
fun `executes with correct delay when in integration tests`() {
val executorService = mockk<ScheduledExecutorService>(relaxed = true)
val dispatcher = Dispatcher(executorService, runningIntegrationTests = true)
dispatcher.enqueue({ }, delay = Delay.LONG)
verify(exactly = 1) {
executorService.schedule(
any(),
withArg { delay ->
assertThat(delay).isGreaterThanOrEqualTo(50L)
assertThat(delay).isLessThan(100L)
},
TimeUnit.MILLISECONDS,
)
}
}

class CurrentThreadExecutorService(
private val callerRunsPolicy: CallerRunsPolicy = CallerRunsPolicy()
): ThreadPoolExecutor(
Expand Down

0 comments on commit 6d96a29

Please sign in to comment.