-
Notifications
You must be signed in to change notification settings - Fork 40
[ECO-5338] Liveobjects: Setup tests #1092
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
+454
−26
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f61cc6a
[ECO-5338] Created tests package for liveobjects
sacOO7 0b44a36
[ECO-5338] Created unit/integration test package for liveobjects
sacOO7 af9deda
[ECO-5338[Integration test] Added ably sandbox script
sacOO7 1adf275
[ECO-5338][Unit test] Created unittest setup class for initial setup
sacOO7 5d3316f
[ECO-5338][Unit test] Upgraded dependencies as per review comments, o…
sacOO7 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
11 changes: 11 additions & 0 deletions
11
live-objects/src/main/kotlin/io/ably/lib/objects/ErrorCodes.kt
This file contains hidden or 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,11 @@ | ||
package io.ably.lib.objects | ||
|
||
internal enum class ErrorCode(public val code: Int) { | ||
BadRequest(40_000), | ||
InternalError(50_000), | ||
} | ||
|
||
internal enum class HttpStatusCode(public val code: Int) { | ||
BadRequest(400), | ||
InternalServerError(500), | ||
} |
This file contains hidden or 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,36 @@ | ||
package io.ably.lib.objects | ||
|
||
import io.ably.lib.types.AblyException | ||
import io.ably.lib.types.ErrorInfo | ||
|
||
internal fun ablyException( | ||
errorMessage: String, | ||
errorCode: ErrorCode, | ||
statusCode: HttpStatusCode = HttpStatusCode.BadRequest, | ||
cause: Throwable? = null, | ||
): AblyException { | ||
val errorInfo = createErrorInfo(errorMessage, errorCode, statusCode) | ||
return createAblyException(errorInfo, cause) | ||
} | ||
|
||
internal fun ablyException( | ||
errorInfo: ErrorInfo, | ||
cause: Throwable? = null, | ||
): AblyException = createAblyException(errorInfo, cause) | ||
|
||
private fun createErrorInfo( | ||
errorMessage: String, | ||
errorCode: ErrorCode, | ||
statusCode: HttpStatusCode, | ||
) = ErrorInfo(errorMessage, statusCode.code, errorCode.code) | ||
|
||
private fun createAblyException( | ||
errorInfo: ErrorInfo, | ||
cause: Throwable?, | ||
) = cause?.let { AblyException.fromErrorInfo(it, errorInfo) } | ||
?: AblyException.fromErrorInfo(errorInfo) | ||
|
||
internal fun clientError(errorMessage: String) = ablyException(errorMessage, ErrorCode.BadRequest, HttpStatusCode.BadRequest) | ||
|
||
internal fun serverError(errorMessage: String) = ablyException(errorMessage, ErrorCode.InternalError, HttpStatusCode.InternalServerError) | ||
|
61 changes: 61 additions & 0 deletions
61
live-objects/src/test/kotlin/io/ably/lib/objects/TestUtils.kt
This file contains hidden or 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,61 @@ | ||
package io.ably.lib.objects | ||
|
||
import java.lang.reflect.Field | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.delay | ||
import kotlinx.coroutines.suspendCancellableCoroutine | ||
import kotlinx.coroutines.withContext | ||
import kotlinx.coroutines.withTimeout | ||
|
||
suspend fun assertWaiter(timeoutInMs: Long = 10_000, block: suspend () -> Boolean) { | ||
withContext(Dispatchers.Default) { | ||
withTimeout(timeoutInMs) { | ||
do { | ||
val success = block() | ||
delay(100) | ||
} while (!success) | ||
} | ||
} | ||
} | ||
|
||
fun Any.setPrivateField(name: String, value: Any?) { | ||
val valueField = javaClass.findField(name) | ||
valueField.isAccessible = true | ||
valueField.set(this, value) | ||
} | ||
|
||
fun <T>Any.getPrivateField(name: String): T { | ||
val valueField = javaClass.findField(name) | ||
valueField.isAccessible = true | ||
@Suppress("UNCHECKED_CAST") | ||
return valueField.get(this) as T | ||
} | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
private fun Class<*>.findField(name: String): Field { | ||
var result = kotlin.runCatching { getDeclaredField(name) } | ||
var currentClass = this | ||
while (result.isFailure && currentClass.superclass != null) // stop when we got field or reached top of class hierarchy | ||
{ | ||
currentClass = currentClass.superclass!! | ||
result = kotlin.runCatching { currentClass.getDeclaredField(name) } | ||
} | ||
if (result.isFailure) { | ||
throw result.exceptionOrNull() as Exception | ||
} | ||
return result.getOrNull() as Field | ||
} | ||
|
||
suspend fun <T> Any.invokePrivateSuspendMethod(methodName: String, vararg args: Any?): T = suspendCancellableCoroutine { cont -> | ||
val suspendMethod = javaClass.declaredMethods.find { it.name == methodName } | ||
?: error("Method '$methodName' not found") | ||
suspendMethod.isAccessible = true | ||
suspendMethod.invoke(this, *args, cont) | ||
} | ||
|
||
|
||
fun <T> Any.invokePrivateMethod(methodName: String, vararg args: Any?): T { | ||
val method = javaClass.declaredMethods.find { it.name == methodName } | ||
method?.isAccessible = true | ||
@Suppress("UNCHECKED_CAST") | ||
return method?.invoke(this, *args) as T | ||
} |
17 changes: 17 additions & 0 deletions
17
live-objects/src/test/kotlin/io/ably/lib/objects/integration/LiveObjectTest.kt
This file contains hidden or 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,17 @@ | ||
package io.ably.lib.objects.integration | ||
|
||
import io.ably.lib.objects.integration.setup.IntegrationTest | ||
import kotlinx.coroutines.test.runTest | ||
import org.junit.Test | ||
import kotlin.test.assertNotNull | ||
|
||
open class LiveObjectTest : IntegrationTest() { | ||
|
||
@Test | ||
fun testChannelObjectGetterTest() = runTest { | ||
val channelName = generateChannelName() | ||
val channel = getRealtimeChannel(channelName) | ||
val objects = channel.objects | ||
assertNotNull(objects) | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.