From a20c55a864efa75df124ef972cd3ce84718c223f Mon Sep 17 00:00:00 2001 From: nathan Date: Tue, 1 Sep 2020 06:42:09 +0200 Subject: [PATCH] Added rmi registration order checks --- .../network/SerializationValidationTest.kt | 85 ++++++++++++++++++- 1 file changed, 84 insertions(+), 1 deletion(-) diff --git a/test/dorkboxTest/network/SerializationValidationTest.kt b/test/dorkboxTest/network/SerializationValidationTest.kt index 9e3106aa..7a5b1bdc 100644 --- a/test/dorkboxTest/network/SerializationValidationTest.kt +++ b/test/dorkboxTest/network/SerializationValidationTest.kt @@ -20,11 +20,12 @@ import dorkbox.network.Server import dorkbox.network.connection.Connection import dorkbox.network.serialization.Serialization import kotlinx.coroutines.runBlocking +import org.junit.Assert import org.junit.Test class SerializationValidationTest : BaseTest() { @Test - fun checkObjects() { + fun checkManyObjects() { run { val configuration = serverConfig() register(configuration.serialization) @@ -62,6 +63,58 @@ class SerializationValidationTest : BaseTest() { waitForThreads() } + + @Test + fun checkOutOfOrder() { + run { + val configuration = serverConfig() + configuration.serialization.registerRmi(TestObject::class.java, TestObjectImpl::class.java) + configuration.serialization.register(TestObject::class.java) // the iface is again, on purpose to verify registration order! + + val server = Server(configuration) + addEndPoint(server) + + server.onMessage { connection, message -> + stopEndPoints() + } + + runBlocking { + server.bind(false) + } + } + + + run { + val configuration = clientConfig() + configuration.serialization.register(TestObject::class.java) + + val client = Client(configuration) + addEndPoint(client) + + client.onConnect { connection -> + connection.logger.error("Connected") + connection.createObject { rmiId, remoteObject -> + connection.logger.error("Starting test") + remoteObject.setValue(43.21f) + + // Normal remote method call. + Assert.assertEquals(43.21f, remoteObject.other(), .0001f) + + // When a proxy object is sent, the other side receives its ACTUAL object (not a proxy of it), because + // that is where that object actually exists. + connection.send(remoteObject) + } + } + + + runBlocking { + client.connect(LOOPBACK) + } + } + + waitForThreads() + } + private fun register(serialization: Serialization) { serialization.register(Command1::class.java) serialization.register(Command2::class.java) @@ -465,4 +518,34 @@ class SerializationValidationTest : BaseTest() { private class Command198 private class Command199 private class FinishedCommand + + + private interface TestObject { + suspend fun setValue(aFloat: Float) + fun other(): Float + } + + private class TestObjectImpl : TestObject { + private var aFloat = 0f + override suspend fun setValue(aFloat: Float) { + throw RuntimeException("Whoops!") + } + + suspend fun setValue(connection: Connection, aFloat: Float) { + connection.logger.error("receiving") + this.aFloat = aFloat + } + + override fun other(): Float { + throw RuntimeException("Whoops!") + } + + fun other(connection: Connection): Float { + return aFloat + } + + override fun hashCode(): Int { + return 1 + } + } }