-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
as a convenient function for those users that use coroutines. New module pubnub-kotlin-coroutines has been created.
- Loading branch information
1 parent
c30e2f0
commit f350145
Showing
5 changed files
with
78 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
plugins { | ||
alias(libs.plugins.benmanes.versions) | ||
id("pubnub.kotlin-library") | ||
id("pubnub.dokka") | ||
id("pubnub.shared") | ||
} | ||
|
||
dependencies { | ||
api(project(":pubnub-kotlin:pubnub-kotlin-api")) | ||
api(libs.coroutines) | ||
implementation(kotlin("test")) | ||
testImplementation(libs.junit.jupiter.engine) | ||
testImplementation(libs.junit.jupiter) | ||
} |
19 changes: 19 additions & 0 deletions
19
pubnub-kotlin-coroutines/src/main/kotlin/com/pubnub/coroutines/Util.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.pubnub.coroutines | ||
|
||
import com.pubnub.kmp.PNFuture | ||
import kotlinx.coroutines.suspendCancellableCoroutine | ||
import kotlin.coroutines.resume | ||
import kotlin.coroutines.resumeWithException | ||
|
||
suspend fun <T> PNFuture<T>.await(): T { | ||
val t = suspendCancellableCoroutine { cont -> | ||
async { result -> | ||
result.onSuccess { | ||
cont.resume(it) | ||
}.onFailure { | ||
cont.resumeWithException(it) | ||
} | ||
} | ||
} | ||
return t | ||
} |
43 changes: 43 additions & 0 deletions
43
pubnub-kotlin-coroutines/src/test/kotlin/com/pubnub/coroutines/UtilKtTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package com.pubnub.coroutines | ||
|
||
import com.pubnub.api.v2.callbacks.Consumer | ||
import com.pubnub.kmp.PNFuture | ||
import kotlinx.coroutines.runBlocking | ||
import org.junit.jupiter.api.Assertions.assertEquals | ||
import org.junit.jupiter.api.Test | ||
import kotlin.test.assertFailsWith | ||
|
||
class FakePNFuture<T>(private val block: (Consumer<com.pubnub.api.v2.callbacks.Result<T>>) -> Unit) : PNFuture<T> { | ||
override fun async(callback: Consumer<com.pubnub.api.v2.callbacks.Result<T>>) { | ||
block(callback) | ||
} | ||
} | ||
|
||
class UtilKtTest { | ||
@Test | ||
fun `await returns result on success`() = runBlocking { | ||
val expected = "Success Result" | ||
val future = FakePNFuture<String> { callback -> | ||
// Simulate a successful asynchronous result. | ||
callback.accept(com.pubnub.api.v2.callbacks.Result.success(expected)) | ||
} | ||
// When await() is called, it should resume with the expected result. | ||
val result = future.await() | ||
assertEquals(expected, result) | ||
} | ||
|
||
@Test | ||
fun `await throws exception on failure`() = runBlocking { | ||
val exception = Exception("Failure occurred") | ||
val future = FakePNFuture<String> { callback -> | ||
// Simulate an asynchronous failure. | ||
callback.accept(com.pubnub.api.v2.callbacks.Result.failure(exception)) | ||
} | ||
// When await() is called, it should resume with an exception. | ||
val thrown = assertFailsWith<Exception> { | ||
future.await() | ||
} | ||
// Optionally verify the exception message. | ||
assertEquals(exception.message, thrown.message) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters