Network/test/dorkboxTest/network/memoryTest/MemoryTest.kt

178 lines
5.0 KiB
Kotlin
Raw Normal View History

2020-09-29 15:43:37 +02:00
/*
2023-06-06 00:10:20 +02:00
* Copyright 2023 dorkbox, llc
2020-09-29 15:43:37 +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.memoryTest
import ch.qos.logback.classic.Level
import dorkbox.network.Client
import dorkbox.network.Server
import dorkbox.network.connection.Connection
import dorkbox.network.rmi.RemoteObject
import dorkboxTest.network.BaseTest
import kotlinx.coroutines.runBlocking
import org.junit.Ignore
import org.junit.Test
import java.util.concurrent.atomic.*
2020-09-29 15:43:37 +02:00
@Ignore
class MemoryTest : BaseTest() {
init {
// the logger cannot keep-up if it's on trace
setLogLevel(Level.DEBUG)
}
@Test
fun runForeverIpcAsyncNormal() {
runBlocking {
2023-06-28 15:12:55 +02:00
val counter = AtomicLong(0)
2021-07-02 11:40:20 +02:00
val RMI_ID = 12251
2020-09-29 15:43:37 +02:00
run {
val configuration = serverConfig()
2021-07-02 11:40:20 +02:00
configuration.serialization.rmi.register(TestObject::class.java, TestObjectImpl::class.java)
2020-09-29 15:43:37 +02:00
val server = Server<Connection>(configuration)
2021-07-02 11:40:20 +02:00
server.rmiGlobal.save(TestObjectImpl(counter), RMI_ID)
server.bindIpc()
2020-09-29 15:43:37 +02:00
}
run {
val client = Client<Connection>(clientConfig())
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-29 15:43:37 +02:00
obj.async = true
var i = 0L
while (true) {
i++
try {
remoteObject.setOther(i)
} catch (e: Exception) {
logger.error("Timeout when calling RMI method")
2020-09-29 15:43:37 +02:00
e.printStackTrace()
}
}
}
2023-06-28 15:12:55 +02:00
client.connectIpc()
2020-09-29 15:43:37 +02:00
}
Thread.sleep(Long.MAX_VALUE)
}
}
@Test
fun runForeverIpcAsyncSuspend() {
2023-06-28 15:12:55 +02:00
val counter = AtomicLong(0)
2021-07-02 11:40:20 +02:00
val RMI_ID = 12251
2020-09-29 15:43:37 +02:00
runBlocking {
run {
val configuration = serverConfig()
2021-07-02 11:40:20 +02:00
configuration.serialization.rmi.register(TestObject::class.java, TestObjectImpl::class.java)
2020-09-29 15:43:37 +02:00
val server = Server<Connection>(configuration)
2021-07-02 11:40:20 +02:00
server.rmiGlobal.save(TestObjectImpl(counter), RMI_ID)
server.bindIpc()
2020-09-29 15:43:37 +02:00
}
run {
val client = Client<Connection>(clientConfig())
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-29 15:43:37 +02:00
obj.async = true
var i = 0L
while (true) {
i++
try {
2023-09-04 00:47:46 +02:00
runBlocking {
remoteObject.setOtherSus(i)
}
2020-09-29 15:43:37 +02:00
} catch (e: Exception) {
logger.error("Timeout when calling RMI method")
2020-09-29 15:43:37 +02:00
e.printStackTrace()
}
}
}
2023-06-28 15:12:55 +02:00
client.connectIpc()
2020-09-29 15:43:37 +02:00
}
Thread.sleep(Long.MAX_VALUE)
}
}
@Test
fun runForeverIpc() {
runBlocking {
2023-07-01 13:18:30 +02:00
val server = run {
2020-09-29 15:43:37 +02:00
val configuration = serverConfig()
val server = Server<Connection>(configuration)
server.onMessage<Long> { testObject ->
send(testObject+1)
2020-09-29 15:43:37 +02:00
}
2023-07-01 13:18:30 +02:00
server
2020-09-29 15:43:37 +02:00
}
2023-07-01 13:18:30 +02:00
val client = run {
2020-09-29 15:43:37 +02:00
val client = Client<Connection>(clientConfig())
client.onMessage<Long> { testObject ->
send(testObject+1)
2020-09-29 15:43:37 +02:00
}
client.onConnect {
send(0L)
2020-09-29 15:43:37 +02:00
}
2023-07-01 13:18:30 +02:00
client
2020-09-29 15:43:37 +02:00
}
server.bindIpc()
2023-07-01 13:18:30 +02:00
client.connectIpc()
2020-09-29 15:43:37 +02:00
Thread.sleep(Long.MAX_VALUE)
}
}
private interface TestObject {
fun setOther(value: Long): Boolean
suspend fun setOtherSus(value: Long): Boolean
}
private class TestObjectImpl(private val counter: AtomicLong) : TestObject {
@Override
2021-07-02 16:18:49 +02:00
override fun setOther(value: Long): Boolean {
2020-09-29 15:43:37 +02:00
counter.getAndIncrement()
return true
}
@Override
2021-07-02 16:18:49 +02:00
override suspend fun setOtherSus(value: Long): Boolean {
2020-09-29 15:43:37 +02:00
counter.getAndIncrement()
return true
}
}
}