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

138 lines
3.9 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.Assert
import org.junit.Test
import java.util.concurrent.atomic.*
2020-08-19 01:30:13 +02:00
2020-09-09 12:30:07 +02:00
class RmiSpamSyncTest : BaseTest() {
2020-08-19 01:30:13 +02:00
private val counter = AtomicLong(0)
private val RMI_ID = 12251
2020-09-09 12:30:07 +02:00
init {
// the logger cannot keep-up if it's on trace
2020-09-09 12:30:07 +02:00
setLogLevel(Level.DEBUG)
}
2020-08-19 01:30:13 +02:00
@Test
fun rmiNetwork() {
2021-04-30 18:22:38 +02:00
rmi()
2020-09-02 03:18:10 +02:00
}
@Test
fun rmiIpc() {
2021-04-30 18:22:38 +02:00
rmi() {
enableIpc = true
2020-08-28 10:21: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 = 400L
2020-09-29 15:24:41 +02:00
val totalRuns = 1_000L
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-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)
2021-07-02 11:40:20 +02:00
server.rmiGlobal.save(TestObjectImpl(counter), RMI_ID)
server
2020-08-19 01:30:13 +02:00
}
var ipc: Boolean
val client = run {
2020-08-19 01:30:13 +02:00
val configuration = clientConfig()
config(configuration)
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 = false
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.
client.logger.error("$i")
}
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()
}
}
// have to do this first, so it will wait for the client responses!
// if we close the client first, the connection will be closed, and the responses will never arrive to the server
stopEndPoints()
}
ipc = configuration.enableIpc
client
}
server.bind(2000)
if (ipc) {
client.connectIpc()
} else {
client.connect(LOCALHOST, 2000)
2020-08-19 01:30:13 +02:00
}
2020-09-09 12:30:07 +02:00
waitForThreads()
Assert.assertEquals(totalRuns, counter.get())
2020-08-19 01:30:13 +02:00
}
private interface TestObject {
fun setOther(value: Long): Boolean
}
private class TestObjectImpl(private val counter: AtomicLong) : TestObject {
@Override
2020-09-10 14:39:39 +02:00
override fun setOther(value: Long): Boolean {
2020-08-19 01:30:13 +02:00
counter.getAndIncrement()
return true
}
}
}