sushma nayak
03/25/2021, 6:55 PMsuspend fun <T : Any> request(target: APITarget, responseDeserializer: DeserializationStrategy<T>, response: (NetworkResult) -> Unit): Cancelable
How do I test this?
Wait till the response lamda completes and cross verify the result.
Putting the test inside runblocking doesn’t work.Benoît
03/25/2021, 7:16 PMclass CoroutineLock() {
private var channel by AtomicReferenceDelegate<ConflatedBroadcastChannel<Unit>?>(null)
private val mutex = Mutex()
val isLocked: Boolean get() = channel != null
suspend fun lock() {
mutex.withLock {
val channel = channel ?: ConflatedBroadcastChannel()
this.channel = channel
channel.openSubscription().receive()
}
}
fun unlock() {
channel?.offer(Unit)
channel = null
}
}
With the AtomicReferenceDelegate being a simple ReadWriteProperty around AtomicReference to avoid mutation crashes on native.
It works like the JVM wait()
and notify()
functions on Object.
You can use this to synchronise your code the way you need toaraqnid
03/25/2021, 7:39 PMCompletableDeferred<NetworkResult>
was more appropriate:
val result = CompletableDeferred<NetworkResult>()
launch { request(target, deser) { result.complete(it) } }
result.await()
Benoît
03/25/2021, 7:58 PMsushma nayak
03/25/2021, 7:58 PMaraqnid
03/25/2021, 8:42 PMsupendCancellableCoroutine
then execution is suspended, and you’re given a continuation to hook up to the callback, or call cancel()
oncont.invokeOnCancellation { }
to propagate a cancellation of the coroutine context to the CancelablesuspendCancellableCoroutine
are a good place to lookBenoît
03/25/2021, 9:46 PMsushma nayak
03/26/2021, 7:48 AMsuspendCancellableCoroutine
is \m/ It seems to be working. Thank you @Benoît and @araqnid 😄
People like you in the community make newbies like me survive in the platform. Could otherwise drive us away with frustration.Benoît
03/26/2021, 9:35 AM