2023-09-21 12:54:42 +02:00
|
|
|
/*
|
|
|
|
* Copyright 2023 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
|
|
|
|
|
|
|
|
import dorkbox.network.Client
|
|
|
|
import dorkbox.network.Server
|
|
|
|
import dorkbox.network.connection.Connection
|
2023-09-22 15:53:46 +02:00
|
|
|
import dorkbox.network.connection.session.SessionClient
|
2023-09-21 12:54:42 +02:00
|
|
|
import dorkbox.network.connection.session.SessionConnection
|
2023-09-22 15:53:46 +02:00
|
|
|
import dorkbox.network.connection.session.SessionServer
|
2023-09-21 12:54:42 +02:00
|
|
|
import dorkbox.network.rmi.RemoteObject
|
|
|
|
import dorkboxTest.network.rmi.cows.TestCow
|
|
|
|
import dorkboxTest.network.rmi.cows.TestCowImpl
|
|
|
|
import kotlinx.coroutines.GlobalScope
|
|
|
|
import kotlinx.coroutines.delay
|
|
|
|
import kotlinx.coroutines.launch
|
|
|
|
import org.junit.Assert
|
|
|
|
import org.junit.Test
|
|
|
|
|
2023-09-25 13:59:46 +02:00
|
|
|
|
|
|
|
class MessageToContinue
|
|
|
|
|
2023-09-21 12:54:42 +02:00
|
|
|
class SessionReconnectTest: BaseTest() {
|
|
|
|
@Test
|
|
|
|
fun rmiReconnectSessions() {
|
|
|
|
val server = run {
|
|
|
|
val configuration = serverConfig()
|
|
|
|
|
|
|
|
configuration.serialization.rmi.register(TestCow::class.java, TestCowImpl::class.java)
|
2023-09-25 13:59:46 +02:00
|
|
|
configuration.serialization.register(MessageToContinue::class.java)
|
2023-09-21 12:54:42 +02:00
|
|
|
configuration.serialization.register(UnsupportedOperationException::class.java)
|
|
|
|
|
|
|
|
// for Client -> Server RMI
|
|
|
|
configuration.serialization.rmi.register(TestCow::class.java, TestCowImpl::class.java)
|
|
|
|
configuration.enableSessionManagement = true
|
|
|
|
|
|
|
|
val server = SessionServer<SessionConnection>(configuration)
|
|
|
|
|
|
|
|
addEndPoint(server)
|
|
|
|
|
2023-09-25 13:59:46 +02:00
|
|
|
server.onMessage<MessageToContinue> { m ->
|
|
|
|
send(MessageToContinue())
|
2023-09-21 12:54:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
server
|
|
|
|
}
|
|
|
|
|
|
|
|
val client = run {
|
|
|
|
val configuration = clientConfig()
|
|
|
|
|
|
|
|
|
|
|
|
val client = SessionClient<SessionConnection>(configuration)
|
|
|
|
|
|
|
|
addEndPoint(client)
|
|
|
|
|
|
|
|
var rmiId = 0
|
|
|
|
|
|
|
|
client.onConnect {
|
|
|
|
logger.error("Connecting")
|
|
|
|
|
|
|
|
rmi.create<TestCow>(23) {
|
|
|
|
rmiId = it
|
|
|
|
moo("Client -> Server")
|
|
|
|
|
2023-09-25 13:59:46 +02:00
|
|
|
this@onConnect.send(MessageToContinue())
|
2023-09-21 12:54:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-25 13:59:46 +02:00
|
|
|
client.onMessage<MessageToContinue> { _ ->
|
|
|
|
val get = rmi.get<TestCow>(rmiId)
|
|
|
|
RemoteObject.cast(get).responseTimeout = 50_000
|
2023-09-21 12:54:42 +02:00
|
|
|
|
|
|
|
GlobalScope.launch {
|
|
|
|
delay(4000)
|
|
|
|
|
2023-09-25 13:59:46 +02:00
|
|
|
get.moo("DELAYED AND NOT CRASHED!")
|
2023-09-21 12:54:42 +02:00
|
|
|
|
|
|
|
stopEndPoints()
|
|
|
|
}
|
|
|
|
|
|
|
|
client.close(false)
|
|
|
|
client.connectIpc()
|
|
|
|
}
|
|
|
|
|
|
|
|
client
|
|
|
|
}
|
|
|
|
|
|
|
|
server.bindIpc()
|
|
|
|
client.connectIpc()
|
|
|
|
|
|
|
|
waitForThreads()
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun rmiReconnectReplayMessages() {
|
|
|
|
val server = run {
|
|
|
|
val configuration = serverConfig()
|
|
|
|
|
|
|
|
configuration.serialization.rmi.register(TestCow::class.java, TestCowImpl::class.java)
|
2023-09-25 13:59:46 +02:00
|
|
|
configuration.serialization.register(MessageToContinue::class.java)
|
2023-09-21 12:54:42 +02:00
|
|
|
configuration.serialization.register(UnsupportedOperationException::class.java)
|
|
|
|
|
|
|
|
// for Client -> Server RMI
|
|
|
|
configuration.serialization.rmi.register(TestCow::class.java, TestCowImpl::class.java)
|
|
|
|
configuration.enableSessionManagement = true
|
|
|
|
|
|
|
|
val server = SessionServer<SessionConnection>(configuration)
|
|
|
|
|
|
|
|
addEndPoint(server)
|
|
|
|
|
2023-09-25 13:59:46 +02:00
|
|
|
server.onMessage<MessageToContinue> { m ->
|
|
|
|
send(MessageToContinue())
|
2023-09-21 12:54:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
server
|
|
|
|
}
|
|
|
|
|
|
|
|
val client = run {
|
|
|
|
val configuration = clientConfig()
|
|
|
|
|
|
|
|
|
|
|
|
val client = SessionClient<SessionConnection>(configuration)
|
|
|
|
|
|
|
|
addEndPoint(client)
|
|
|
|
|
|
|
|
var rmiId = 0
|
|
|
|
|
|
|
|
client.onConnect {
|
|
|
|
logger.error("Connecting")
|
|
|
|
|
|
|
|
rmi.create<TestCow>(23) {
|
|
|
|
rmiId = it
|
|
|
|
moo("Client -> Server")
|
|
|
|
|
2023-09-25 13:59:46 +02:00
|
|
|
this@onConnect.send(MessageToContinue())
|
2023-09-21 12:54:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-25 13:59:46 +02:00
|
|
|
client.onMessage<MessageToContinue> { _ ->
|
|
|
|
logger.error("Starting reconnect bits")
|
2023-09-21 12:54:42 +02:00
|
|
|
val obj = rmi.get<TestCow>(rmiId)
|
|
|
|
|
2023-09-25 13:59:46 +02:00
|
|
|
// closing client
|
2023-09-21 12:54:42 +02:00
|
|
|
client.close(false)
|
2023-09-25 13:59:46 +02:00
|
|
|
client.waitForClose()
|
2023-09-21 12:54:42 +02:00
|
|
|
|
2023-09-25 13:59:46 +02:00
|
|
|
logger.error("Getting object again. it should be the cached version")
|
|
|
|
val cast = RemoteObject.cast(obj)
|
|
|
|
cast.responseTimeout = 50_000
|
2023-09-21 12:54:42 +02:00
|
|
|
|
|
|
|
GlobalScope.launch {
|
|
|
|
delay(1000) // must be shorter than the RMI timeout for our tests
|
|
|
|
|
|
|
|
client.connectIpc()
|
2023-09-25 13:59:46 +02:00
|
|
|
|
|
|
|
delay(1000) // give some time for the messages to go! If we close instantly, the return RMI message will fail
|
2023-09-21 12:54:42 +02:00
|
|
|
stopEndPoints()
|
|
|
|
}
|
|
|
|
|
|
|
|
// this is SYNC, so it waits for a response!
|
2023-09-25 13:59:46 +02:00
|
|
|
try {
|
|
|
|
cast.async = false
|
|
|
|
obj.moo("DELAYED AND NOT CRASHED!") // will wait for a response
|
|
|
|
}
|
|
|
|
catch (e: Exception) {
|
|
|
|
e.printStackTrace()
|
|
|
|
e.cause?.printStackTrace()
|
|
|
|
Assert.fail(".moo() should not throw an exception, because it will succeed before the timeout")
|
|
|
|
}
|
2023-09-21 12:54:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
client
|
|
|
|
}
|
|
|
|
|
|
|
|
server.bindIpc()
|
|
|
|
client.connectIpc()
|
|
|
|
|
|
|
|
waitForThreads()
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun rmiReconnectSessionsFail() {
|
|
|
|
var rmiId = 0
|
|
|
|
|
|
|
|
val server = run {
|
|
|
|
val configuration = serverConfig()
|
|
|
|
|
|
|
|
configuration.serialization.rmi.register(TestCow::class.java, TestCowImpl::class.java)
|
2023-09-25 13:59:46 +02:00
|
|
|
configuration.serialization.register(MessageToContinue::class.java)
|
2023-09-21 12:54:42 +02:00
|
|
|
configuration.serialization.register(UnsupportedOperationException::class.java)
|
|
|
|
|
|
|
|
// for Client -> Server RMI
|
|
|
|
configuration.serialization.rmi.register(TestCow::class.java, TestCowImpl::class.java)
|
|
|
|
configuration.enableSessionManagement = true
|
|
|
|
|
|
|
|
val server = SessionServer<SessionConnection>(configuration)
|
|
|
|
|
|
|
|
addEndPoint(server)
|
|
|
|
|
2023-09-25 13:59:46 +02:00
|
|
|
server.onMessage<MessageToContinue> { m ->
|
2023-09-21 12:54:42 +02:00
|
|
|
rmi.delete(rmiId)
|
|
|
|
|
2023-09-25 13:59:46 +02:00
|
|
|
// NOTE: if we send an RMI object, it will automatically be saved!
|
|
|
|
send(MessageToContinue())
|
2023-09-21 12:54:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
server
|
|
|
|
}
|
|
|
|
|
|
|
|
val client = run {
|
|
|
|
val configuration = clientConfig()
|
|
|
|
|
|
|
|
val client = SessionClient<SessionConnection>(configuration)
|
|
|
|
|
|
|
|
addEndPoint(client)
|
|
|
|
|
|
|
|
var firstTime = true
|
|
|
|
|
|
|
|
client.onConnect {
|
|
|
|
if (firstTime) {
|
|
|
|
firstTime = false
|
|
|
|
logger.error("Connecting")
|
|
|
|
|
|
|
|
rmi.create<TestCow>(23) {
|
|
|
|
rmiId = it
|
|
|
|
moo("Client -> Server")
|
2023-09-25 13:59:46 +02:00
|
|
|
this@onConnect.send(MessageToContinue())
|
2023-09-21 12:54:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-25 13:59:46 +02:00
|
|
|
client.onMessage<MessageToContinue> { _ ->
|
2023-09-21 12:54:42 +02:00
|
|
|
val obj = rmi.get<TestCow>(rmiId)
|
2023-09-25 13:59:46 +02:00
|
|
|
val o2 = RemoteObject.cast(obj)
|
2023-09-21 12:54:42 +02:00
|
|
|
|
|
|
|
GlobalScope.launch {
|
|
|
|
delay(4000)
|
|
|
|
|
|
|
|
try {
|
|
|
|
o2.sync {
|
|
|
|
obj.moo("DELAYED AND NOT CRASHED!")
|
2023-09-25 13:59:46 +02:00
|
|
|
Assert.fail(".moo() should throw an timeout exception, the backing RMI object doesn't exist!")
|
2023-09-21 12:54:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (ignored: Exception) {
|
|
|
|
}
|
|
|
|
|
|
|
|
stopEndPoints()
|
|
|
|
}
|
|
|
|
|
|
|
|
client.close(false)
|
|
|
|
client.connectIpc()
|
|
|
|
}
|
|
|
|
|
|
|
|
client
|
|
|
|
}
|
|
|
|
|
|
|
|
server.bindIpc()
|
|
|
|
client.connectIpc()
|
|
|
|
|
|
|
|
waitForThreads()
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun rmiReconnectSessionsFail2() {
|
|
|
|
|
|
|
|
var rmiId = 0
|
|
|
|
|
|
|
|
val server = run {
|
|
|
|
val configuration = serverConfig()
|
|
|
|
|
|
|
|
configuration.serialization.rmi.register(TestCow::class.java, TestCowImpl::class.java)
|
2023-09-25 13:59:46 +02:00
|
|
|
configuration.serialization.register(MessageToContinue::class.java)
|
2023-09-21 12:54:42 +02:00
|
|
|
configuration.serialization.register(UnsupportedOperationException::class.java)
|
|
|
|
|
|
|
|
// for Client -> Server RMI
|
|
|
|
configuration.serialization.rmi.register(TestCow::class.java, TestCowImpl::class.java)
|
|
|
|
|
|
|
|
val server = Server<Connection>(configuration)
|
|
|
|
|
|
|
|
addEndPoint(server)
|
|
|
|
|
2023-09-25 13:59:46 +02:00
|
|
|
server.onMessage<MessageToContinue> { m ->
|
2023-09-21 12:54:42 +02:00
|
|
|
rmi.delete(rmiId)
|
|
|
|
|
2023-09-25 13:59:46 +02:00
|
|
|
// NOTE: if we send an RMI object, it will automatically be saved!
|
|
|
|
send(MessageToContinue())
|
2023-09-21 12:54:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
server
|
|
|
|
}
|
|
|
|
|
|
|
|
val client = run {
|
|
|
|
val configuration = clientConfig()
|
|
|
|
|
|
|
|
|
|
|
|
val client = Client<Connection>(configuration)
|
|
|
|
|
|
|
|
addEndPoint(client)
|
|
|
|
|
|
|
|
var firstTime = true
|
|
|
|
|
|
|
|
client.onConnect {
|
|
|
|
if (firstTime) {
|
|
|
|
firstTime = false
|
|
|
|
logger.error("Connecting")
|
|
|
|
|
|
|
|
rmi.create<TestCow>(23) {
|
|
|
|
rmiId = it
|
|
|
|
moo("Client -> Server")
|
|
|
|
|
2023-09-25 13:59:46 +02:00
|
|
|
this@onConnect.send(MessageToContinue())
|
2023-09-21 12:54:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-25 13:59:46 +02:00
|
|
|
client.onMessage<MessageToContinue> { _ ->
|
2023-09-21 12:54:42 +02:00
|
|
|
val obj = rmi.get<TestCow>(rmiId)
|
2023-09-25 13:59:46 +02:00
|
|
|
val o2 = RemoteObject.cast(obj)
|
|
|
|
o2.responseTimeout = 50_000
|
2023-09-21 12:54:42 +02:00
|
|
|
|
|
|
|
GlobalScope.launch {
|
|
|
|
delay(4000)
|
|
|
|
|
|
|
|
try {
|
|
|
|
o2.sync {
|
|
|
|
obj.moo("CRASHED!")
|
2023-09-25 13:59:46 +02:00
|
|
|
Assert.fail(".moo() should throw an timeout exception, the backing RMI object doesn't exist!")
|
2023-09-21 12:54:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (e: Exception) {
|
|
|
|
logger.error("Successfully caught error!")
|
|
|
|
}
|
|
|
|
|
|
|
|
stopEndPoints()
|
|
|
|
}
|
|
|
|
|
|
|
|
client.close(false)
|
|
|
|
client.connectIpc()
|
|
|
|
}
|
|
|
|
|
|
|
|
client
|
|
|
|
}
|
|
|
|
|
|
|
|
server.bindIpc()
|
|
|
|
client.connectIpc()
|
|
|
|
|
|
|
|
waitForThreads()
|
|
|
|
}
|
|
|
|
}
|