Network/test/dorkboxTest/network/rmi/RmiSpamAsyncTest.kt

130 lines
3.6 KiB
Kotlin
Raw Normal View History

2020-08-19 15:29:35 +02:00
/*
2023-06-16 11:18:03 +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-19 01:30:13 +02:00
import ch.qos.logback.classic.Level
2020-08-19 01:30:13 +02:00
import dorkbox.network.Client
import dorkbox.network.Configuration
import dorkbox.network.Server
import dorkbox.network.connection.Connection
import dorkbox.network.rmi.RemoteObject
import dorkboxTest.network.BaseTest
import org.junit.Test
2023-06-25 17:25:29 +02:00
import java.util.concurrent.*
2020-08-19 01:30:13 +02:00
2020-09-09 12:30:07 +02:00
class RmiSpamAsyncTest : BaseTest() {
2020-08-19 01:30:13 +02:00
private val RMI_ID = 12251
2020-09-02 03:18:10 +02:00
@Test
2020-09-09 12:30:07 +02:00
fun rmiNetworkAsync() {
2021-04-30 18:22:38 +02:00
rmi()
2020-09-02 03:18:10 +02:00
}
2020-08-19 01:30:13 +02:00
@Test
2020-09-02 03:18:10 +02:00
fun rmiIpcAsync() {
2021-04-30 18:22:38 +02:00
rmi() {
enableIpc = true
2020-09-02 03:18:10 +02:00
}
2020-08-19 01:30:13 +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.
*/
2023-06-25 17:25:29 +02:00
private fun rmi(config: Configuration.() -> Unit = {}) {
2020-09-09 12:30:07 +02:00
val mod = 100_000L
2023-06-25 17:25:29 +02:00
val totalRuns = 1_000_000
val latch = CountDownLatch(totalRuns)
2020-08-27 13:55:32 +02:00
val server = run {
2020-08-19 01:30:13 +02:00
val configuration = serverConfig()
config(configuration)
2021-04-30 22:17:26 +02:00
// the logger cannot keep-up if it's on trace
setLogLevel(Level.DEBUG)
2021-07-02 11:40:20 +02:00
configuration.serialization.rmi.register(TestObject::class.java, TestObjectImpl::class.java)
2020-08-19 01:30:13 +02:00
val server = Server<Connection>(configuration)
2020-08-19 01:30:13 +02:00
addEndPoint(server)
2023-06-25 17:25:29 +02:00
server.rmiGlobal.save(TestObjectImpl(latch), RMI_ID)
server
2020-08-19 01:30:13 +02:00
}
val client = run {
2020-08-19 01:30:13 +02:00
val configuration = clientConfig()
config(configuration)
2021-04-30 22:17:26 +02:00
// the logger cannot keep-up if it's on trace
setLogLevel(Level.DEBUG)
val client = Client<Connection>(configuration)
2020-08-19 01:30:13 +02:00
addEndPoint(client)
client.onConnect {
2021-07-02 11:40:20 +02:00
val remoteObject = rmi.getGlobal<TestObject>(RMI_ID)
2023-02-27 11:28:24 +01:00
val obj = RemoteObject.cast(remoteObject)
2020-09-09 12:30:07 +02:00
obj.async = true
2020-08-19 01:30:13 +02:00
var started = false
for (i in 0 until totalRuns) {
if (!started) {
started = true
logger.error("Running for $totalRuns iterations....")
2020-08-19 01:30:13 +02:00
}
2020-08-27 13:55:32 +02:00
if (i % mod == 0L) {
2020-08-19 01:30:13 +02:00
// this doesn't always output to the console. weird.
logger.error("$i")
2020-08-19 01:30:13 +02:00
}
try {
2020-09-29 15:24:41 +02:00
remoteObject.setOther(i)
2020-08-19 01:30:13 +02:00
} catch (e: Exception) {
logger.error("Timeout when calling RMI method")
2020-08-19 01:30:13 +02:00
e.printStackTrace()
}
}
}
client
2020-08-19 01:30:13 +02:00
}
server.bind(2000)
client.connect(LOCALHOST, 2000)
2023-06-25 17:25:29 +02:00
latch.await()
stopEndPointsBlocking()
waitForThreads()
2020-08-19 01:30:13 +02:00
}
private interface TestObject {
2023-06-25 17:25:29 +02:00
fun setOther(value: Int): Boolean
2020-08-19 01:30:13 +02:00
}
2023-06-25 17:25:29 +02:00
private class TestObjectImpl(private val latch: CountDownLatch) : TestObject {
2020-08-19 01:30:13 +02:00
@Override
2023-06-25 17:25:29 +02:00
override fun setOther(value: Int): Boolean {
latch.countDown()
2020-08-19 01:30:13 +02:00
return true
}
}
}