Skip to content

Commit

Permalink
fun
Browse files Browse the repository at this point in the history
  • Loading branch information
mareklangiewicz committed Jun 6, 2024
1 parent 965270c commit 81f1f43
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 58 deletions.
2 changes: 1 addition & 1 deletion settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pluginManagement {
}

plugins {
id("pl.mareklangiewicz.deps.settings") version "0.3.16" // https://plugins.gradle.org/search?term=mareklangiewicz
id("pl.mareklangiewicz.deps.settings") version "0.3.17" // https://plugins.gradle.org/search?term=mareklangiewicz
id("com.gradle.develocity") version "3.17.4" // https://docs.gradle.com/develocity/gradle-plugin/
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import kotlin.Result.Companion.success
import kotlin.coroutines.resume
import kotlin.coroutines.resumeWithException

internal infix fun <T> T.chkEq(expected: T) = check(this == expected) { "$this != $expected" }

@OptIn(DelicateCoroutinesApi::class)
class GetUserDetailsFastTest {

Expand All @@ -33,58 +35,58 @@ class GetUserDetailsFastTest {
}
}

"is active" o { deferred.isActive eq true }
"getting cached details started" o { getCachedUserDetails.invocations eq listOf(7) }
"fetching details started too" o { fetchUserDetails.invocations eq listOf(7) }
"is active" o { deferred.isActive chkEq true }
"getting cached details started" o { getCachedUserDetails.invocations chkEq listOf(7) }
"fetching details started too" o { fetchUserDetails.invocations chkEq listOf(7) }

"On cached details" o {
getCachedUserDetails.resume("cached details")

"is still active" o { deferred.isActive eq true }
"is still active" o { deferred.isActive chkEq true }

"On fetching cancelled" o { // see GetUserDetailsFastXTest for autoCancel and cancellation checking
fetchUserDetails.resumeWithException(CancellationException())

"return cached details" o { deferred.getCompleted() eq success("cached details") }
"return cached details" o { deferred.getCompleted() chkEq success("cached details") }
}
}

"On no cached details" o {
getCachedUserDetails.resume(null)

"is still active" o { deferred.isActive eq true }
"is still active" o { deferred.isActive chkEq true }

"On fetched details " o {
fetchUserDetails.resume("details from network")

"put fetched details to cache" o {
putCachedUserDetails.invocations eq listOf(7 to "details from network")
putCachedUserDetails.invocations chkEq listOf(7 to "details from network")
}
"On putting to cache finish" o { // see GetUserDetailsFastXTest for mocking with autoResume
putCachedUserDetails.resume(Unit)

"return fetched details" o { deferred.getCompleted() eq success("details from network") }
"return fetched details" o { deferred.getCompleted() chkEq success("details from network") }
}
}

"On fetching network error" o {
fetchUserDetails.resumeWithException(RuntimeException("network error"))

"do not put anything to cache" o { putCachedUserDetails.invocations.size eq 0 }
"is completed" o { deferred.isCompleted eq true }
"return failure" o { deferred.getCompleted().exceptionOrNull()?.message eq "network error" }
"do not put anything to cache" o { putCachedUserDetails.invocations.size chkEq 0 }
"is completed" o { deferred.isCompleted chkEq true }
"return failure" o { deferred.getCompleted().exceptionOrNull()?.message chkEq "network error" }
}
}

"On cached details error" o {
getCachedUserDetails.resumeWithException(RuntimeException("cache error"))

"is still active" o { deferred.isActive eq true }
"is still active" o { deferred.isActive chkEq true }

"On fetching cancelled" o { // see GetUserDetailsFastXTest for autoCancel and cancellation checking
fetchUserDetails.resumeWithException(CancellationException())

"return cache error" o { deferred.getCompleted().exceptionOrNull()?.message eq "cache error" }
"return cache error" o { deferred.getCompleted().exceptionOrNull()?.message chkEq "cache error" }
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,53 +34,53 @@ class GetUserDetailsFastXTest {
}
}

"is active" o { deferred.isActive eq true }
"getting cached details started" o { getCachedUserDetails.invocations eq listOf(7) }
"fetching details started too" o { fetchUserDetails.invocations eq listOf(7) }
"is active" o { deferred.isActive chkEq true }
"getting cached details started" o { getCachedUserDetails.invocations chkEq listOf(7) }
"fetching details started too" o { fetchUserDetails.invocations chkEq listOf(7) }

"On cached details" o {
getCachedUserDetails.resume("cached details")

"fetching is cancelled" o { fetchUserDetails.cancellations eq 1 }
"return cached details" o { deferred.getCompleted() eq success("cached details") }
"fetching is cancelled" o { fetchUserDetails.cancellations chkEq 1 }
"return cached details" o { deferred.getCompleted() chkEq success("cached details") }
}

"On no cached details" o {
getCachedUserDetails.resume(null)

"is still active" o { deferred.isActive eq true }
"is still active" o { deferred.isActive chkEq true }

"On fetched details " o {
fetchUserDetails.resume("details from network")

"put fetched details to cache" o {
putCachedUserDetails.invocations eq listOf(7 to "details from network")
putCachedUserDetails.invocations chkEq listOf(7 to "details from network")
// putCachedUserDetails resumes immediately thanks to autoResume
}
"return fetched details" o { deferred.getCompleted() eq success("details from network") }
"return fetched details" o { deferred.getCompleted() chkEq success("details from network") }
}

"On fetching network error" o {
fetchUserDetails.resumeWithException(RuntimeException("network error"))

"do not put anything to cache" o { putCachedUserDetails.invocations.size eq 0 }
"is completed" o { deferred.isCompleted eq true }
"return failure" o { deferred.getCompleted().exceptionOrNull()?.message eq "network error" }
"do not put anything to cache" o { putCachedUserDetails.invocations.size chkEq 0 }
"is completed" o { deferred.isCompleted chkEq true }
"return failure" o { deferred.getCompleted().exceptionOrNull()?.message chkEq "network error" }
}

}

"On cancel whole thing from outside" o {
deferred.cancel()

"getting cached details is cancelled" o { getCachedUserDetails.cancellations eq 1 }
"getting cached details is cancelled" o { getCachedUserDetails.cancellations chkEq 1 }
}

"On cached details error" o {
getCachedUserDetails.resumeWithException(RuntimeException("cache error"))

"fetching is cancelled" o { fetchUserDetails.cancellations eq 1 }
"return cache error" o { deferred.getCompleted().exceptionOrNull()?.message eq "cache error" }
"fetching is cancelled" o { fetchUserDetails.cancellations chkEq 1 }
"return cache error" o { deferred.getCompleted().exceptionOrNull()?.message chkEq "cache error" }
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,54 +39,54 @@ class WebSearchTest {

assert(job.isActive)

rendered.size eq 1
rendered.last() eq emptyList() // renders empty results at start
rendered.size chkEq 1
rendered.last() chkEq emptyList() // renders empty results at start

apiCall.invocations.size eq 0
apiCall.invocations.size chkEq 0

inputTextChangeS.onNext("") // too short text - no api call

apiCall.invocations.size eq 0 // no api call
apiCall.invocations.size chkEq 0 // no api call

inputTextChangeS.onNext("aaa")

apiCall.invocations.size eq 1
apiCall.invocations[0] eq "aaa"
apiCall.invocations.size chkEq 1
apiCall.invocations[0] chkEq "aaa"

val results1 = listOf("aaa bla", "aaa ble")
apiCall.resume(results1)

rendered.size eq 2
rendered.last() eq results1
rendered.size chkEq 2
rendered.last() chkEq results1

inputTextChangeS.onNext("xy") // too short text again

apiCall.invocations.size eq 1 // no new api call
apiCall.invocations.size chkEq 1 // no new api call

inputTextChangeS.onNext("abcde")

apiCall.invocations.size eq 2
apiCall.invocations.last() eq "abcde"
apiCall.invocations.size chkEq 2
apiCall.invocations.last() chkEq "abcde"

rendered.size eq 2 // do not render anything new yet
rendered.size chkEq 2 // do not render anything new yet

apiCall.resumeWithException(RuntimeException("terrible network failure"))

rendered.size eq 3
rendered.last() eq listOf("terrible network failure")
rendered.size chkEq 3
rendered.last() chkEq listOf("terrible network failure")

inputTextChangeS.onNext("xyz")

apiCall.invocations.size eq 3
apiCall.invocations.last() eq "xyz"
apiCall.invocations.size chkEq 3
apiCall.invocations.last() chkEq "xyz"

rendered.size eq 3 // do not render anything new yet
rendered.size chkEq 3 // do not render anything new yet

val results2 = listOf("xyz bla bla bla", "xyz ble ble ble")
apiCall.resume(results2)

rendered.size eq 4
rendered.last() eq results2
rendered.size chkEq 4
rendered.last() chkEq results2

assert(job.isActive)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,31 +26,31 @@ class With1STimeoutXTest {
with1STimeout(delay1s::invoke, block::invoke)
}

"job is active" o { job.isActive eq true }
"block has started" o { block.invocations eq 1 }
"delay1s has started" o { delay1s.invocations eq 1 }
"job is active" o { job.isActive chkEq true }
"block has started" o { block.invocations chkEq 1 }
"delay1s has started" o { delay1s.invocations chkEq 1 }

"On block completion" o {
block.resume(Unit)

"delay1s is cancelled" o { delay1s.cancellations eq 1 }
"job is not cancelled" o { job.isCancelled eq false }
"job is completed" o { job.isCompleted eq true }
"delay1s is cancelled" o { delay1s.cancellations chkEq 1 }
"job is not cancelled" o { job.isCancelled chkEq false }
"job is completed" o { job.isCompleted chkEq true }
}

"On block exception" o {
block.resumeWithException(RuntimeException())

"delay1s is cancelled" o { delay1s.cancellations eq 1 }
"job is cancelled" o { job.isCancelled eq true }
"delay1s is cancelled" o { delay1s.cancellations chkEq 1 }
"job is cancelled" o { job.isCancelled chkEq true }
}

"On 1s timeout" o {
delay1s.resume(Unit)

"block is cancelled" o { block.cancellations eq 1 }
"job is not cancelled" o { job.isCancelled eq false }
"job is completed" o { job.isCompleted eq true }
"block is cancelled" o { block.cancellations chkEq 1 }
"job is not cancelled" o { job.isCancelled chkEq false }
"job is completed" o { job.isCompleted chkEq true }
}
}
}
Expand Down

0 comments on commit 81f1f43

Please sign in to comment.