2020-08-19 15:29:35 +02:00
|
|
|
/*
|
|
|
|
* Copyright 2020 dorkbox, llc
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
import kotlinx.coroutines.runBlocking
|
|
|
|
import org.junit.Test
|
|
|
|
import java.util.concurrent.atomic.AtomicInteger
|
|
|
|
|
|
|
|
class RmiDelayedInvocationTest : BaseTest() {
|
|
|
|
private val iterateLock = Any()
|
|
|
|
private val OBJ_ID = 123
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun rmiNetwork() {
|
2020-08-28 10:21:10 +02:00
|
|
|
runBlocking {
|
2020-09-09 01:33:09 +02:00
|
|
|
rmi { configuration ->
|
2020-09-02 03:18:10 +02:00
|
|
|
configuration.enableIpcForLoopback = false
|
|
|
|
}
|
2020-08-28 10:21:10 +02:00
|
|
|
}
|
2020-08-13 11:06:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2020-09-02 03:18:10 +02:00
|
|
|
fun rmiIpc() {
|
|
|
|
runBlocking {
|
|
|
|
rmi()
|
|
|
|
}
|
2020-08-13 11:06:44 +02:00
|
|
|
}
|
|
|
|
|
2020-08-26 15:34:04 +02:00
|
|
|
fun register(serialization: Serialization) {
|
2020-08-13 11:06:44 +02:00
|
|
|
serialization.registerRmi(TestObject::class.java, TestObjectImpl::class.java)
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2020-08-28 10:21:10 +02:00
|
|
|
suspend fun rmi(config: (Configuration) -> Unit = {}) {
|
2020-08-13 11:06:44 +02:00
|
|
|
run {
|
|
|
|
val configuration = serverConfig()
|
|
|
|
config(configuration)
|
|
|
|
register(configuration.serialization)
|
|
|
|
|
|
|
|
val server = Server<Connection>(configuration)
|
|
|
|
addEndPoint(server)
|
|
|
|
|
|
|
|
server.saveGlobalObject(TestObjectImpl(iterateLock), OBJ_ID)
|
2020-09-02 03:18:10 +02:00
|
|
|
server.bind()
|
2020-08-13 11:06:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
run {
|
|
|
|
val configuration = clientConfig()
|
|
|
|
config(configuration)
|
|
|
|
|
|
|
|
val client = Client<Connection>(configuration)
|
|
|
|
addEndPoint(client)
|
|
|
|
|
|
|
|
client.onConnect { connection ->
|
|
|
|
val remoteObject = connection.getGlobalObject<TestObject>(OBJ_ID)
|
|
|
|
|
2020-08-27 13:55:32 +02:00
|
|
|
val totalRuns = 100
|
2020-08-13 11:06:44 +02:00
|
|
|
var abort = false
|
|
|
|
System.err.println("Running for $totalRuns iterations....")
|
|
|
|
|
|
|
|
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.
|
|
|
|
remoteObject.setOther(i.toFloat())
|
|
|
|
synchronized(iterateLock) {
|
|
|
|
try {
|
|
|
|
(iterateLock as Object).wait(1)
|
|
|
|
} catch (e: InterruptedException) {
|
|
|
|
System.err.println("Failed after: $i")
|
|
|
|
e.printStackTrace()
|
|
|
|
abort = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
System.err.println("Done with delay invocation test")
|
|
|
|
|
|
|
|
stopEndPoints()
|
|
|
|
}
|
|
|
|
|
2020-09-01 14:37:43 +02:00
|
|
|
client.connect(LOOPBACK)
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
private class TestObjectImpl(private val iterateLock: Any) : TestObject {
|
|
|
|
@Transient
|
|
|
|
private val ID = idCounter.getAndIncrement()
|
|
|
|
private var aFloat = 0f
|
|
|
|
|
|
|
|
override fun setOther(aFloat: Float) {
|
|
|
|
this.aFloat = aFloat
|
|
|
|
|
|
|
|
synchronized(iterateLock) { (iterateLock as Object).notify() }
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun other(): Float {
|
|
|
|
return aFloat
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun hashCode(): Int {
|
|
|
|
return ID
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
companion object {
|
|
|
|
private val idCounter = AtomicInteger()
|
|
|
|
}
|
|
|
|
}
|