-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert timeout values to milliseconds if less than 10 seconds.
- Loading branch information
1 parent
d8ee5ad
commit 65bee86
Showing
6 changed files
with
107 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
elasticmagic/src/commonMain/kotlin/dev/evo/elasticmagic/util/Helper.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package dev.evo.elasticmagic.util | ||
|
||
import kotlin.time.Duration | ||
|
||
private const val USE_MILLISECONDS_WHILE_SECONDS_LESS_THAN = 10 | ||
|
||
fun Duration.toTimeoutString() = if (inWholeSeconds > USE_MILLISECONDS_WHILE_SECONDS_LESS_THAN) { | ||
"${inWholeSeconds}s" | ||
} else { | ||
"${inWholeMilliseconds}ms" | ||
} |
71 changes: 71 additions & 0 deletions
71
elasticmagic/src/commonTest/kotlin/dev/evo/elasticmagic/SearchQueryTimeoutTests.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package dev.evo.elasticmagic | ||
|
||
import dev.evo.elasticmagic.compile.BaseCompilerTest | ||
import dev.evo.elasticmagic.compile.SearchQueryCompiler | ||
import dev.evo.elasticmagic.doc.Document | ||
import io.kotest.matchers.maps.shouldContainExactly | ||
import io.kotest.matchers.shouldBe | ||
import kotlin.test.Test | ||
import kotlin.time.Duration.Companion.seconds | ||
import kotlin.time.ExperimentalTime | ||
|
||
@OptIn(ExperimentalTime::class) | ||
class SearchQueryTimeoutTests : BaseCompilerTest<SearchQueryCompiler>(::SearchQueryCompiler) { | ||
@Test | ||
fun timeoutInSearchQuery() = testWithCompiler { | ||
val userDoc = object : Document() { | ||
val login by keyword() | ||
val isActive by boolean() | ||
} | ||
|
||
val sq1 = SearchQuery() | ||
.filter(userDoc.login.eq("root")) | ||
.setTimeout(4.seconds) | ||
|
||
sq1.prepareSearch().let { | ||
it.size shouldBe null | ||
it.filters.size shouldBe 1 | ||
it.timeout shouldBe 4.seconds | ||
} | ||
|
||
compile(sq1).body shouldBe mapOf( | ||
"query" to mapOf( | ||
"bool" to mapOf( | ||
"filter" to listOf( | ||
mapOf("term" to mapOf("login" to "root")) | ||
) | ||
) | ||
), | ||
"timeout" to "4000ms" | ||
) | ||
} | ||
|
||
@Test | ||
fun timeoutInParams() = testWithCompiler { | ||
val userDoc = object : Document() { | ||
val login by keyword() | ||
val isActive by boolean() | ||
} | ||
|
||
val sq1 = SearchQuery(params = Params("timeout" to 4.seconds)) | ||
.filter(userDoc.login.eq("root")) | ||
|
||
|
||
sq1.prepareSearch().let { | ||
it.size shouldBe null | ||
it.filters.size shouldBe 1 | ||
it.timeout shouldBe null | ||
} | ||
val compiled = compile(sq1) | ||
compiled.body shouldBe mapOf( | ||
"query" to mapOf( | ||
"bool" to mapOf( | ||
"filter" to listOf( | ||
mapOf("term" to mapOf("login" to "root")) | ||
) | ||
) | ||
) | ||
) | ||
compiled.params shouldContainExactly mapOf("timeout" to listOf("4000ms")) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters