Valider les resource entrente #91
@@ -374,6 +374,12 @@ tasks.register("testWorkgroup", Test::class) {
|
|||||||
includeTags("workgroup")
|
includeTags("workgroup")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
tasks.register("testViews", Test::class) {
|
||||||
|
group = "tests"
|
||||||
|
useJUnitPlatform {
|
||||||
|
includeTags("view")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
dependencyCheck {
|
dependencyCheck {
|
||||||
formats = listOf(ReportGenerator.Format.HTML, ReportGenerator.Format.XML)
|
formats = listOf(ReportGenerator.Format.HTML, ReportGenerator.Format.XML)
|
||||||
|
|||||||
41
src/main/kotlin/fr/dcproject/common/utils/retry.kt
Normal file
41
src/main/kotlin/fr/dcproject/common/utils/retry.kt
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
package fr.dcproject.common.utils
|
||||||
|
|
||||||
|
import org.slf4j.Logger
|
||||||
|
import org.slf4j.LoggerFactory
|
||||||
|
import kotlin.time.Duration
|
||||||
|
import kotlin.time.ExperimentalTime
|
||||||
|
|
||||||
|
@ExperimentalTime
|
||||||
|
fun <T> retry(numOfRetries: Int, duration: Duration = Duration.ZERO, block: (RetryContext) -> T): T {
|
||||||
|
val logger: Logger = LoggerFactory.getLogger("fr.dcproject.utils.retry")
|
||||||
|
var throwable: Throwable? = null
|
||||||
|
for (attempt in 1..numOfRetries) {
|
||||||
|
val context = RetryContext()
|
||||||
|
try {
|
||||||
|
val output = block(context)
|
||||||
|
if (context.hasStop()) {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
return output
|
||||||
|
} catch (e: Throwable) {
|
||||||
|
throwable = e
|
||||||
|
logger.debug("Failed attempt $attempt / $numOfRetries. Wait ${duration.inSeconds} seconds")
|
||||||
|
Thread.sleep(duration.inMilliseconds.toLong())
|
||||||
|
} finally {
|
||||||
|
if (context.hasStop()) {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
throw throwable!!
|
||||||
|
}
|
||||||
|
|
||||||
|
class RetryContext() {
|
||||||
|
var stoped = false
|
||||||
|
|
||||||
|
fun stop() {
|
||||||
|
stoped = true
|
||||||
|
}
|
||||||
|
|
||||||
|
fun hasStop(): Boolean = stoped
|
||||||
|
}
|
||||||
@@ -2,6 +2,7 @@ package functional
|
|||||||
|
|
||||||
import fr.dcproject.application.Env.TEST
|
import fr.dcproject.application.Env.TEST
|
||||||
import fr.dcproject.application.module
|
import fr.dcproject.application.module
|
||||||
|
import fr.dcproject.common.utils.retry
|
||||||
import fr.dcproject.component.article.database.ArticleForView
|
import fr.dcproject.component.article.database.ArticleForView
|
||||||
import fr.dcproject.component.article.database.ArticleViewRepository
|
import fr.dcproject.component.article.database.ArticleViewRepository
|
||||||
import fr.dcproject.component.auth.database.UserCreator
|
import fr.dcproject.component.auth.database.UserCreator
|
||||||
@@ -20,6 +21,8 @@ import org.junit.jupiter.api.TestInstance
|
|||||||
import org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS
|
import org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS
|
||||||
import org.koin.ktor.ext.get
|
import org.koin.ktor.ext.get
|
||||||
import java.util.UUID
|
import java.util.UUID
|
||||||
|
import kotlin.time.ExperimentalTime
|
||||||
|
import kotlin.time.seconds
|
||||||
|
|
||||||
@KtorExperimentalLocationsAPI
|
@KtorExperimentalLocationsAPI
|
||||||
@KtorExperimentalAPI
|
@KtorExperimentalAPI
|
||||||
@@ -27,6 +30,7 @@ import java.util.UUID
|
|||||||
@TestInstance(PER_CLASS)
|
@TestInstance(PER_CLASS)
|
||||||
@Tags(Tag("functional"), Tag("view"))
|
@Tags(Tag("functional"), Tag("view"))
|
||||||
class ViewTest {
|
class ViewTest {
|
||||||
|
@ExperimentalTime
|
||||||
@Test
|
@Test
|
||||||
fun `test View Article`() {
|
fun `test View Article`() {
|
||||||
val article = ArticleForView(
|
val article = ArticleForView(
|
||||||
@@ -75,15 +79,15 @@ class ViewTest {
|
|||||||
article
|
article
|
||||||
)
|
)
|
||||||
|
|
||||||
/* Sleep because ES is not sync ! */
|
/* Retry because ES is not sync ! */
|
||||||
Thread.sleep(1000)
|
retry(10, 0.3.seconds) {
|
||||||
|
/* Get view */
|
||||||
|
val afterView = viewRepository.getViewsCount(article)
|
||||||
|
|
||||||
/* Get view */
|
/* Check if view has increment */
|
||||||
val afterView = viewRepository.getViewsCount(article)
|
afterView.total `should be equal to` startView.total + 4
|
||||||
|
afterView.unique `should be equal to` startView.unique + 3
|
||||||
/* Check if view has increment */
|
}
|
||||||
afterView.total `should be equal to` startView.total + 4
|
|
||||||
afterView.unique `should be equal to` startView.unique + 3
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user