2020-08-25 17:45:08 +02:00
|
|
|
package dorkboxTest.network
|
|
|
|
|
|
|
|
import dorkbox.network.Client
|
|
|
|
import dorkbox.network.Server
|
2021-04-27 13:57:47 +02:00
|
|
|
import dorkbox.network.aeron.AeronDriver
|
2020-08-25 17:45:08 +02:00
|
|
|
import dorkbox.network.connection.Connection
|
2022-08-04 03:39:14 +02:00
|
|
|
import dorkbox.network.rmi.RemoteObject
|
2020-08-25 17:45:08 +02:00
|
|
|
import kotlinx.atomicfu.atomic
|
|
|
|
import kotlinx.coroutines.delay
|
2022-07-16 16:26:12 +02:00
|
|
|
import kotlinx.coroutines.runBlocking
|
2020-08-25 17:45:08 +02:00
|
|
|
import org.junit.Assert
|
|
|
|
import org.junit.Test
|
|
|
|
import java.io.IOException
|
|
|
|
|
|
|
|
class DisconnectReconnectTest : BaseTest() {
|
|
|
|
private val reconnectCount = atomic(0)
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun reconnectClient() {
|
|
|
|
run {
|
|
|
|
val configuration = serverConfig()
|
|
|
|
|
|
|
|
val server: Server<Connection> = Server(configuration)
|
|
|
|
addEndPoint(server)
|
2020-09-02 03:18:10 +02:00
|
|
|
server.bind()
|
2020-08-25 17:45:08 +02:00
|
|
|
|
2021-04-30 22:39:36 +02:00
|
|
|
server.onConnect {
|
|
|
|
logger.error("Disconnecting after 2 seconds.")
|
2020-08-25 17:45:08 +02:00
|
|
|
delay(2000)
|
|
|
|
|
2021-04-30 22:39:36 +02:00
|
|
|
logger.error("Disconnecting....")
|
|
|
|
close()
|
2020-08-25 17:45:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
run {
|
|
|
|
val config = clientConfig()
|
|
|
|
|
|
|
|
val client: Client<Connection> = Client(config)
|
|
|
|
addEndPoint(client)
|
|
|
|
|
|
|
|
|
2021-04-30 22:39:36 +02:00
|
|
|
client.onDisconnect {
|
|
|
|
logger.error("Disconnected!")
|
2020-08-27 02:34:36 +02:00
|
|
|
|
2020-08-25 17:45:08 +02:00
|
|
|
val count = reconnectCount.getAndIncrement()
|
|
|
|
if (count == 3) {
|
2021-04-30 22:39:36 +02:00
|
|
|
logger.error("Shutting down")
|
2020-08-25 17:45:08 +02:00
|
|
|
stopEndPoints()
|
|
|
|
}
|
|
|
|
else {
|
2021-04-30 22:39:36 +02:00
|
|
|
logger.error("Reconnecting: $count")
|
2020-08-25 17:45:08 +02:00
|
|
|
try {
|
2022-03-15 12:44:52 +01:00
|
|
|
client.connect(LOCALHOST)
|
2020-08-25 17:45:08 +02:00
|
|
|
} catch (e: IOException) {
|
|
|
|
e.printStackTrace()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-15 12:44:52 +01:00
|
|
|
client.connect(LOCALHOST)
|
2021-04-30 18:22:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-08-02 12:11:36 +02:00
|
|
|
waitForThreads()
|
2021-04-30 18:22:38 +02:00
|
|
|
|
|
|
|
System.err.println("Connection count (after reconnecting) is: " + reconnectCount.value)
|
|
|
|
Assert.assertEquals(4, reconnectCount.value)
|
|
|
|
}
|
|
|
|
|
2021-04-30 21:18:57 +02:00
|
|
|
@Test
|
|
|
|
fun reconnectClientViaClientClose() {
|
|
|
|
run {
|
2022-08-02 12:11:36 +02:00
|
|
|
val configuration = serverConfig {
|
2022-07-27 00:20:34 +02:00
|
|
|
uniqueAeronDirectory = true
|
2021-04-30 21:18:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
val server: Server<Connection> = Server(configuration)
|
|
|
|
addEndPoint(server)
|
|
|
|
server.bind()
|
|
|
|
}
|
|
|
|
|
|
|
|
run {
|
|
|
|
val config = clientConfig() {
|
2022-07-27 00:20:34 +02:00
|
|
|
uniqueAeronDirectory = true
|
2021-04-30 21:18:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
val client: Client<Connection> = Client(config)
|
|
|
|
addEndPoint(client)
|
|
|
|
|
2021-04-30 22:39:36 +02:00
|
|
|
client.onConnect {
|
|
|
|
logger.error("Disconnecting after 2 seconds.")
|
2021-04-30 21:18:57 +02:00
|
|
|
delay(2000)
|
|
|
|
|
2021-04-30 22:39:36 +02:00
|
|
|
logger.error("Disconnecting....")
|
2021-04-30 21:18:57 +02:00
|
|
|
client.close()
|
|
|
|
}
|
|
|
|
|
2021-04-30 22:39:36 +02:00
|
|
|
client.onDisconnect {
|
|
|
|
logger.error("Disconnected!")
|
2021-04-30 21:18:57 +02:00
|
|
|
|
|
|
|
val count = reconnectCount.getAndIncrement()
|
|
|
|
if (count == 3) {
|
2021-04-30 22:39:36 +02:00
|
|
|
logger.error("Shutting down")
|
2021-04-30 21:18:57 +02:00
|
|
|
stopEndPoints()
|
|
|
|
}
|
|
|
|
else {
|
2021-04-30 22:39:36 +02:00
|
|
|
logger.error("Reconnecting: $count")
|
2021-04-30 21:18:57 +02:00
|
|
|
try {
|
2022-03-15 12:44:52 +01:00
|
|
|
client.connect(LOCALHOST)
|
2021-04-30 21:18:57 +02:00
|
|
|
} catch (e: IOException) {
|
|
|
|
e.printStackTrace()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-15 12:44:52 +01:00
|
|
|
client.connect(LOCALHOST)
|
2021-04-30 21:18:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-08-02 12:11:36 +02:00
|
|
|
waitForThreads()
|
2021-04-30 21:18:57 +02:00
|
|
|
|
|
|
|
System.err.println("Connection count (after reconnecting) is: " + reconnectCount.value)
|
|
|
|
Assert.assertEquals(4, reconnectCount.value)
|
|
|
|
}
|
|
|
|
|
2021-04-30 18:22:38 +02:00
|
|
|
interface CloseIface {
|
2022-08-04 03:39:14 +02:00
|
|
|
fun close()
|
2021-04-30 18:22:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class CloseImpl : CloseIface {
|
2022-08-04 03:39:14 +02:00
|
|
|
override fun close() {
|
2021-04-30 18:22:38 +02:00
|
|
|
// the connection specific one is called instead
|
|
|
|
}
|
|
|
|
|
2022-08-04 03:39:14 +02:00
|
|
|
fun close(connection: Connection) {
|
2022-08-02 12:11:36 +02:00
|
|
|
connection.logger.error { "PRE CLOSE MESSAGE!" }
|
2021-04-30 18:22:38 +02:00
|
|
|
connection.close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun reconnectRmiClient() {
|
|
|
|
val CLOSE_ID = 33
|
|
|
|
|
|
|
|
run {
|
|
|
|
val config = serverConfig()
|
2021-07-02 11:40:20 +02:00
|
|
|
config.serialization.rmi.register(CloseIface::class.java)
|
2021-04-30 18:22:38 +02:00
|
|
|
|
|
|
|
val server: Server<Connection> = Server(config)
|
|
|
|
addEndPoint(server)
|
|
|
|
server.bind()
|
|
|
|
|
|
|
|
|
2021-04-30 22:39:36 +02:00
|
|
|
server.onConnect {
|
|
|
|
logger.error("Disconnecting after 2 seconds.")
|
2021-04-30 18:22:38 +02:00
|
|
|
delay(2000)
|
|
|
|
|
2021-04-30 22:39:36 +02:00
|
|
|
logger.error("Disconnecting via RMI ....")
|
2021-07-06 15:38:53 +02:00
|
|
|
|
|
|
|
val closerObject = rmi.get<CloseIface>(CLOSE_ID)
|
2022-08-04 03:39:14 +02:00
|
|
|
|
|
|
|
// the close operation will kill the connection, preventing the response from returning.
|
2022-12-16 00:01:11 +01:00
|
|
|
closerObject as RemoteObject<CloseIface>
|
2022-08-04 03:39:14 +02:00
|
|
|
closerObject.async = true
|
|
|
|
|
2021-04-30 18:22:38 +02:00
|
|
|
closerObject.close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
run {
|
|
|
|
val config = clientConfig()
|
2021-07-02 11:40:20 +02:00
|
|
|
config.serialization.rmi.register(CloseIface::class.java, CloseImpl::class.java)
|
2021-04-30 18:22:38 +02:00
|
|
|
|
|
|
|
val client: Client<Connection> = Client(config)
|
|
|
|
addEndPoint(client)
|
2021-07-02 11:40:20 +02:00
|
|
|
|
2022-08-02 12:11:36 +02:00
|
|
|
client.onInit {
|
2021-07-02 11:40:20 +02:00
|
|
|
rmi.save(CloseImpl(), CLOSE_ID)
|
|
|
|
}
|
2021-04-30 18:22:38 +02:00
|
|
|
|
2021-04-30 22:39:36 +02:00
|
|
|
client.onDisconnect {
|
|
|
|
logger.error("Disconnected!")
|
2021-04-30 18:22:38 +02:00
|
|
|
|
|
|
|
val count = reconnectCount.getAndIncrement()
|
|
|
|
if (count == 3) {
|
2021-04-30 22:39:36 +02:00
|
|
|
logger.error("Shutting down")
|
2021-04-30 18:22:38 +02:00
|
|
|
stopEndPoints()
|
|
|
|
}
|
|
|
|
else {
|
2021-04-30 22:39:36 +02:00
|
|
|
logger.error("Reconnecting: $count")
|
2021-04-30 18:22:38 +02:00
|
|
|
try {
|
2022-03-15 12:44:52 +01:00
|
|
|
client.connect(LOCALHOST)
|
2021-04-30 18:22:38 +02:00
|
|
|
} catch (e: IOException) {
|
|
|
|
e.printStackTrace()
|
|
|
|
}
|
|
|
|
}
|
2020-08-25 17:45:08 +02:00
|
|
|
}
|
2021-04-30 18:22:38 +02:00
|
|
|
|
2022-03-15 12:44:52 +01:00
|
|
|
client.connect(LOCALHOST)
|
2020-08-25 17:45:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-08-02 12:11:36 +02:00
|
|
|
waitForThreads(AUTO_FAIL_TIMEOUT*10)
|
2020-09-10 14:40:09 +02:00
|
|
|
|
2022-07-14 04:54:50 +02:00
|
|
|
//System.err.println("Connection count (after reconnecting) is: " + reconnectCount.value)
|
2020-09-10 14:40:09 +02:00
|
|
|
Assert.assertEquals(4, reconnectCount.value)
|
|
|
|
}
|
|
|
|
|
2020-09-15 12:01:29 +02:00
|
|
|
@Test
|
|
|
|
fun manualMediaDriverAndReconnectClient() {
|
2021-04-30 21:18:57 +02:00
|
|
|
// NOTE: once a config is assigned to a driver, the config cannot be changed
|
|
|
|
val aeronDriver = AeronDriver(serverConfig())
|
2022-07-16 16:26:12 +02:00
|
|
|
runBlocking {
|
|
|
|
aeronDriver.start()
|
|
|
|
}
|
2020-09-15 12:01:29 +02:00
|
|
|
|
|
|
|
run {
|
2021-04-30 21:18:57 +02:00
|
|
|
val serverConfiguration = serverConfig()
|
2020-09-15 12:01:29 +02:00
|
|
|
val server: Server<Connection> = Server(serverConfiguration)
|
|
|
|
addEndPoint(server)
|
|
|
|
server.bind()
|
|
|
|
|
2021-04-30 22:39:36 +02:00
|
|
|
server.onConnect {
|
|
|
|
logger.error("Disconnecting after 2 seconds.")
|
2020-09-15 12:01:29 +02:00
|
|
|
delay(2000)
|
|
|
|
|
2021-04-30 22:39:36 +02:00
|
|
|
logger.error("Disconnecting....")
|
|
|
|
close()
|
2020-09-15 12:01:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
run {
|
|
|
|
val config = clientConfig()
|
|
|
|
|
|
|
|
val client: Client<Connection> = Client(config)
|
|
|
|
addEndPoint(client)
|
|
|
|
|
|
|
|
|
2021-04-30 22:39:36 +02:00
|
|
|
client.onDisconnect {
|
|
|
|
logger.error("Disconnected!")
|
2020-09-15 12:01:29 +02:00
|
|
|
|
|
|
|
val count = reconnectCount.getAndIncrement()
|
|
|
|
if (count == 3) {
|
2021-04-30 22:39:36 +02:00
|
|
|
logger.error("Shutting down")
|
2020-09-15 12:01:29 +02:00
|
|
|
stopEndPoints()
|
|
|
|
}
|
|
|
|
else {
|
2021-04-30 22:39:36 +02:00
|
|
|
logger.error("Reconnecting: $count")
|
2020-09-15 12:01:29 +02:00
|
|
|
try {
|
2022-03-15 12:44:52 +01:00
|
|
|
client.connect(LOCALHOST)
|
2020-09-15 12:01:29 +02:00
|
|
|
} catch (e: IOException) {
|
|
|
|
e.printStackTrace()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-15 12:44:52 +01:00
|
|
|
client.connect(LOCALHOST)
|
2020-09-15 12:01:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
waitForThreads()
|
2022-07-16 16:26:12 +02:00
|
|
|
runBlocking {
|
|
|
|
aeronDriver.close()
|
|
|
|
}
|
2020-09-15 12:01:29 +02:00
|
|
|
|
2022-07-14 04:54:50 +02:00
|
|
|
//System.err.println("Connection count (after reconnecting) is: " + reconnectCount.value)
|
2020-09-15 12:01:29 +02:00
|
|
|
Assert.assertEquals(4, reconnectCount.value)
|
|
|
|
}
|
|
|
|
|
2020-09-10 14:40:09 +02:00
|
|
|
@Test
|
|
|
|
fun reconnectWithFallbackClient() {
|
2021-04-30 18:22:38 +02:00
|
|
|
// this tests IPC with fallback to UDP (because the server has IPC disabled, and the client has it enabled)
|
2020-09-10 14:40:09 +02:00
|
|
|
run {
|
|
|
|
val config = serverConfig()
|
|
|
|
config.enableIpc = false
|
|
|
|
|
|
|
|
val server: Server<Connection> = Server(config)
|
|
|
|
addEndPoint(server)
|
|
|
|
server.bind()
|
|
|
|
|
2021-04-30 22:39:36 +02:00
|
|
|
server.onConnect {
|
|
|
|
logger.error("Disconnecting after 2 seconds.")
|
2020-09-10 14:40:09 +02:00
|
|
|
delay(2000)
|
|
|
|
|
2021-04-30 22:39:36 +02:00
|
|
|
logger.error("Disconnecting....")
|
|
|
|
close()
|
2020-09-10 14:40:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
run {
|
|
|
|
val config = clientConfig()
|
|
|
|
config.enableIpc = true
|
|
|
|
|
|
|
|
val client: Client<Connection> = Client(config)
|
|
|
|
addEndPoint(client)
|
|
|
|
|
|
|
|
|
2021-04-30 22:39:36 +02:00
|
|
|
client.onDisconnect {
|
|
|
|
logger.error("Disconnected!")
|
2020-09-10 14:40:09 +02:00
|
|
|
|
|
|
|
val count = reconnectCount.getAndIncrement()
|
|
|
|
if (count == 3) {
|
2021-04-30 22:39:36 +02:00
|
|
|
logger.error("Shutting down")
|
2020-09-10 14:40:09 +02:00
|
|
|
stopEndPoints()
|
|
|
|
}
|
|
|
|
else {
|
2021-04-30 22:39:36 +02:00
|
|
|
logger.error("Reconnecting: $count")
|
2020-09-10 14:40:09 +02:00
|
|
|
try {
|
2022-03-15 12:44:52 +01:00
|
|
|
client.connect(LOCALHOST)
|
2020-09-10 14:40:09 +02:00
|
|
|
} catch (e: IOException) {
|
|
|
|
e.printStackTrace()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-15 12:44:52 +01:00
|
|
|
client.connect(LOCALHOST)
|
2020-09-10 14:40:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-08-02 12:11:36 +02:00
|
|
|
waitForThreads()
|
2020-08-25 17:45:08 +02:00
|
|
|
|
2022-07-14 04:54:50 +02:00
|
|
|
//System.err.println("Connection count (after reconnecting) is: " + reconnectCount.value)
|
2020-08-25 17:45:08 +02:00
|
|
|
Assert.assertEquals(4, reconnectCount.value)
|
|
|
|
}
|
2020-09-11 03:30:00 +02:00
|
|
|
|
|
|
|
@Test
|
2020-09-15 12:01:29 +02:00
|
|
|
fun disconnectedMediaDriver() {
|
2020-09-11 03:30:00 +02:00
|
|
|
val server: Server<Connection>
|
|
|
|
run {
|
|
|
|
val config = serverConfig()
|
|
|
|
config.enableIpc = false
|
2022-07-27 00:20:34 +02:00
|
|
|
config.uniqueAeronDirectory = true
|
2020-09-11 03:30:00 +02:00
|
|
|
|
|
|
|
server = Server(config)
|
|
|
|
addEndPoint(server)
|
|
|
|
server.bind()
|
|
|
|
|
2021-04-30 22:39:36 +02:00
|
|
|
server.onConnect {
|
|
|
|
logger.error("Connected!")
|
2020-09-11 03:30:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
val client: Client<Connection>
|
|
|
|
run {
|
|
|
|
val config = clientConfig()
|
|
|
|
config.enableIpc = false
|
2022-07-27 00:20:34 +02:00
|
|
|
config.uniqueAeronDirectory = true
|
2020-09-11 03:30:00 +02:00
|
|
|
|
|
|
|
client = Client(config)
|
|
|
|
addEndPoint(client)
|
|
|
|
|
2021-04-30 22:39:36 +02:00
|
|
|
client.onConnect {
|
|
|
|
logger.error("Connected!")
|
2020-09-11 03:30:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
client.onDisconnect {
|
|
|
|
stopEndPoints()
|
|
|
|
}
|
|
|
|
|
2022-03-15 12:44:52 +01:00
|
|
|
client.connect(LOCALHOST)
|
2020-09-11 03:30:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
server.close()
|
|
|
|
|
|
|
|
|
|
|
|
waitForThreads()
|
|
|
|
}
|
2020-08-25 17:45:08 +02:00
|
|
|
}
|