From 5d6fc25f37308f6fcf67c9854be389f7a056abe7 Mon Sep 17 00:00:00 2001 From: Daniel Frett Date: Wed, 21 Dec 2022 16:06:59 -0500 Subject: [PATCH] remove v3.14.0 deprecated snowplow EventSynchronizer --- .../snowplow/events/EventSynchronizer.kt | 33 -------- .../snowplow/events/EventSynchronizerTest.kt | 78 ------------------- 2 files changed, 111 deletions(-) delete mode 100644 gto-support-snowplow/src/main/kotlin/org/ccci/gto/android/common/snowplow/events/EventSynchronizer.kt delete mode 100644 gto-support-snowplow/src/test/kotlin/org/ccci/gto/android/common/snowplow/events/EventSynchronizerTest.kt diff --git a/gto-support-snowplow/src/main/kotlin/org/ccci/gto/android/common/snowplow/events/EventSynchronizer.kt b/gto-support-snowplow/src/main/kotlin/org/ccci/gto/android/common/snowplow/events/EventSynchronizer.kt deleted file mode 100644 index fa1be6e8e..000000000 --- a/gto-support-snowplow/src/main/kotlin/org/ccci/gto/android/common/snowplow/events/EventSynchronizer.kt +++ /dev/null @@ -1,33 +0,0 @@ -package org.ccci.gto.android.common.snowplow.events - -import androidx.annotation.VisibleForTesting -import com.snowplowanalytics.snowplow.event.Event -import java.util.concurrent.Semaphore -import java.util.concurrent.TimeUnit -import java.util.concurrent.atomic.AtomicReference - -@Deprecated("Since v3.14.0, We no longer use snowplow for any analytics") -internal object EventSynchronizer { - @VisibleForTesting - internal val semaphore = Semaphore(1, true) - private val currentEvent = AtomicReference() - - @VisibleForTesting - internal var lockTimeout = 2000L - - fun lockFor(event: Event) { - while (true) { - val curr = currentEvent.get() - if (semaphore.tryAcquire(lockTimeout, TimeUnit.MILLISECONDS)) { - if (currentEvent.compareAndSet(null, event)) break - semaphore.release() - } else if (curr != null && currentEvent.compareAndSet(curr, event)) { - break - } - } - } - - fun unlockFor(event: Event) { - if (currentEvent.compareAndSet(event, null)) semaphore.release() - } -} diff --git a/gto-support-snowplow/src/test/kotlin/org/ccci/gto/android/common/snowplow/events/EventSynchronizerTest.kt b/gto-support-snowplow/src/test/kotlin/org/ccci/gto/android/common/snowplow/events/EventSynchronizerTest.kt deleted file mode 100644 index 5910990ad..000000000 --- a/gto-support-snowplow/src/test/kotlin/org/ccci/gto/android/common/snowplow/events/EventSynchronizerTest.kt +++ /dev/null @@ -1,78 +0,0 @@ -package org.ccci.gto.android.common.snowplow.events - -import com.snowplowanalytics.snowplow.event.Event -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.coroutineScope -import kotlinx.coroutines.delay -import kotlinx.coroutines.launch -import kotlinx.coroutines.runBlocking -import kotlinx.coroutines.sync.Mutex -import org.junit.Assert.assertEquals -import org.junit.Assert.assertTrue -import org.junit.Test -import org.mockito.kotlin.mock - -private const val THREADS = 50 -private const val ITERATIONS = 50 - -private const val TIMEOUT_TEST_LOCK_TIMEOUT = 1L -private const val TIMEOUT_TEST_DELAY = TIMEOUT_TEST_LOCK_TIMEOUT * 3 - -class EventSynchronizerTest { - @Test - fun verifyMutualExclusionOfLock() = runBlocking { - var count = 0 - val mutex = Mutex() - coroutineScope { - repeat(THREADS) { - launch(Dispatchers.IO) { - repeat(ITERATIONS) { - val event = mock() - EventSynchronizer.lockFor(event) - assertEquals(0, EventSynchronizer.semaphore.availablePermits()) - assertTrue("Mutual Exclusion not maintained!", mutex.tryLock(this)) - mutex.unlock(this) - count++ - EventSynchronizer.unlockFor(event) - assertTrue(EventSynchronizer.semaphore.availablePermits() <= 1) - } - } - } - } - - assertEquals(1, EventSynchronizer.semaphore.availablePermits()) - assertEquals(THREADS * ITERATIONS, count) - } - - @Test(timeout = THREADS * ITERATIONS * TIMEOUT_TEST_DELAY) - fun verifyLockTimeout() = runBlocking { - var count = 0 - EventSynchronizer.lockTimeout = TIMEOUT_TEST_LOCK_TIMEOUT - - coroutineScope { - repeat(THREADS) { - launch(Dispatchers.IO) { - repeat(ITERATIONS) { - val event = mock() - EventSynchronizer.lockFor(event) - assertEquals( - "EventSynchronizer is not currently locked", - 0, - EventSynchronizer.semaphore.availablePermits() - ) - count++ - delay(TIMEOUT_TEST_DELAY) - EventSynchronizer.unlockFor(event) - assertTrue( - "There are too many permits in the semaphore", - EventSynchronizer.semaphore.availablePermits() <= 1 - ) - } - } - } - } - - assertEquals(1, EventSynchronizer.semaphore.availablePermits()) - assertEquals("Not all iterations were processed", THREADS * ITERATIONS, count) - } -}