From 8e573f05049bc55249f8a328ac7012b76055e5df Mon Sep 17 00:00:00 2001 From: Wojtek Zieba Date: Fri, 3 Nov 2023 16:29:53 +0100 Subject: [PATCH] tests: add unit test coverage for QueueManager --- .../parsely/parselyandroid/QueueManager.kt | 46 ++++++++----------- .../parselyandroid/QueueManagerTest.kt | 4 +- 2 files changed, 20 insertions(+), 30 deletions(-) diff --git a/parsely/src/main/java/com/parsely/parselyandroid/QueueManager.kt b/parsely/src/main/java/com/parsely/parselyandroid/QueueManager.kt index c05d1fe2..6a786cc1 100644 --- a/parsely/src/main/java/com/parsely/parselyandroid/QueueManager.kt +++ b/parsely/src/main/java/com/parsely/parselyandroid/QueueManager.kt @@ -1,38 +1,28 @@ -package com.parsely.parselyandroid; +package com.parsely.parselyandroid -import android.os.AsyncTask; +import android.os.AsyncTask -import androidx.annotation.NonNull; +internal class QueueManager( + private val parselyTracker: ParselyTracker, + private val localStorageRepository: LocalStorageRepository +) : AsyncTask() { -class QueueManager extends AsyncTask { - static final int QUEUE_SIZE_LIMIT = 50; - static final int STORAGE_SIZE_LIMIT = 100; - - @NonNull - private final ParselyTracker parselyTracker; - @NonNull - private final LocalStorageRepository localStorageRepository; - - public QueueManager( - @NonNull ParselyTracker parselyTracker, - @NonNull LocalStorageRepository localStorageRepository - ) { - this.parselyTracker = parselyTracker; - this.localStorageRepository = localStorageRepository; - } - - @Override - protected Void doInBackground(Void... params) { + override fun doInBackground(vararg params: Void?): Void? { // if event queue is too big, push to persisted storage - if (parselyTracker.getInMemoryQueue().size() > QUEUE_SIZE_LIMIT) { - ParselyTracker.PLog("Queue size exceeded, expelling oldest event to persistent memory"); - localStorageRepository.persistQueue(parselyTracker.getInMemoryQueue()); - parselyTracker.getInMemoryQueue().remove(0); + if (parselyTracker.inMemoryQueue.size > QUEUE_SIZE_LIMIT) { + ParselyTracker.PLog("Queue size exceeded, expelling oldest event to persistent memory") + localStorageRepository.persistQueue(parselyTracker.inMemoryQueue) + parselyTracker.inMemoryQueue.removeAt(0) // if persisted storage is too big, expel one if (parselyTracker.storedEventsCount() > STORAGE_SIZE_LIMIT) { - localStorageRepository.expelStoredEvent(); + localStorageRepository.expelStoredEvent() } } - return null; + return null + } + + companion object { + const val QUEUE_SIZE_LIMIT = 50 + const val STORAGE_SIZE_LIMIT = 100 } } diff --git a/parsely/src/test/java/com/parsely/parselyandroid/QueueManagerTest.kt b/parsely/src/test/java/com/parsely/parselyandroid/QueueManagerTest.kt index 309b52e6..2bb00887 100644 --- a/parsely/src/test/java/com/parsely/parselyandroid/QueueManagerTest.kt +++ b/parsely/src/test/java/com/parsely/parselyandroid/QueueManagerTest.kt @@ -1,8 +1,8 @@ package com.parsely.parselyandroid import androidx.test.core.app.ApplicationProvider -import com.parsely.parselyandroid.QueueManager.QUEUE_SIZE_LIMIT -import com.parsely.parselyandroid.QueueManager.STORAGE_SIZE_LIMIT +import com.parsely.parselyandroid.QueueManager.Companion.QUEUE_SIZE_LIMIT +import com.parsely.parselyandroid.QueueManager.Companion.STORAGE_SIZE_LIMIT import org.assertj.core.api.Assertions.assertThat import org.junit.Before import org.junit.Test