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 7, 2025
1 parent 87819fd commit d1df975
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -1,42 +1,24 @@
package com.pubnub.coroutines

import com.pubnub.api.v2.callbacks.Consumer
import com.pubnub.kmp.PNFuture
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

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`() = 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)
assertEquals(expected, expected.asFuture().await())
}

@Test
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 = Unit.asFuture().then { error("exception!") }

val future = exception.asFuture().then { throw it }
// When await() is called, it should resume with an exception.
val thrown = assertFailsWith<Exception> {
future.await()
Expand Down
2 changes: 1 addition & 1 deletion settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ include("pubnub-kotlin")
include("pubnub-kotlin:pubnub-kotlin-core-api")
include("pubnub-kotlin:pubnub-kotlin-api")
include("pubnub-kotlin:pubnub-kotlin-impl")
include("pubnub-kotlin:pubnub-kotlin-coroutines")
include("pubnub-kotlin:pubnub-kotlin-test")
include("pubnub-gson")
include("pubnub-gson:pubnub-gson-api")
Expand All @@ -29,5 +30,4 @@ include("examples:kotlin-app")
include("examples:java-app")
includeBuild("build-logic/ktlint-custom-rules")
includeBuild("migration_utils")
include("pubnub-kotlin:pubnub-kotlin-coroutines")

0 comments on commit d1df975

Please sign in to comment.