Skip to content

Commit

Permalink
tests: add unit test coverage for QueueManager
Browse files Browse the repository at this point in the history
  • Loading branch information
wzieba committed Nov 3, 2023
1 parent 9ccb532 commit 8e573f0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 30 deletions.
46 changes: 18 additions & 28 deletions parsely/src/main/java/com/parsely/parselyandroid/QueueManager.kt
Original file line number Diff line number Diff line change
@@ -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<Void?, Void?, Void?>() {

class QueueManager extends AsyncTask<Void, Void, Void> {
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
}
}
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 8e573f0

Please sign in to comment.