Refact mock ktor (#127)

* refact: exception colors

* refact: [MOCK] ktor support

* added missing file

---------

Co-authored-by: Florent Champigny <florent@bere.al>
This commit is contained in:
Florent CHAMPIGNY 2025-08-21 11:58:52 +02:00 committed by GitHub
parent 447b928cda
commit 9ca6409acd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
29 changed files with 1506 additions and 355 deletions

View file

@ -1,5 +1,7 @@
package io.github.openflocon.flocon.plugins.network.model
import kotlin.random.Random
data class BadQualityConfig(
val latency: LatencyConfig,
val errorProbability: Double, // chance of triggering an error
@ -9,7 +11,17 @@ data class BadQualityConfig(
val latencyTriggerProbability: Float,
val minLatencyMs: Long,
val maxLatencyMs: Long,
)
) {
fun shouldSimulateLatency(): Boolean {
return latencyTriggerProbability > 0f && (latencyTriggerProbability == 1f || Math.random() < latencyTriggerProbability)
}
fun getRandomLatency() : Long {
return Random.nextLong(
minLatencyMs,
maxLatencyMs + 1
)
}
}
class Error(
val weight: Float, // increase the probability of being triggered vs all others errors
val type: Type,
@ -22,7 +34,39 @@ data class BadQualityConfig(
) : Type
data class ErrorThrow(
val classPath: String,
) : Type
) : Type {
fun generate() : Throwable? {
val errorClass = Class.forName(classPath)
return errorClass.newInstance() as? Throwable
}
}
}
}
}
fun shouldFail(): Boolean {
return errorProbability > 0 && Math.random() < errorProbability
}
fun selectRandomError(): BadQualityConfig.Error? {
if (errors.isEmpty()) {
return null
}
// Calculer la somme totale des poids
val totalWeight = errors.sumOf { it.weight.toDouble() }
// Générer un nombre aléatoire entre 0 et la somme totale des poids
var randomNumber = Random.nextDouble(0.0, totalWeight)
// Parcourir la liste pour trouver l'erreur sélectionnée
for (error in errors) {
randomNumber -= error.weight.toDouble()
if (randomNumber <= 0) {
return error
}
}
// Cas de secours (ne devrait pas arriver si les poids sont positifs)
return errors.first()
}
}

View file

@ -10,7 +10,16 @@ data class MockNetworkResponse(
val urlPattern: String, // a regex
val pattern: Pattern,
val method: String, // can be get, post, put, ... or a wildcard *
)
) {
fun matches(url: String, method: String): Boolean {
val urlMatches = pattern.matcher(url).matches()
val methodMatches = this.method == "*" || this.method.equals(
method,
ignoreCase = true
)
return urlMatches && methodMatches
}
}
data class Response(
val httpCode: Int,