-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #122 from GetStream/feature/stream-result-call-tests
[PBE-3875] Feature/stream result call tests
- Loading branch information
Showing
10 changed files
with
379 additions
and
15 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
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
95 changes: 95 additions & 0 deletions
95
...t-call-retrofit/src/test/kotlin/io/getstream/result/call/retrofit/BlockedRetrofit2Call.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,95 @@ | ||
/* | ||
* Copyright (c) 2014-2022 Stream.io Inc. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.getstream.result.call.retrofit | ||
|
||
import io.getstream.result.call.dispatcher.CallDispatcherProvider | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.delay | ||
import kotlinx.coroutines.launch | ||
import kotlinx.coroutines.runBlocking | ||
import kotlinx.coroutines.withContext | ||
import okhttp3.Request | ||
import okio.Timeout | ||
import org.mockito.kotlin.mock | ||
import retrofit2.Call | ||
import retrofit2.Callback | ||
import retrofit2.Response | ||
import java.io.IOException | ||
import java.util.concurrent.atomic.AtomicBoolean | ||
|
||
internal class BlockedRetrofit2Call<T>( | ||
private val scope: CoroutineScope, | ||
private val value: T? = null, | ||
private val error: IOException? = null | ||
) : retrofit2.Call<T> { | ||
|
||
init { | ||
if (value == null) { | ||
requireNotNull(error) { | ||
"BlockedRetrofit2Call should be initialized with an error or value not null" | ||
} | ||
} | ||
if (error == null) { | ||
requireNotNull(value) { | ||
"BlockedRetrofit2Call should be initialized with an error or value not null" | ||
} | ||
} | ||
if (error != null && value != null) error("BlockedRetrofit2Call can't be initialized with a value and an error") | ||
} | ||
|
||
private val isBlocked = AtomicBoolean(true) | ||
private val started = AtomicBoolean(false) | ||
private val completed = AtomicBoolean(false) | ||
private val cancelled = AtomicBoolean(false) | ||
|
||
fun unblock() { | ||
isBlocked.set(false) | ||
} | ||
|
||
private suspend fun run() = withContext(CallDispatcherProvider.IO) { | ||
started.set(true) | ||
while (isBlocked.get()) { | ||
delay(10) | ||
} | ||
if (!cancelled.get()) completed.set(true) | ||
} | ||
|
||
fun isStarted(): Boolean = started.get() | ||
fun isCompleted(): Boolean = completed.get() | ||
override fun enqueue(callback: Callback<T>) { | ||
scope.launch { | ||
run() | ||
if (value != null) callback.onResponse(this@BlockedRetrofit2Call, Response.success(value)) | ||
if (error != null) callback.onFailure(this@BlockedRetrofit2Call, error) | ||
} | ||
} | ||
|
||
override fun execute(): Response<T> = runBlocking { | ||
run() | ||
if (value != null) { | ||
Response.success(value) | ||
} else { | ||
throw error!! | ||
} | ||
} | ||
|
||
override fun isExecuted(): Boolean = started.get() | ||
override fun cancel() { cancelled.set(true) } | ||
override fun isCanceled(): Boolean = cancelled.get() | ||
override fun request(): Request = mock() | ||
override fun timeout(): Timeout = mock() | ||
override fun clone(): Call<T> = BlockedRetrofit2Call(scope, value, error) | ||
} |
37 changes: 37 additions & 0 deletions
37
stream-result-call-retrofit/src/test/kotlin/io/getstream/result/call/retrofit/Mother.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,37 @@ | ||
/* | ||
* Copyright (c) 2014-2022 Stream.io Inc. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.getstream.result.call | ||
|
||
import kotlin.random.Random | ||
|
||
private val charPool: CharArray = (('a'..'z') + ('A'..'Z') + ('0'..'9')).toCharArray() | ||
|
||
public fun positiveRandomInt(maxInt: Int = Int.MAX_VALUE - 1): Int = | ||
Random.nextInt(1, maxInt + 1) | ||
|
||
public fun positiveRandomLong(maxLong: Long = Long.MAX_VALUE - 1): Long = | ||
Random.nextLong(1, maxLong + 1) | ||
|
||
public fun randomInt(): Int = Random.nextInt() | ||
public fun randomIntBetween(min: Int, max: Int): Int = Random.nextInt(min, max + 1) | ||
public fun randomLong(): Long = Random.nextLong() | ||
public fun randomLongBetween(min: Long, max: Long = Long.MAX_VALUE - 1): Long = Random.nextLong(min, max + 1) | ||
public fun randomBoolean(): Boolean = Random.nextBoolean() | ||
public fun randomString(size: Int = 20): String = buildString(capacity = size) { | ||
repeat(size) { | ||
append(charPool.random()) | ||
} | ||
} |
154 changes: 154 additions & 0 deletions
154
...esult-call-retrofit/src/test/kotlin/io/getstream/result/call/retrofit/RetrofitCallTest.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,154 @@ | ||
/* | ||
* Copyright (c) 2014-2024 Stream.io Inc. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.getstream.result.call.retrofit | ||
|
||
import io.getstream.result.Error | ||
import io.getstream.result.Result | ||
import io.getstream.result.call.Call | ||
import io.getstream.result.call.randomString | ||
import kotlinx.coroutines.async | ||
import kotlinx.coroutines.delay | ||
import kotlinx.coroutines.test.runTest | ||
import org.amshove.kluent.`should be equal to` | ||
import org.amshove.kluent.`should be instance of` | ||
import org.amshove.kluent.shouldBeInstanceOf | ||
import org.junit.jupiter.api.Test | ||
import org.junit.jupiter.api.extension.RegisterExtension | ||
import org.mockito.Mockito | ||
import org.mockito.kotlin.any | ||
import org.mockito.kotlin.mock | ||
import org.mockito.kotlin.never | ||
import org.mockito.kotlin.only | ||
import java.io.IOException | ||
|
||
internal class RetrofitCallTest { | ||
|
||
companion object { | ||
@JvmField | ||
@RegisterExtension | ||
val testCoroutines = TestCoroutineExtension() | ||
} | ||
|
||
val resultValue = randomString() | ||
val validResult: Result<String> = Result.Success(resultValue) | ||
val parser: ErrorParser<*> = mock() | ||
|
||
@Test | ||
fun `Call should be executed and return a valid result`() = runTest { | ||
val blockedRetrofit2Call = BlockedRetrofit2Call(testCoroutines.scope, value = resultValue).apply { unblock() } | ||
val call = RetrofitCall(blockedRetrofit2Call, parser, testCoroutines.scope) | ||
|
||
val result = call.execute() | ||
|
||
result `should be equal to` validResult | ||
blockedRetrofit2Call.isStarted() `should be equal to` true | ||
blockedRetrofit2Call.isCompleted() `should be equal to` true | ||
} | ||
|
||
@Test | ||
fun `Call should be executed and return a failure result`() = runTest { | ||
val blockedRetrofit2Call = | ||
BlockedRetrofit2Call<String>(testCoroutines.scope, error = IOException(randomString())).apply { unblock() } | ||
val call = RetrofitCall(blockedRetrofit2Call, parser, testCoroutines.scope) | ||
|
||
val result = call.execute() | ||
|
||
result.shouldBeInstanceOf(Result.Failure::class) | ||
(result as Result.Failure).value `should be instance of` Error::class | ||
blockedRetrofit2Call.isStarted() `should be equal to` true | ||
blockedRetrofit2Call.isCompleted() `should be equal to` true | ||
} | ||
|
||
@Test | ||
fun `Canceled Call should be executed and return a cancel error`() = runTest { | ||
val blockedRetrofit2Call = | ||
BlockedRetrofit2Call<String>(testCoroutines.scope, error = IOException(randomString())) | ||
val call = RetrofitCall(blockedRetrofit2Call, parser, testCoroutines.scope) | ||
|
||
val deferedResult = async { call.execute() } | ||
call.cancel() | ||
blockedRetrofit2Call.unblock() | ||
val result = deferedResult.await() | ||
|
||
result.shouldBeInstanceOf(Result.Failure::class) | ||
(result as Result.Failure).value `should be instance of` Error::class | ||
blockedRetrofit2Call.isStarted() `should be equal to` true | ||
blockedRetrofit2Call.isCompleted() `should be equal to` false | ||
blockedRetrofit2Call.isCanceled() `should be equal to` true | ||
} | ||
|
||
@Test | ||
fun `Call should be enqueued and return a valid result by the callback`() = runTest { | ||
val callback: Call.Callback<String> = mock() | ||
val blockedRetrofit2Call = BlockedRetrofit2Call(testCoroutines.scope, value = resultValue).apply { unblock() } | ||
val call = RetrofitCall(blockedRetrofit2Call, parser, testCoroutines.scope) | ||
|
||
call.enqueue(callback) | ||
|
||
Mockito.verify(callback, only()).onResult( | ||
org.mockito.kotlin.check { | ||
it `should be equal to` validResult | ||
} | ||
) | ||
blockedRetrofit2Call.isStarted() `should be equal to` true | ||
blockedRetrofit2Call.isCompleted() `should be equal to` true | ||
} | ||
|
||
@Test | ||
fun `Canceled Call should be enqueued and shouldn't return value on the callback`() = runTest { | ||
val callback: Call.Callback<String> = mock() | ||
val blockedRetrofit2Call = BlockedRetrofit2Call(testCoroutines.scope, value = resultValue) | ||
val call = RetrofitCall(blockedRetrofit2Call, parser, testCoroutines.scope) | ||
|
||
call.enqueue(callback) | ||
call.cancel() | ||
blockedRetrofit2Call.unblock() | ||
|
||
Mockito.verify(callback, never()).onResult(any()) | ||
blockedRetrofit2Call.isStarted() `should be equal to` true | ||
blockedRetrofit2Call.isCompleted() `should be equal to` false | ||
blockedRetrofit2Call.isCanceled() `should be equal to` true | ||
} | ||
|
||
@Test | ||
fun `Call should be executed asynchronous and return a valid result`() = runTest { | ||
val blockedRetrofit2Call = BlockedRetrofit2Call(testCoroutines.scope, value = resultValue).apply { unblock() } | ||
val call = RetrofitCall(blockedRetrofit2Call, parser, testCoroutines.scope) | ||
|
||
val result = call.await() | ||
|
||
result `should be equal to` validResult | ||
blockedRetrofit2Call.isStarted() `should be equal to` true | ||
blockedRetrofit2Call.isCompleted() `should be equal to` true | ||
} | ||
|
||
@Test | ||
fun `Canceled Call should be executed asynchronous and return a cancel error`() = runTest { | ||
val blockedRetrofit2Call = BlockedRetrofit2Call(testCoroutines.scope, value = resultValue) | ||
val call = RetrofitCall(blockedRetrofit2Call, parser, testCoroutines.scope) | ||
|
||
val deferedResult = async { call.await() } | ||
delay(10) | ||
call.cancel() | ||
blockedRetrofit2Call.unblock() | ||
val result = deferedResult.await() | ||
|
||
result `should be equal to` Call.callCanceledError() | ||
blockedRetrofit2Call.isStarted() `should be equal to` true | ||
blockedRetrofit2Call.isCompleted() `should be equal to` false | ||
blockedRetrofit2Call.isCanceled() `should be equal to` true | ||
} | ||
} |
Oops, something went wrong.