Make sure now that errors during unit tests are properly failing (or ignoring) as appropriate the test.

master
Robinson 2023-11-22 20:38:46 +01:00
parent 76f42c900c
commit 2cfc2e41e6
No known key found for this signature in database
GPG Key ID: 8E7DB78588BD6F5C
3 changed files with 49 additions and 10 deletions

View File

@ -168,6 +168,8 @@ abstract class BaseTest {
private val endPointConnections: MutableList<EndPoint<*>> = CopyOnWriteArrayList()
private var errors = mutableListOf<Throwable>()
@Volatile
private var isStopping = false
@ -195,11 +197,14 @@ abstract class BaseTest {
endPoint.onConnect { logger.error("UNIT TEST: connect $id (${uuid.toHexString()})") }
endPoint.onDisconnect { logger.error("UNIT TEST: disconnect $id (${uuid.toHexString()})") }
endPoint.onError { logger.error("UNIT TEST: ERROR! $id (${uuid.toHexString()})", it) }
endPoint.onError {
logger.error("UNIT TEST: ERROR! $id (${uuid.toHexString()})", it)
Assert.fail("Exception caught, and it shouldn't have happened!")
errors.add(it)
}
endPoint.onErrorGlobal {
logger.error("UNIT TEST: GLOBAL ERROR!", it)
errors.add(it)
}
endPointConnections.add(endPoint)
@ -272,7 +277,7 @@ abstract class BaseTest {
*
* @param stopAfterSeconds how many seconds to wait, the default is 2 minutes.
*/
fun waitForThreads(stopAfterSeconds: Long = AUTO_FAIL_TIMEOUT) {
fun waitForThreads(stopAfterSeconds: Long = AUTO_FAIL_TIMEOUT, onShutdown: (List<Throwable>) -> List<Throwable> = { it }) {
val clients = endPointConnections.filterIsInstance<Client<Connection>>()
val servers = endPointConnections.filterIsInstance<Server<Connection>>()
@ -330,6 +335,18 @@ abstract class BaseTest {
endPointConnections.clear()
logger.error("Finished shutting down all endpoints... ($successClients, $successServers)")
if (errors.isNotEmpty()) {
val acceptableErrors = errors.filterNot { it.message?.contains("Unable to send message. (Connection in non-connected state, aborted attempt! Not connected)") ?: false }
val filteredErrors = onShutdown(acceptableErrors)
if (filteredErrors.isNotEmpty()) {
filteredErrors.forEach {
it.printStackTrace()
}
Assert.fail("Exception caught, and it shouldn't have happened!")
}
}
}
@Before

View File

@ -271,7 +271,11 @@ class ConnectionFilterTest : BaseTest() {
client.connect(LOCALHOST, 2000)
} catch (e: Exception) {
stopEndPoints()
waitForThreads()
waitForThreads { errors ->
errors.filterNot {
it.message?.contains("Connection was not permitted!") ?: false
}
}
throw e
}
@ -370,7 +374,11 @@ class ConnectionFilterTest : BaseTest() {
client.connect(LOCALHOST, 2000)
} catch (e: Exception) {
stopEndPoints()
waitForThreads()
waitForThreads { errors ->
errors.filterNot {
it.message?.contains("Connection was not permitted!") ?: false
}
}
throw e
}
@ -518,7 +526,11 @@ class ConnectionFilterTest : BaseTest() {
client.connect(LOCALHOST, 2000)
} catch (e: Exception) {
stopEndPoints()
waitForThreads()
waitForThreads{ errors ->
errors.filterNot {
it.message?.contains("Connection was not permitted!") ?: false
}
}
throw e
}
@ -560,7 +572,12 @@ class ConnectionFilterTest : BaseTest() {
client.connect(LOCALHOST, 2000)
} catch (e: Exception) {
stopEndPoints()
waitForThreads()
waitForThreads{ errors ->
errors.filterNot {
it.message?.contains("Connection was not permitted!") ?: false
}
}
throw e
}

View File

@ -27,6 +27,8 @@ class ErrorLoggerTest : BaseTest() {
@Test
fun customErrorLoggerTest() {
val exception = Exception("server ERROR. SHOULD BE CAUGHT")
val server = run {
val configuration = serverConfig()
configuration.aeronErrorFilter = {
@ -50,7 +52,7 @@ class ErrorLoggerTest : BaseTest() {
}
server.onMessage<Any> {
throw Exception("server ERROR. SHOULD BE CAUGHT")
throw exception
}
server
@ -79,6 +81,9 @@ class ErrorLoggerTest : BaseTest() {
server.bind(2000)
client.connect(LOCALHOST, 2000)
waitForThreads()
waitForThreads() { errors ->
// we don't want to fail the unit test for this exception
errors.filter { it != exception }
}
}
}