2020-08-19 15:29:35 +02:00
|
|
|
/*
|
2023-06-06 00:09:22 +02:00
|
|
|
* Copyright 2023 dorkbox, llc
|
2020-08-19 15:29:35 +02:00
|
|
|
*
|
|
|
|
* 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 dorkboxTest.network.rmi
|
|
|
|
|
2020-08-13 11:06:44 +02:00
|
|
|
import dorkbox.network.Client
|
|
|
|
import dorkbox.network.Configuration
|
|
|
|
import dorkbox.network.Server
|
|
|
|
import dorkbox.network.connection.Connection
|
2020-08-26 15:34:04 +02:00
|
|
|
import dorkbox.network.serialization.Serialization
|
2020-08-13 11:06:44 +02:00
|
|
|
import dorkboxTest.network.BaseTest
|
2021-07-02 16:18:40 +02:00
|
|
|
import kotlinx.coroutines.runBlocking
|
2020-08-13 11:06:44 +02:00
|
|
|
import org.junit.Test
|
2023-06-06 00:09:22 +02:00
|
|
|
import java.util.concurrent.*
|
2022-03-15 12:44:52 +01:00
|
|
|
import java.util.concurrent.atomic.*
|
2020-08-13 11:06:44 +02:00
|
|
|
|
|
|
|
class RmiDelayedInvocationTest : BaseTest() {
|
|
|
|
private val iterateLock = Any()
|
|
|
|
private val OBJ_ID = 123
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun rmiNetwork() {
|
2021-04-30 18:22:38 +02:00
|
|
|
rmi()
|
2020-08-13 11:06:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2020-09-02 03:18:10 +02:00
|
|
|
fun rmiIpc() {
|
2021-04-30 18:22:38 +02:00
|
|
|
rmi {
|
|
|
|
enableIpc = true
|
2020-09-02 03:18:10 +02:00
|
|
|
}
|
2020-08-13 11:06:44 +02:00
|
|
|
}
|
|
|
|
|
2021-07-09 15:16:20 +02:00
|
|
|
fun register(serialization: Serialization<*>) {
|
2021-07-02 11:40:20 +02:00
|
|
|
serialization.rmi.register(TestObject::class.java, TestObjectImpl::class.java)
|
2020-08-13 11:06:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* In this test the server has two objects in an object space. The client
|
|
|
|
* uses the first remote object to get the second remote object.
|
|
|
|
*/
|
2021-04-30 18:22:38 +02:00
|
|
|
fun rmi(config: Configuration.() -> Unit = {}) {
|
2023-06-06 00:09:22 +02:00
|
|
|
val countDownLatch = CountDownLatch(1)
|
2020-08-13 11:06:44 +02:00
|
|
|
run {
|
|
|
|
val configuration = serverConfig()
|
|
|
|
config(configuration)
|
|
|
|
register(configuration.serialization)
|
|
|
|
|
|
|
|
val server = Server<Connection>(configuration)
|
|
|
|
addEndPoint(server)
|
|
|
|
|
2023-06-06 00:09:22 +02:00
|
|
|
server.rmiGlobal.save(TestObjectImpl(countDownLatch), OBJ_ID)
|
2023-06-26 19:28:55 +02:00
|
|
|
server.bind(2000)
|
2020-08-13 11:06:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
run {
|
|
|
|
val configuration = clientConfig()
|
|
|
|
config(configuration)
|
|
|
|
|
|
|
|
val client = Client<Connection>(configuration)
|
|
|
|
addEndPoint(client)
|
|
|
|
|
2021-04-30 22:39:36 +02:00
|
|
|
client.onConnect {
|
2021-07-02 11:40:20 +02:00
|
|
|
val remoteObject = rmi.getGlobal<TestObject>(OBJ_ID)
|
2020-08-13 11:06:44 +02:00
|
|
|
|
2020-08-27 13:55:32 +02:00
|
|
|
val totalRuns = 100
|
2020-08-13 11:06:44 +02:00
|
|
|
var abort = false
|
2021-04-30 22:39:36 +02:00
|
|
|
logger.error("Running for $totalRuns iterations....")
|
2020-08-13 11:06:44 +02:00
|
|
|
|
|
|
|
for (i in 0 until totalRuns) {
|
|
|
|
if (abort) {
|
|
|
|
break
|
|
|
|
}
|
2020-08-27 13:55:32 +02:00
|
|
|
if (i % 10 == 0) {
|
2020-08-13 11:06:44 +02:00
|
|
|
System.err.println(i)
|
|
|
|
}
|
|
|
|
|
|
|
|
// sometimes, this method is never called right away.
|
2023-06-06 00:09:22 +02:00
|
|
|
// this will also count-down the latch
|
2020-08-13 11:06:44 +02:00
|
|
|
remoteObject.setOther(i.toFloat())
|
2021-07-02 16:18:40 +02:00
|
|
|
|
|
|
|
runBlocking {
|
2023-06-06 00:09:22 +02:00
|
|
|
try {
|
|
|
|
// it should be instant!!
|
|
|
|
countDownLatch.await(10, TimeUnit.SECONDS)
|
|
|
|
} catch (e: InterruptedException) {
|
|
|
|
logger.error("Failed after: $i")
|
|
|
|
e.printStackTrace()
|
|
|
|
abort = true
|
2020-08-13 11:06:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-04-30 22:39:36 +02:00
|
|
|
logger.error("Done with delay invocation test")
|
2020-08-13 11:06:44 +02:00
|
|
|
|
|
|
|
stopEndPoints()
|
|
|
|
}
|
|
|
|
|
2023-06-26 19:28:55 +02:00
|
|
|
client.connect(LOCALHOST, 2000)
|
2020-08-13 11:06:44 +02:00
|
|
|
}
|
|
|
|
|
2020-09-09 01:33:09 +02:00
|
|
|
waitForThreads()
|
2020-08-13 11:06:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private interface TestObject {
|
|
|
|
fun setOther(aFloat: Float)
|
|
|
|
fun other(): Float
|
|
|
|
}
|
|
|
|
|
2023-06-06 00:09:22 +02:00
|
|
|
private class TestObjectImpl(private val latch: CountDownLatch) : TestObject {
|
2020-08-13 11:06:44 +02:00
|
|
|
@Transient
|
|
|
|
private val ID = idCounter.getAndIncrement()
|
|
|
|
private var aFloat = 0f
|
|
|
|
|
|
|
|
override fun setOther(aFloat: Float) {
|
|
|
|
this.aFloat = aFloat
|
2023-06-06 00:09:22 +02:00
|
|
|
latch.countDown()
|
2020-08-13 11:06:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun other(): Float {
|
|
|
|
return aFloat
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun hashCode(): Int {
|
|
|
|
return ID
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
companion object {
|
|
|
|
private val idCounter = AtomicInteger()
|
|
|
|
}
|
|
|
|
}
|