2023-03-17 15:00:00 +01: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.
|
|
|
|
*/
|
|
|
|
|
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
|
2023-02-27 11:28:24 +01:00
|
|
|
import mu.KotlinLogging
|
2020-08-25 17:45:08 +02:00
|
|
|
import org.junit.Assert
|
|
|
|
import org.junit.Test
|
2023-06-25 17:25:29 +02:00
|
|
|
import java.util.concurrent.*
|
2020-08-25 17:45:08 +02:00
|
|
|
|
|
|
|
class DisconnectReconnectTest : BaseTest() {
|
2023-06-25 17:25:29 +02:00
|
|
|
private val reconnects = 2
|
2020-08-25 17:45:08 +02:00
|
|
|
|
|
|
|
@Test
|
|
|
|
fun reconnectClient() {
|
2023-06-25 17:25:29 +02:00
|
|
|
val latch = CountDownLatch(reconnects+1)
|
|
|
|
val reconnectCount = atomic(0)
|
|
|
|
|
2020-08-25 17:45:08 +02:00
|
|
|
run {
|
|
|
|
val configuration = serverConfig()
|
|
|
|
|
|
|
|
val server: Server<Connection> = Server(configuration)
|
|
|
|
addEndPoint(server)
|
2023-06-26 19:28:55 +02:00
|
|
|
server.bind(2000)
|
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 {
|
2023-06-25 17:25:29 +02:00
|
|
|
latch.countDown()
|
2021-04-30 22:39:36 +02:00
|
|
|
logger.error("Disconnected!")
|
2020-08-27 02:34:36 +02:00
|
|
|
|
2020-08-25 17:45:08 +02:00
|
|
|
val count = reconnectCount.getAndIncrement()
|
2023-06-25 17:25:29 +02:00
|
|
|
if (count < reconnects) {
|
2021-04-30 22:39:36 +02:00
|
|
|
logger.error("Reconnecting: $count")
|
2023-06-26 19:28:55 +02:00
|
|
|
client.reconnect()
|
2020-08-25 17:45:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-26 19:28:55 +02:00
|
|
|
client.connect(LOCALHOST, 2000)
|
2021-04-30 18:22:38 +02:00
|
|
|
}
|
|
|
|
|
2023-06-25 17:25:29 +02:00
|
|
|
latch.await()
|
|
|
|
stopEndPointsBlocking()
|
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)
|
2023-06-25 17:25:29 +02:00
|
|
|
Assert.assertEquals(reconnects+1, reconnectCount.value)
|
2021-04-30 18:22:38 +02:00
|
|
|
}
|
|
|
|
|
2021-04-30 21:18:57 +02:00
|
|
|
@Test
|
|
|
|
fun reconnectClientViaClientClose() {
|
2023-06-25 17:25:29 +02:00
|
|
|
val latch = CountDownLatch(reconnects+1)
|
|
|
|
val reconnectCount = atomic(0)
|
|
|
|
|
2021-04-30 21:18:57 +02:00
|
|
|
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)
|
2023-06-26 19:28:55 +02:00
|
|
|
server.bind(2000)
|
2021-04-30 21:18:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
run {
|
2023-03-17 15:05:59 +01:00
|
|
|
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....")
|
2023-05-28 18:41:46 +02:00
|
|
|
close()
|
2021-04-30 21:18:57 +02:00
|
|
|
}
|
|
|
|
|
2021-04-30 22:39:36 +02:00
|
|
|
client.onDisconnect {
|
2023-06-25 17:25:29 +02:00
|
|
|
latch.countDown()
|
2021-04-30 22:39:36 +02:00
|
|
|
logger.error("Disconnected!")
|
2021-04-30 21:18:57 +02:00
|
|
|
|
|
|
|
val count = reconnectCount.getAndIncrement()
|
2023-06-25 17:25:29 +02:00
|
|
|
if (count < reconnects) {
|
2023-03-17 15:05:59 +01:00
|
|
|
logger.error("Reconnecting: $count")
|
2023-06-26 19:28:55 +02:00
|
|
|
client.reconnect()
|
2021-04-30 21:18:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-26 19:28:55 +02:00
|
|
|
client.connect(LOCALHOST, 2000)
|
2021-04-30 21:18:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-06-25 17:25:29 +02:00
|
|
|
latch.await()
|
|
|
|
stopEndPointsBlocking()
|
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)
|
2023-06-25 17:25:29 +02:00
|
|
|
Assert.assertEquals(reconnects+1, reconnectCount.value)
|
2021-04-30 21:18:57 +02:00
|
|
|
}
|
|
|
|
|
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) {
|
2023-03-17 15:05:59 +01:00
|
|
|
runBlocking {
|
|
|
|
connection.close()
|
|
|
|
}
|
2021-04-30 18:22:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun reconnectRmiClient() {
|
2023-06-25 17:25:29 +02:00
|
|
|
val latch = CountDownLatch(reconnects+1)
|
|
|
|
val reconnectCount = atomic(0)
|
|
|
|
|
2021-04-30 18:22:38 +02:00
|
|
|
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)
|
2023-06-26 19:28:55 +02:00
|
|
|
server.bind(2000)
|
2021-04-30 18:22:38 +02:00
|
|
|
|
|
|
|
|
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.
|
2023-02-27 11:28:24 +01:00
|
|
|
RemoteObject.cast(closerObject).async = true
|
2022-08-04 03:39:14 +02:00
|
|
|
|
2023-05-28 18:41:46 +02:00
|
|
|
// this just calls connection.close() (on the client)
|
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 {
|
2023-06-25 17:25:29 +02:00
|
|
|
latch.countDown()
|
2021-04-30 22:39:36 +02:00
|
|
|
logger.error("Disconnected!")
|
2021-04-30 18:22:38 +02:00
|
|
|
|
|
|
|
val count = reconnectCount.getAndIncrement()
|
2023-06-25 17:25:29 +02:00
|
|
|
if (count < reconnects) {
|
2021-04-30 22:39:36 +02:00
|
|
|
logger.error("Reconnecting: $count")
|
2023-06-26 19:28:55 +02:00
|
|
|
client.reconnect()
|
2021-04-30 18:22:38 +02:00
|
|
|
}
|
2020-08-25 17:45:08 +02:00
|
|
|
}
|
2021-04-30 18:22:38 +02:00
|
|
|
|
2023-06-26 19:28:55 +02:00
|
|
|
client.connect(LOCALHOST, 2000)
|
2020-08-25 17:45:08 +02:00
|
|
|
}
|
|
|
|
|
2023-06-25 17:25:29 +02:00
|
|
|
latch.await()
|
|
|
|
stopEndPointsBlocking()
|
2023-06-18 19:41:57 +02:00
|
|
|
waitForThreads()
|
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)
|
2023-06-25 17:25:29 +02:00
|
|
|
Assert.assertEquals(reconnects+1, reconnectCount.value)
|
2020-09-10 14:40:09 +02:00
|
|
|
}
|
|
|
|
|
2020-09-15 12:01:29 +02:00
|
|
|
@Test
|
|
|
|
fun manualMediaDriverAndReconnectClient() {
|
2023-06-25 17:25:29 +02:00
|
|
|
val latch = CountDownLatch(reconnects+1)
|
|
|
|
val reconnectCount = atomic(0)
|
|
|
|
|
2023-02-27 11:28:24 +01:00
|
|
|
val log = KotlinLogging.logger("DCUnitTest")
|
2021-04-30 21:18:57 +02:00
|
|
|
// NOTE: once a config is assigned to a driver, the config cannot be changed
|
2023-03-17 15:05:59 +01:00
|
|
|
val aeronDriver = runBlocking {
|
|
|
|
val driver = AeronDriver(serverConfig(), log)
|
|
|
|
driver.start()
|
|
|
|
driver
|
2022-07-16 16:26:12 +02:00
|
|
|
}
|
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)
|
2023-06-26 19:28:55 +02:00
|
|
|
server.bind(2000)
|
2020-09-15 12:01:29 +02:00
|
|
|
|
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 {
|
2023-06-25 17:25:29 +02:00
|
|
|
latch.countDown()
|
2021-04-30 22:39:36 +02:00
|
|
|
logger.error("Disconnected!")
|
2020-09-15 12:01:29 +02:00
|
|
|
|
|
|
|
val count = reconnectCount.getAndIncrement()
|
2023-06-25 17:25:29 +02:00
|
|
|
if (count < reconnects) {
|
2021-04-30 22:39:36 +02:00
|
|
|
logger.error("Reconnecting: $count")
|
2023-06-26 19:28:55 +02:00
|
|
|
client.reconnect()
|
2020-09-15 12:01:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-26 19:28:55 +02:00
|
|
|
client.connect(LOCALHOST, 2000)
|
2020-09-15 12:01:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-06-25 17:25:29 +02:00
|
|
|
latch.await()
|
|
|
|
stopEndPointsBlocking()
|
2020-09-15 12:01:29 +02:00
|
|
|
waitForThreads()
|
2023-06-25 17:25:29 +02:00
|
|
|
|
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)
|
2023-06-25 17:25:29 +02:00
|
|
|
Assert.assertEquals(reconnects+1, reconnectCount.value)
|
2020-09-15 12:01:29 +02:00
|
|
|
}
|
|
|
|
|
2020-09-10 14:40:09 +02:00
|
|
|
@Test
|
|
|
|
fun reconnectWithFallbackClient() {
|
2023-06-25 17:25:29 +02:00
|
|
|
val latch = CountDownLatch(reconnects+1)
|
|
|
|
val reconnectCount = atomic(0)
|
|
|
|
|
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)
|
2023-06-26 19:28:55 +02:00
|
|
|
server.bind(2000)
|
2020-09-10 14:40:09 +02:00
|
|
|
|
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!")
|
2023-06-25 17:25:29 +02:00
|
|
|
latch.countDown()
|
2020-09-10 14:40:09 +02:00
|
|
|
|
|
|
|
val count = reconnectCount.getAndIncrement()
|
2023-06-25 17:25:29 +02:00
|
|
|
if (count < reconnects) {
|
2021-04-30 22:39:36 +02:00
|
|
|
logger.error("Reconnecting: $count")
|
2023-06-26 19:28:55 +02:00
|
|
|
client.reconnect()
|
2020-09-10 14:40:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-26 19:28:55 +02:00
|
|
|
client.connect(LOCALHOST, 2000)
|
2020-09-10 14:40:09 +02:00
|
|
|
}
|
|
|
|
|
2023-06-25 17:25:29 +02:00
|
|
|
latch.await()
|
|
|
|
stopEndPointsBlocking()
|
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)
|
2023-06-25 17:25:29 +02:00
|
|
|
Assert.assertEquals(reconnects+1, reconnectCount.value)
|
2020-08-25 17:45:08 +02:00
|
|
|
}
|
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)
|
2023-06-26 19:28:55 +02:00
|
|
|
server.bind(2000)
|
2020-09-11 03:30:00 +02:00
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2023-06-26 19:28:55 +02:00
|
|
|
client.connect(LOCALHOST, 2000)
|
2020-09-11 03:30:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
server.close()
|
2023-06-25 17:25:29 +02:00
|
|
|
runBlocking {
|
|
|
|
client.waitForClose()
|
|
|
|
server.waitForClose()
|
|
|
|
}
|
2020-09-11 03:30:00 +02:00
|
|
|
|
|
|
|
waitForThreads()
|
|
|
|
}
|
2020-08-25 17:45:08 +02:00
|
|
|
}
|