Skip to content

Commit

Permalink
Cover DefaultLoadingHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
matt-ramotar committed Jul 14, 2024
1 parent 2041c5d commit d13997d
Show file tree
Hide file tree
Showing 14 changed files with 361 additions and 34 deletions.
2 changes: 2 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ plugins {
alias(libs.plugins.kotlinx.benchmark) apply false
alias(libs.plugins.allopen) apply false
alias(libs.plugins.kotlin.plugin.parcelize) apply false
id("com.google.devtools.ksp") version "2.0.0-1.0.22" apply false
id("dev.mokkery") version "2.1.1" apply false
}

buildscript {
Expand Down
9 changes: 6 additions & 3 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,10 @@ ktor-serialization-json = { module = "io.ktor:ktor-serialization-kotlinx-json",
ktor-serialization-xml = { module = "io.ktor:ktor-serialization-kotlinx-xml", version.ref = "ktor" }
circuit-foundation = { module = "com.slack.circuit:circuit-foundation", version.ref = "circuit" }
swipe = { module = "me.saket.swipe:swipe", version = "1.3.0" }
mockk = {module = "io.mockk:mockk", version.ref = "mockk"}
jetbrains-compose-runtime = {module = "org.jetbrains.compose.runtime:runtime", version.ref="jetbrainsCompose"}

mockk = { module = "io.mockk:mockk", version.ref = "mockk" }
jetbrains-compose-runtime = { module = "org.jetbrains.compose.runtime:runtime", version.ref = "jetbrainsCompose" }
allopen = { module = "org.jetbrains.kotlin:kotlin-allopen", version.ref = "kotlin" }
mokkery = { module = "dev.mokkery:mokkery-gradle", version = "2.1.1" }
[plugins]
android-application = { id = "com.android.application", version.ref = "agp" }
android-library = { id = "com.android.library", version.ref = "agp" }
Expand All @@ -123,3 +124,5 @@ sqldelight = { id = "app.cash.sqldelight", version.ref = "sqldelight" }
kotlinx-benchmark = { id = "org.jetbrains.kotlinx.benchmark", version.ref = "benchmark" }
allopen = { id = "org.jetbrains.kotlin.plugin.allopen", version.ref = "kotlin" }

[bundles]
testing = ["kotlin-test-common", "kotlin-test-annotations-common", "turbine", "kotlinx-coroutines-test"]
15 changes: 7 additions & 8 deletions paging-runtime/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import dev.mokkery.MockMode

plugins {
id("storex.android.library")
id("storex.multiplatform")
Expand All @@ -21,22 +23,19 @@ kotlin {
}
}

commonTest {
val commonTest by getting {
dependencies {
implementation(libs.kotlin.test)
implementation(libs.kotlin.test.common)
implementation(libs.kotlin.test.annotations.common)
implementation(libs.turbine)
implementation(libs.kotlinx.coroutines.test)
implementation(libs.bundles.testing)
implementation(libs.kotlinx.datetime)
}
}
}
}

android {
namespace = "org.mobilenativefoundation.paging.core"
namespace = "org.mobilenativefoundation.paging.runtime"

sourceSets["main"].manifest.srcFile("src/androidMain/AndroidManifest.xml")

compileSdk = libs.versions.android.compileSdk.get().toInt()
}
}
Original file line number Diff line number Diff line change
@@ -1,29 +1,43 @@
package org.mobilenativefoundation.storex.paging.runtime.internal.logger.impl

import co.touchlab.kermit.Logger
import org.mobilenativefoundation.storex.paging.runtime.Identifier
import org.mobilenativefoundation.storex.paging.runtime.PagingConfig
import org.mobilenativefoundation.storex.paging.runtime.Severity
import org.mobilenativefoundation.storex.paging.runtime.internal.logger.api.PagingLogger

