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
|
|
|
|
import kotlinx.atomicfu.atomic
|
|
|
|
import kotlinx.coroutines.delay
|
|
|
|
import kotlinx.coroutines.runBlocking
|
|
|
|
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
|
|
|
|
|
|
|
server.onConnect { connection ->
|
2020-08-27 02:34:36 +02:00
|
|
|
connection.logger.error("Disconnecting after 2 seconds.")
|
2020-08-25 17:45:08 +02:00
|
|
|
delay(2000)
|
|
|
|
|
2020-08-27 02:34:36 +02:00
|
|
|
connection.logger.error("Disconnecting....")
|
2020-08-25 17:45:08 +02:00
|
|
|
connection.close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
run {
|
|
|
|
val config = clientConfig()
|
|
|
|
|
|
|
|
val client: Client<Connection> = Client(config)
|
|
|
|
addEndPoint(client)
|
|
|
|
|
|
|
|
|
|
|
|
client.onDisconnect { connection ->
|
2020-08-27 02:34:36 +02:00
|
|
|
connection.logger.error("Disconnected!")
|
|
|
|
|
2020-08-25 17:45:08 +02:00
|
|
|
val count = reconnectCount.getAndIncrement()
|
|
|
|
if (count == 3) {
|
2020-08-27 02:34:36 +02:00
|
|
|
connection.logger.error("Shutting down")
|
2020-08-25 17:45:08 +02:00
|
|
|
stopEndPoints()
|
|
|
|
}
|
|
|
|
else {
|
2020-08-27 02:34:36 +02:00
|
|
|
connection.logger.error("Reconnecting: $count")
|
2020-08-25 17:45:08 +02:00
|
|
|
try {
|
|
|
|
client.connect(LOOPBACK)
|
|
|
|
} catch (e: IOException) {
|
|
|
|
e.printStackTrace()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
runBlocking {
|
|
|
|
client.connect(LOOPBACK)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-09-10 14:40:09 +02:00
|
|
|
waitForThreads()
|
|
|
|
|
|
|
|
System.err.println("Connection count (after reconnecting) is: " + reconnectCount.value)
|
|
|
|
Assert.assertEquals(4, reconnectCount.value)
|
|
|
|
}
|
|
|
|
|
2020-09-15 12:01:29 +02:00
|
|
|
@Test
|
|
|
|
fun manualMediaDriverAndReconnectClient() {
|
|
|
|
val serverConfiguration = serverConfig()
|
2021-04-27 13:57:47 +02:00
|
|
|
val aeronDriver = runBlocking {
|
|
|
|
AeronDriver.validateConfig(serverConfiguration)
|
|
|
|
AeronDriver(serverConfiguration)
|
2020-09-15 12:01:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
run {
|
|
|
|
val server: Server<Connection> = Server(serverConfiguration)
|
|
|
|
addEndPoint(server)
|
|
|
|
server.bind()
|
|
|
|
|
|
|
|
server.onConnect { connection ->
|
|
|
|
connection.logger.error("Disconnecting after 2 seconds.")
|
|
|
|
delay(2000)
|
|
|
|
|
|
|
|
connection.logger.error("Disconnecting....")
|
|
|
|
connection.close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
run {
|
|
|
|
val config = clientConfig()
|
|
|
|
|
|
|
|
val client: Client<Connection> = Client(config)
|
|
|
|
addEndPoint(client)
|
|
|
|
|
|
|
|
|
|
|
|
client.onDisconnect { connection ->
|
|
|
|
connection.logger.error("Disconnected!")
|
|
|
|
|
|
|
|
val count = reconnectCount.getAndIncrement()
|
|
|
|
if (count == 3) {
|
|
|
|
connection.logger.error("Shutting down")
|
|
|
|
stopEndPoints()
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
connection.logger.error("Reconnecting: $count")
|
|
|
|
try {
|
|
|
|
client.connect(LOOPBACK)
|
|
|
|
} catch (e: IOException) {
|
|
|
|
e.printStackTrace()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
runBlocking {
|
|
|
|
client.connect(LOOPBACK)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
waitForThreads()
|
|
|
|
|
2020-09-19 19:37:04 +02:00
|
|
|
runBlocking {
|
2021-04-27 13:57:47 +02:00
|
|
|
aeronDriver.close()
|
2020-09-19 19:37:04 +02:00
|
|
|
}
|
2020-09-15 12:01:29 +02:00
|
|
|
|
|
|
|
System.err.println("Connection count (after reconnecting) is: " + reconnectCount.value)
|
|
|
|
Assert.assertEquals(4, reconnectCount.value)
|
|
|
|
}
|
|
|
|
|
2020-09-10 14:40:09 +02:00
|
|
|
@Test
|
|
|
|
fun reconnectWithFallbackClient() {
|
|
|
|
run {
|
|
|
|
val config = serverConfig()
|
|
|
|
config.enableIpc = false
|
|
|
|
|
|
|
|
val server: Server<Connection> = Server(config)
|
|
|
|
addEndPoint(server)
|
|
|
|
server.bind()
|
|
|
|
|
|
|
|
server.onConnect { connection ->
|
|
|
|
connection.logger.error("Disconnecting after 2 seconds.")
|
|
|
|
delay(2000)
|
|
|
|
|
|
|
|
connection.logger.error("Disconnecting....")
|
|
|
|
connection.close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
run {
|
|
|
|
val config = clientConfig()
|
|
|
|
config.enableIpc = true
|
|
|
|
config.enableIpcForLoopback = true
|
|
|
|
|
|
|
|
val client: Client<Connection> = Client(config)
|
|
|
|
addEndPoint(client)
|
|
|
|
|
|
|
|
|
|
|
|
client.onDisconnect { connection ->
|
|
|
|
connection.logger.error("Disconnected!")
|
|
|
|
|
|
|
|
val count = reconnectCount.getAndIncrement()
|
|
|
|
if (count == 3) {
|
|
|
|
connection.logger.error("Shutting down")
|
|
|
|
stopEndPoints()
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
connection.logger.error("Reconnecting: $count")
|
|
|
|
try {
|
|
|
|
client.connect(LOOPBACK)
|
|
|
|
} catch (e: IOException) {
|
|
|
|
e.printStackTrace()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
runBlocking {
|
|
|
|
client.connect(LOOPBACK)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-08-25 17:45:08 +02:00
|
|
|
waitForThreads()
|
|
|
|
|
|
|
|
System.err.println("Connection count (after reconnecting) is: " + reconnectCount.value)
|
|
|
|
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
|
|
|
|
config.aeronDirectoryForceUnique = true
|
|
|
|
|
|
|
|
server = Server(config)
|
|
|
|
addEndPoint(server)
|
|
|
|
server.bind()
|
|
|
|
|
|
|
|
server.onConnect { connection ->
|
|
|
|
connection.logger.error("Connected!")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
val client: Client<Connection>
|
|
|
|
run {
|
|
|
|
val config = clientConfig()
|
|
|
|
config.enableIpc = false
|
|
|
|
config.aeronDirectoryForceUnique = true
|
|
|
|
|
|
|
|
client = Client(config)
|
|
|
|
addEndPoint(client)
|
|
|
|
|
|
|
|
client.onConnect { connection ->
|
|
|
|
connection.logger.error("Connected!")
|
|
|
|
}
|
|
|
|
|
|
|
|
client.onDisconnect {
|
|
|
|
stopEndPoints()
|
|
|
|
}
|
|
|
|
|
|
|
|
runBlocking {
|
|
|
|
client.connect(LOOPBACK)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
server.close()
|
|
|
|
|
|
|
|
|
|
|
|
waitForThreads()
|
|
|
|
}
|
2020-08-25 17:45:08 +02:00
|
|
|
}
|