From 6d96a295c4eaa6b4fd9a454ed22287ea74214812 Mon Sep 17 00:00:00 2001 From: Toni Rico Date: Wed, 19 Jul 2023 14:19:22 +0200 Subject: [PATCH] Add tests for dispatcher logic --- .../purchases/common/DispatcherTest.kt | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/purchases/src/test/java/com/revenuecat/purchases/common/DispatcherTest.kt b/purchases/src/test/java/com/revenuecat/purchases/common/DispatcherTest.kt index 48fad4d59..7239eb37e 100644 --- a/purchases/src/test/java/com/revenuecat/purchases/common/DispatcherTest.kt +++ b/purchases/src/test/java/com/revenuecat/purchases/common/DispatcherTest.kt @@ -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 @@ -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 @@ -184,6 +186,40 @@ class DispatcherTest { assertThat(currentThreadExecutorService.executeCalled).isTrue } + @Test + fun `executes with correct delay when not in integration tests`() { + val executorService = mockk(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(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(