-
Notifications
You must be signed in to change notification settings - Fork 42
[ECO-5338] Liveobject unit/integration test setup #1095
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
c444220
[ECO-5338] Created tests package for liveobjects
sacOO7 e0b4dcd
[ECO-5338] Updated CI scripts to run liveobject plugin tests
sacOO7 9cac543
[ECO-5338] Fixed liveobject test dependency, utils as per review comm…
sacOO7 1a7fafd
[ECO-5338] Fixed liveobject test dependency
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
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
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,35 @@ | ||
| 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) |
60 changes: 60 additions & 0 deletions
60
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,60 @@ | ||
| 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 | ||
| } | ||
|
|
||
| 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 } ?: error("Method '$methodName' not found") | ||
| 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 | ||
|
|
||
| class LiveObjectTest : IntegrationTest() { | ||
|
|
||
| @Test | ||
| fun testChannelObjectGetterTest() = runTest { | ||
| val channelName = generateChannelName() | ||
| val channel = getRealtimeChannel(channelName) | ||
| val objects = channel.objects | ||
| assertNotNull(objects) | ||
| } | ||
| } |
98 changes: 98 additions & 0 deletions
98
live-objects/src/test/kotlin/io/ably/lib/objects/integration/setup/IntegrationTest.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,98 @@ | ||
| package io.ably.lib.objects.integration.setup | ||
|
|
||
| import io.ably.lib.realtime.AblyRealtime | ||
| import io.ably.lib.realtime.Channel | ||
| import io.ably.lib.types.ChannelMode | ||
| import io.ably.lib.types.ChannelOptions | ||
| import kotlinx.coroutines.runBlocking | ||
| import org.junit.After | ||
| import org.junit.AfterClass | ||
| import org.junit.BeforeClass | ||
| import org.junit.Rule | ||
| import org.junit.rules.Timeout | ||
| import org.junit.runner.RunWith | ||
| import org.junit.runners.Parameterized | ||
| import java.util.UUID | ||
|
|
||
| @RunWith(Parameterized::class) | ||
| abstract class IntegrationTest { | ||
| @Parameterized.Parameter | ||
| lateinit var testParams: String | ||
|
|
||
| @JvmField | ||
| @Rule | ||
| val timeout: Timeout = Timeout.seconds(10) | ||
|
|
||
| private val realtimeClients = mutableMapOf<String, AblyRealtime>() | ||
|
|
||
| /** | ||
| * Retrieves a realtime channel for the specified channel name and client ID | ||
| * If a client with the given clientID does not exist, a new client is created using the provided options. | ||
| * The channel is attached and ensured to be in the attached state before returning. | ||
| * | ||
| * @param channelName Name of the channel | ||
| * @param clientId The ID of the client to use or create. Defaults to "client1". | ||
| * @return The attached realtime channel. | ||
| * @throws Exception If the channel fails to attach or the client fails to connect. | ||
| */ | ||
| internal suspend fun getRealtimeChannel(channelName: String, clientId: String = "client1"): Channel { | ||
| val client = realtimeClients.getOrPut(clientId) { | ||
| sandbox.createRealtimeClient { | ||
| this.clientId = clientId | ||
| useBinaryProtocol = testParams == "msgpack_protocol" | ||
| }. apply { ensureConnected() } | ||
| } | ||
| val channelOpts = ChannelOptions().apply { | ||
| modes = arrayOf(ChannelMode.object_publish, ChannelMode.object_subscribe) | ||
| } | ||
| return client.channels.get(channelName, channelOpts).apply { | ||
| attach() | ||
| ensureAttached() | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Generates a unique channel name for testing purposes. | ||
| * This is mainly to avoid channel name/state/history collisions across tests in same file. | ||
| */ | ||
| internal fun generateChannelName(): String { | ||
| return "test-channel-${UUID.randomUUID()}" | ||
| } | ||
|
|
||
| @After | ||
| fun afterEach() { | ||
| for (ablyRealtime in realtimeClients.values) { | ||
| for ((channelName, channel) in ablyRealtime.channels.entrySet()) { | ||
| channel.off() | ||
| ablyRealtime.channels.release(channelName) | ||
| } | ||
| ablyRealtime.close() | ||
| } | ||
| realtimeClients.clear() | ||
| } | ||
|
|
||
| companion object { | ||
| private lateinit var sandbox: Sandbox | ||
|
|
||
| @JvmStatic | ||
| @Parameterized.Parameters(name = "{0}") | ||
| fun data(): Iterable<String> { | ||
| return listOf("msgpack_protocol", "json_protocol") | ||
| } | ||
|
|
||
| @JvmStatic | ||
| @BeforeClass | ||
| @Throws(Exception::class) | ||
| fun setUpBeforeClass() { | ||
| runBlocking { | ||
| sandbox = Sandbox.createInstance() | ||
| } | ||
| } | ||
|
|
||
| @JvmStatic | ||
| @AfterClass | ||
| @Throws(Exception::class) | ||
| fun tearDownAfterClass() { | ||
| } | ||
| } | ||
| } | ||
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.