class RealPagingLogger<Id : Identifier<*>, K : Any>(
private val pagingConfig: PagingConfig<Id, K>
class RealPagingLogger(
private val severity: Severity
) : PagingLogger {
override fun debug(message: String) {
if (pagingConfig.logging.ordinal >= Severity.Debug.ordinal) {
if (severity.ordinal >= Severity.Debug.ordinal) {
Logger.d("storex/paging") { message }
}
}

override fun error(message: String, error: Throwable) {
if (pagingConfig.logging.ordinal >= Severity.Error.ordinal) {
if (severity.ordinal >= Severity.Error.ordinal) {
Logger.e("storex/paging", error) { message }
}
}

override fun verbose(message: String) {
if (pagingConfig.logging.ordinal >= Severity.Verbose.ordinal) {
if (severity.ordinal >= Severity.Verbose.ordinal) {
Logger.v("storex/paging") { message }
}
}
}

class TestPagingLogger : PagingLogger {
override fun verbose(message: String) {
println(message)
}

override fun debug(message: String) {
println(message)
}

override fun error(message: String, error: Throwable) {
println(message)
println(error)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,13 @@ internal interface LoadParamsQueue<K : Comparable<K>> {
* Removes the first matching element in the queue.
* @return The first matching element in the queue.
*/
suspend fun removeFirst(predicate: (element: Element<K>) -> Boolean): Element<K>
suspend fun removeFirst(predicate: (element: Element<K>) -> Boolean)

/**
* Removes the last matching element in the queue.
* @return The last matching element in the queue.
*/
suspend fun removeLast(predicate: (element: Element<K>) -> Boolean): Element<K>
suspend fun removeLast(predicate: (element: Element<K>) -> Boolean)

/**
* Returns the last element in the queue without removing it.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,9 @@ internal class DefaultLoadingHandler<Id : Identifier<Id>, K : Comparable<K>, V :
}

private suspend fun enqueueNext(key: K, direction: LoadDirection) {
// Skip cache because these params come from a network response
val action = Action.Enqueue(key, direction, LoadStrategy.SkipCache, jump = false)
logger.debug("Enqueuing next action: $action")
when (direction) {
LoadDirection.Append -> queueManager.enqueueAppend(action)
LoadDirection.Prepend -> queueManager.enqueuePrepend(action)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.mobilenativefoundation.storex.paging.runtime.internal.pager.impl

import kotlinx.atomicfu.AtomicInt
import kotlinx.atomicfu.atomic
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
Expand Down Expand Up @@ -46,14 +45,18 @@ internal class RealLoadParamsQueue<K : Comparable<K>> : LoadParamsQueue<K> {
queue.removeFirst()
}

override suspend fun removeLast(predicate: (element: LoadParamsQueue.Element<K>) -> Boolean): LoadParamsQueue.Element<K> = mutex.withLock{
val index = queue.indexOfLast(predicate)
return queue.removeAt(index)
override suspend fun removeLast(predicate: (element: LoadParamsQueue.Element<K>) -> Boolean) {
mutex.withLock {
val index = queue.indexOfLast(predicate)
queue.removeAt(index)
}
}

override suspend fun removeFirst(predicate: (element: LoadParamsQueue.Element<K>) -> Boolean): LoadParamsQueue.Element<K> {
val index = queue.indexOfFirst(predicate)
return queue.removeAt(index)
override suspend fun removeFirst(predicate: (element: LoadParamsQueue.Element<K>) -> Boolean) {
mutex.withLock {
val index = queue.indexOfFirst(predicate)
queue.removeAt(index)
}
}

override suspend fun last(): LoadParamsQueue.Element<K> = mutex.withLock {
Expand Down Expand Up @@ -108,6 +111,7 @@ internal class RealLoadParamsQueue<K : Comparable<K>> : LoadParamsQueue<K> {
override suspend fun isNotEmpty(): Boolean = mutex.withLock {
size.value > 0
}

override suspend fun size(): Int = mutex.withLock {
size.value
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class PagingScopeBuilder<Id : Identifier<Id>, K : Comparable<K>, V : Identifiabl
private val pagingConfig: PagingConfig<Id, K>
) : PagingScope.Builder<Id, K, V> {

private val logger = RealPagingLogger(pagingConfig)
private val logger = RealPagingLogger(pagingConfig.logging)
private val actionsFlow = MutableSharedFlow<Action<K>>(replay = 20)

private var initialState: PagingState<Id> = PagingState.initial()
Expand Down Expand Up @@ -143,8 +143,8 @@ class PagingScopeBuilder<Id : Identifier<Id>, K : Comparable<K>, V : Identifiabl
val dispatcher = RealDispatcher(actionsFlow)
val recompositionMode = RecompositionMode.ContextClock

val pagingStateManager = RealPagingStateManager(initialState)
val queueManager = RealQueueManager<K>()
val pagingStateManager = RealPagingStateManager(initialState, logger)
val queueManager = RealQueueManager<K>(logger)
val operationApplier = ConcurrentOperationApplier(operationManager)
val loadingHandler = createLoadingHandler(
store = store,
Expand Down
Loading

0 comments on commit d13997d

Please sign in to comment.