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-19 01:30:13 +02:00
|
|
|
|
2020-09-02 15:02:52 +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
|
2022-03-15 14:44:50 +01:00
|
|
|
import kotlinx.coroutines.delay
|
2020-08-19 01:30:13 +02:00
|
|
|
import org.junit.Assert
|
|
|
|
import org.junit.Test
|
2022-03-15 12:44:52 +01:00
|
|
|
import java.util.concurrent.atomic.*
|
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 counter = AtomicLong(0)
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
2021-04-30 18:22:38 +02:00
|
|
|
fun rmi(config: Configuration.() -> Unit = {}) {
|
2020-08-19 01:30:13 +02:00
|
|
|
val server: Server<Connection>
|
|
|
|
|
2020-09-09 12:30:07 +02:00
|
|
|
val mod = 100_000L
|
2020-09-29 15:24:41 +02:00
|
|
|
val totalRuns = 1_000_000L
|
2020-08-27 13:55:32 +02:00
|
|
|
|
2020-08-19 01:30:13 +02:00
|
|
|
run {
|
|
|
|
val configuration = serverConfig()
|
|
|
|
config(configuration)
|
2020-09-02 15:02:52 +02:00
|
|
|
|
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
|
|
|
|
2020-08-27 13:55:32 +02:00
|
|
|
server = Server(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)
|
2020-09-02 03:18:10 +02:00
|
|
|
server.bind()
|
2020-08-19 01:30:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-09-09 12:30:07 +02:00
|
|
|
val client: Client<Connection>
|
2020-08-19 01:30:13 +02:00
|
|
|
run {
|
|
|
|
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)
|
|
|
|
|
2020-09-09 12:30:07 +02:00
|
|
|
client = Client<Connection>(configuration)
|
2020-08-19 01:30:13 +02:00
|
|
|
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>(RMI_ID)
|
2020-08-19 01:30:13 +02:00
|
|
|
val obj = remoteObject as 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
|
2021-04-30 22:39:36 +02:00
|
|
|
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.
|
2021-04-30 22:39:36 +02:00
|
|
|
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) {
|
2021-04-30 22:39:36 +02:00
|
|
|
logger.error("Timeout when calling RMI method")
|
2020-08-19 01:30:13 +02:00
|
|
|
e.printStackTrace()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-15 14:44:50 +01:00
|
|
|
// The async nature means that we don't know EXACTLY when all the messages will arrive. For testing, this is the closest
|
|
|
|
// we can do to attempt to have a correct info lookup.
|
|
|
|
var count = 0
|
|
|
|
while (counter.get() < totalRuns && count < 30) {
|
|
|
|
logger.error("Waiting for ${totalRuns - counter.get()} more messages...")
|
|
|
|
count++
|
|
|
|
delay(1_000)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-08-19 01:30:13 +02:00
|
|
|
// 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()
|
|
|
|
}
|
|
|
|
|
2022-03-15 12:44:52 +01:00
|
|
|
client.connect(LOCALHOST)
|
2020-08-19 01:30:13 +02:00
|
|
|
}
|
|
|
|
|
2020-09-23 15:59:09 +02:00
|
|
|
waitForThreads()
|
2020-10-06 02:08:12 +02:00
|
|
|
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
|
2021-04-30 22:39:36 +02:00
|
|
|
override fun setOther(value: Long): Boolean {
|
2020-08-19 01:30:13 +02:00
|
|
|
counter.getAndIncrement()
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|