Added unit test for closing endpoint while they haven't fully started

This commit is contained in:
Robinson 2023-09-08 13:19:56 +02:00
parent f9b30012b1
commit 78ae3a38e4
No known key found for this signature in database
GPG Key ID: 8E7DB78588BD6F5C
1 changed files with 89 additions and 0 deletions

View File

@ -0,0 +1,89 @@
/*
* 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.bytes.sha256
import dorkbox.executor.Executor.Companion.log
import dorkbox.network.Client
import dorkbox.network.Server
import dorkbox.network.connection.Connection
import dorkbox.util.Sys
import kotlinx.atomicfu.atomic
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import org.agrona.ExpandableDirectByteBuffer
import org.agrona.concurrent.SigInt
import org.junit.Assert
import org.junit.Test
import java.io.File
import java.security.SecureRandom
class ShutdownWhileInBadStateTest : BaseTest() {
@Test
fun shutdownWhileClientConnecting() {
val isRunning = atomic(true)
val client = run {
val config = clientConfig()
val client: Client<Connection> = Client(config) {
Connection(it)
}
addEndPoint(client)
client
}
GlobalScope.launch(Dispatchers.IO) {
// there might be errors while trying to reconnect. Make sure that we constantly retry.
while (isRunning.value) {
try {
// if the server isn't available, wait forever.
client.connect(LOCALHOST, 2222, connectionTimeoutSec = 0) // bad port
return@launch
} catch (e: Exception) {
e.printStackTrace()
}
}
}
// at wait for the connection process to start
Thread.sleep(4000)
isRunning.lazySet(false)
client.close()
waitForThreads()
}
@Test
fun shutdownServerBeforeBind() {
val server = run {
val configuration = serverConfig()
val server: Server<Connection> = Server(configuration)
addEndPoint(server)
server
}
server.close()
waitForThreads()
}
}