Skip to content

Commit

Permalink
changes after review
Browse files Browse the repository at this point in the history
  • Loading branch information
marcin-cebo committed Mar 6, 2025
1 parent f350145 commit 87819fd
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 18 deletions.
4 changes: 2 additions & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +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"}
kotlinx-coroutines-core = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-core", version.ref = "kotlinx_coroutines"}

# tests
wiremock = { module = "com.github.tomakehurst:wiremock", version = "2.27.2" }
Expand All @@ -45,7 +45,7 @@ mockk = { module = "io.mockk:mockk", version = "1.11.0" }
owner = { module = "org.aeonbits.owner:owner", version = "1.0.12" }
mockito = { module = "org.mockito:mockito-core", version = "4.8.1" }
hamcrest = { module = "org.hamcrest:hamcrest-all", version = "1.3" }
coroutines-test = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-test", version.ref = "kotlinx_coroutines"}
kotlinx-coroutines-test = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-test", version.ref = "kotlinx_coroutines"}

# plugins for included build
kotlin-gradlePlugin = { group = "org.jetbrains.kotlin", name = "kotlin-gradle-plugin", version.ref = "kotlin" }
Expand Down
2 changes: 1 addition & 1 deletion pubnub-kotlin/pubnub-kotlin-api/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ kotlin {
dependencies {
implementation(project(":pubnub-kotlin:pubnub-kotlin-test"))
implementation(kotlin("test"))
implementation(libs.coroutines.test)
implementation(libs.kotlinx.coroutines.test)
}
}

Expand Down
2 changes: 1 addition & 1 deletion pubnub-kotlin/pubnub-kotlin-core-api/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ kotlin {
dependencies {
implementation(project(":pubnub-kotlin:pubnub-kotlin-test"))
implementation(kotlin("test"))
implementation(libs.coroutines.test)
implementation(libs.kotlinx.coroutines.test)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ plugins {

dependencies {
api(project(":pubnub-kotlin:pubnub-kotlin-api"))
api(libs.coroutines)
api(libs.kotlinx.coroutines.core)
implementation(kotlin("test"))
testImplementation(libs.kotlinx.coroutines.test)
testImplementation(libs.junit.jupiter.engine)
testImplementation(libs.junit.jupiter)
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import kotlinx.coroutines.suspendCancellableCoroutine
import kotlin.coroutines.resume
import kotlin.coroutines.resumeWithException

suspend fun <T> PNFuture<T>.await(): T {
val t = suspendCancellableCoroutine { cont ->
suspend fun <T> PNFuture<T>.await(): T =
suspendCancellableCoroutine { cont ->
async { result ->
result.onSuccess {
cont.resume(it)
Expand All @@ -15,5 +15,3 @@ suspend fun <T> PNFuture<T>.await(): T {
}
}
}
return t
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package com.pubnub.coroutines

import com.pubnub.api.v2.callbacks.Consumer
import com.pubnub.kmp.PNFuture
import kotlinx.coroutines.runBlocking
import com.pubnub.kmp.asFuture
import com.pubnub.kmp.then
import kotlinx.coroutines.test.runTest
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
import kotlin.test.assertFailsWith
Expand All @@ -15,24 +17,26 @@ class FakePNFuture<T>(private val block: (Consumer<com.pubnub.api.v2.callbacks.R

class UtilKtTest {
@Test
fun `await returns result on success`() = runBlocking {
fun `await returns result on success`() = runTest {
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 {
fun `await throws exception on failure`() = runTest {
val exception = Exception("Failure occurred")
val future = FakePNFuture<String> { callback ->
// Simulate an asynchronous failure.
callback.accept(com.pubnub.api.v2.callbacks.Result.failure(exception))
}
// val future = FakePNFuture<String> { callback ->
// // Simulate an asynchronous failure.
// callback.accept(com.pubnub.api.v2.callbacks.Result.failure(exception))
// }
val future = Unit.asFuture().then { error("exception!") }
// When await() is called, it should resume with an exception.
val thrown = assertFailsWith<Exception> {
future.await()
Expand Down
2 changes: 1 addition & 1 deletion pubnub-kotlin/pubnub-kotlin-test/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ kotlin {
dependencies {
api(project(":pubnub-kotlin:pubnub-kotlin-api"))
api(kotlin("test"))
api(libs.coroutines.test)
api(libs.kotlinx.coroutines.test)
}
}

Expand Down
3 changes: 2 additions & 1 deletion settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@ include("examples:kotlin-app")
include("examples:java-app")
includeBuild("build-logic/ktlint-custom-rules")
includeBuild("migration_utils")
include("pubnub-kotlin-coroutines")
include("pubnub-kotlin:pubnub-kotlin-coroutines")

0 comments on commit 87819fd

Please sign in to comment.