Skip to content

Commit

Permalink
Merge pull request #150 from jillesvangurp/ktor-client-3-and-wasm
Browse files Browse the repository at this point in the history
upgrade to ktor-client 3.0 & adds wasm
  • Loading branch information
jillesvangurp authored Oct 28, 2024
2 parents 44f402b + 2cdf885 commit bb9c0e4
Show file tree
Hide file tree
Showing 20 changed files with 763 additions and 612 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/pullrequest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ jobs:
- name: Compose up
run: ./gradlew :search-client:composeUp
- name: Gradle Build
run: ./gradlew check -x jsTest -x jsBrowserTest -x jsNodeTest
# skipping js/wasm targets because of browser related flakiness and random client failures
run: ./gradlew check -x jsTest -x jsBrowserTest -x jsNodeTest -x wasmJsBrowserTest -x wasmJsNodeTest -x wasmJsD8Test
- name: Compose down
run: ./gradlew :search-client:composeDown
2 changes: 0 additions & 2 deletions docs/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@ dependencies {
testImplementation("io.ktor:ktor-client-content-negotiation:_")


testImplementation("io.github.microutils:kotlin-logging:_")

// bring your own logging, but we need some in tests
testImplementation("org.slf4j:slf4j-api:_")
testImplementation("org.slf4j:jcl-over-slf4j:_")
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.8-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.10.2-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
Expand Down
236 changes: 123 additions & 113 deletions kotlin-js-store/yarn.lock

Large diffs are not rendered by default.

64 changes: 49 additions & 15 deletions search-client/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
@file:OptIn(ExperimentalWasmDsl::class)

import com.avast.gradle.dockercompose.ComposeExtension
import org.gradle.api.tasks.testing.logging.TestExceptionFormat.FULL
import org.gradle.api.tasks.testing.logging.TestLogEvent.*
import org.gradle.nativeplatform.platform.internal.DefaultNativePlatform
import java.net.URI
import java.net.URL
import java.util.*
import java.util.Properties
import org.gradle.api.tasks.testing.logging.TestExceptionFormat.FULL
import org.gradle.api.tasks.testing.logging.TestLogEvent.FAILED
import org.gradle.api.tasks.testing.logging.TestLogEvent.PASSED
import org.gradle.api.tasks.testing.logging.TestLogEvent.SKIPPED
import org.gradle.api.tasks.testing.logging.TestLogEvent.STANDARD_ERROR
import org.gradle.api.tasks.testing.logging.TestLogEvent.STANDARD_OUT
import org.jetbrains.kotlin.gradle.ExperimentalKotlinGradlePluginApi
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
import org.jetbrains.kotlin.gradle.targets.js.dsl.ExperimentalWasmDsl
Expand Down Expand Up @@ -100,14 +102,34 @@ kotlin {
iosArm64()
iosX64()
iosSimulatorArm64()
// Blocked on json-dsl and kotlinx-serialization-extensions support
// iosSimulatorArm64()
// Blocked on ktor-client support
// wasmJs {
// browser()
// nodejs()
// d8()
// }
@OptIn(org.jetbrains.kotlin.gradle.ExperimentalWasmDsl::class)
wasmJs {
browser {
testTask {
useMocha {
// javascript is a lot slower than Java, we hit the default timeout of 2000
timeout = "30s"
}
}
}
nodejs {
testTask {
useMocha {
// javascript is a lot slower than Java, we hit the default timeout of 2000
timeout = "30s"
}
}
}
// errors
// d8 {
// testTask {
// useMocha {
// // javascript is a lot slower than Java, we hit the default timeout of 2000
// timeout = "30s"
// }
// }
// }
}

sourceSets {
commonMain {
Expand All @@ -125,7 +147,8 @@ kotlin {
api(Ktor.client.auth)
api(Ktor.client.logging)
api(Ktor.client.serialization)
api("io.github.microutils:kotlin-logging:_")
implementation("io.github.oshai:kotlin-logging:_")

implementation("io.ktor:ktor-client-logging:_")
implementation("io.ktor:ktor-serialization-kotlinx:_")
implementation("io.ktor:ktor-serialization-kotlinx-json:_")
Expand All @@ -139,7 +162,6 @@ kotlin {
implementation(kotlin("test-annotations-common", "_"))
implementation(Testing.kotest.assertions.core)
implementation(KotlinX.coroutines.test)
api("io.github.microutils:kotlin-logging:_")
}
}
jvmMain {
Expand Down Expand Up @@ -168,6 +190,18 @@ kotlin {
}
}

wasmJsMain {
dependencies {
implementation("io.ktor:ktor-client-js-wasm-js:_")
}
}

wasmJsTest {
dependencies {
implementation(kotlin("test-wasm-js"))
}
}

iosMain {
dependencies {
implementation(Ktor.client.darwin)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import kotlinx.coroutines.*
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.mapNotNull
import kotlinx.serialization.KSerializer
import mu.KotlinLogging
import io.github.oshai.kotlinlogging.KotlinLogging
import kotlin.time.Duration
import kotlin.time.Duration.Companion.milliseconds
import kotlin.time.Duration.Companion.minutes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import kotlinx.serialization.Serializable
import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.jsonObject
import kotlinx.serialization.json.jsonPrimitive
import mu.KotlinLogging
import io.github.oshai.kotlinlogging.KotlinLogging
import kotlin.time.Duration
import kotlin.time.Duration.Companion.minutes
import kotlin.time.Duration.Companion.seconds
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,110 +10,122 @@ class BulkTest : SearchTestBase() {

@Test
fun shouldBulkIndex() = coRun {
val index = testDocumentIndex()
testDocumentIndex { index ->

client.bulk(refresh = Refresh.WaitFor, bulkSize = 4, target = index) {
(1..20).forEach {
create(TestDocument(name = "doc $it").json(false))

client.bulk(refresh = Refresh.WaitFor, bulkSize = 4, target = index) {
(1..20).forEach {
create(TestDocument(name = "doc $it").json(false))
}
}
}
client.search(index) {
client.search(index) {

}.total shouldBe 20
}.total shouldBe 20
}
}

@Test
fun shouldCallback() = coRun {
val index = testDocumentIndex()
var success = 0
var failed = 0
client.bulk(target = index, bulkSize = 4, callBack = object : BulkItemCallBack {
override fun itemFailed(operationType: OperationType, item: BulkResponse.ItemDetails) {
failed++
}

override fun itemOk(operationType: OperationType, item: BulkResponse.ItemDetails) {
success++
testDocumentIndex { index ->

var success = 0
var failed = 0
client.bulk(target = index, bulkSize = 4, callBack = object : BulkItemCallBack {
override fun itemFailed(operationType: OperationType, item: BulkResponse.ItemDetails) {
failed++
}

override fun itemOk(operationType: OperationType, item: BulkResponse.ItemDetails) {
success++
}

override fun bulkRequestFailed(e: Exception, ops: List<Pair<String, String?>>) {
error("${e.message} on")
}
}) {
(1..10).forEach {
index(TestDocument(name = "doc $it").json())
}
}

override fun bulkRequestFailed(e: Exception, ops: List<Pair<String, String?>>) {
error("${e.message} on")
}
}) {
(1..10).forEach {
index(TestDocument(name = "doc $it").json())
}
success shouldBe 10
failed shouldBe 0
}

success shouldBe 10
failed shouldBe 0
}

@Test
fun shouldHandleSourceFieldOnUpdate() = coRun {
val index = testDocumentIndex()
client.bulk(target = index, source="true") {
update(TestDocument("bar"), id="1", docAsUpsert = true)
testDocumentIndex { index ->
client.bulk(target = index, source = "true") {
update(TestDocument("bar"), id = "1", docAsUpsert = true)
}
}
}

@Test
fun shouldHandleScriptUpdate() = coRun {
val index = testDocumentIndex()
client.bulk(target = index, source="true") {
// will get initialized to 0 by the upsert
update(script = Script.create {
source="ctx._source.number += params.param1"
params= mapOf(
"param1" to 10
)
}, id = "1", upsert = TestDocument("counter", number = 0))
// now the script runs
update(script = Script.create {
source="ctx._source.number += params.param1"
params= mapOf(
"param1" to 10
)
}, id = "1", upsert = TestDocument("counter", number = 0))
testDocumentIndex { index ->
client.bulk(target = index, source = "true") {
// will get initialized to 0 by the upsert
update(script = Script.create {
source = "ctx._source.number += params.param1"
params = mapOf(
"param1" to 10
)
}, id = "1", upsert = TestDocument("counter", number = 0))
// now the script runs
update(script = Script.create {
source = "ctx._source.number += params.param1"
params = mapOf(
"param1" to 10
)
}, id = "1", upsert = TestDocument("counter", number = 0))
}
}
}

@Test
fun shouldAcceptSerializedUpdate() = coRun {
val index = testDocumentIndex()
client.bulk(target = index, source="true") {
// will get initialized to 0 by the upsert
testDocumentIndex { index ->
client.bulk(target = index, source = "true") {
// will get initialized to 0 by the upsert

create(TestDocument("original"), id = "42")
// now the script runs
update(TestDocument("changed"), id = "42")
create(TestDocument("original"), id = "42")
// now the script runs
update(TestDocument("changed"), id = "42")
}
val resp = client.getDocument(index, "42").source!!.parse<TestDocument>()
resp.name shouldBe "changed"
}
val resp = client.getDocument(index,"42").source!!.parse<TestDocument>()
resp.name shouldBe "changed"
}

@Test
fun shouldOverrideBulkRoutingForItemsWithRouting() = coRun {
val index = testDocumentIndex()
client.bulk(target = index, source="true", routing = "1") {
create(doc = TestDocument(name = "document with specific routing"), id = "42", routing = "2")
create(doc = TestDocument(name = "document without specific routing"), id = "43")
index(doc = TestDocument(name = "document to delete"), id = "44", routing = "3")

update(id = "42", doc = TestDocument(name = "document with specific routing updated").json(), routing = "2")
testDocumentIndex { index ->
client.bulk(target = index, source = "true", routing = "1") {
create(doc = TestDocument(name = "document with specific routing"), id = "42", routing = "2")
create(doc = TestDocument(name = "document without specific routing"), id = "43")
index(doc = TestDocument(name = "document to delete"), id = "44", routing = "3")

update(
id = "42",
doc = TestDocument(name = "document with specific routing updated").json(),
routing = "2"
)

delete(id = "44", routing = "3")
}
delete(id = "44", routing = "3")
}

val docWithSpecificRouting = client.getDocument(index, "42")
val docWithoutSpecificRouting = client.getDocument(index, "43")
val docWithSpecificRouting = client.getDocument(index, "42")
val docWithoutSpecificRouting = client.getDocument(index, "43")

shouldThrow<RestException> {
client.getDocument(index, "44")
}.message shouldContain "RequestIsWrong 404"
shouldThrow<RestException> {
client.getDocument(index, "44")
}.message shouldContain "RequestIsWrong 404"

docWithSpecificRouting.routing shouldBe "2"
docWithSpecificRouting.source!!.parse<TestDocument>().name shouldBe "document with specific routing updated"
docWithoutSpecificRouting.routing shouldBe "1"
docWithSpecificRouting.routing shouldBe "2"
docWithSpecificRouting.source!!.parse<TestDocument>().name shouldBe "document with specific routing updated"
docWithoutSpecificRouting.routing shouldBe "1"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@ class DeleteByQyeryTest: SearchTestBase() {

@Test
fun shouldDeleteByQuery() = coRun {
val index = testDocumentIndex()
client.indexDocument(index, TestDocument("foo bar").json(false), refresh = Refresh.WaitFor)
client.indexDocument(index, TestDocument("fooo").json(false), refresh = Refresh.WaitFor)
testDocumentIndex { index ->

val resp = client.deleteByQuery(index) {
query = matchAll()
}
client.indexDocument(index, TestDocument("foo bar").json(false), refresh = Refresh.WaitFor)
client.indexDocument(index, TestDocument("fooo").json(false), refresh = Refresh.WaitFor)

val resp = client.deleteByQuery(index) {
query = matchAll()
}

resp.deleted shouldBe 2
resp.deleted shouldBe 2
}
}
}
Loading

0 comments on commit bb9c0e4

Please sign in to comment.