Skip to content

Commit

Permalink
Added function PNFuture.await()
Browse files Browse the repository at this point in the history
as a convenient function for those users that use coroutines.

New module pubnub-kotlin-coroutines has been created.
  • Loading branch information
marcin-cebo committed Mar 6, 2025
1 parent c30e2f0 commit f350145
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 0 deletions.
1 change: 1 addition & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ cbor = { module = "co.nstant.in:cbor", version = "0.9" }
jetbrains-annotations = { module = "org.jetbrains:annotations", version = "24.1.0" }
kotlinx-atomicfu = { module = "org.jetbrains.kotlinx:atomicfu", version = "0.24.0" }
kotlinx-datetime = { module = "org.jetbrains.kotlinx:kotlinx-datetime", version.ref = "kotlinx_datetime"}
coroutines = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-core", version.ref = "kotlinx_coroutines"}

# tests
wiremock = { module = "com.github.tomakehurst:wiremock", version = "2.27.2" }
Expand Down
14 changes: 14 additions & 0 deletions pubnub-kotlin-coroutines/build.gradle.kts
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)
}
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
}
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)
}
}
1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@ include("examples:kotlin-app")
include("examples:java-app")
includeBuild("build-logic/ktlint-custom-rules")
includeBuild("migration_utils")
include("pubnub-kotlin-coroutines")

0 comments on commit f350145

Please sign in to comment.