Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ description = "Ktor HTTP Client Engine for Smithy services generated by smithy-k
extra["displayName"] = "Smithy :: Kotlin :: HTTP :: Engine :: Ktor"
extra["moduleName"] = "aws.smithy.kotlin.runtime.http.engine.ktor"

val coroutinesVersion: String by project
val ktorVersion: String by project

kotlin {
Expand All @@ -24,6 +25,11 @@ kotlin {
implementation(project(":runtime:logging"))
}
}
jvmTest {
dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-test:$coroutinesVersion")
}
}

all {
languageSettings.optIn("aws.smithy.kotlin.runtime.util.InternalApi")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.channels.SendChannel
import kotlinx.coroutines.job
import kotlinx.coroutines.launch
import kotlinx.coroutines.sync.Mutex
import okhttp3.ConnectionPool
import okhttp3.Protocol
import java.util.concurrent.TimeUnit
Expand Down Expand Up @@ -152,11 +153,11 @@ actual class KtorEngine actual constructor(
* Simple notify mechanism that waits for a signal
*/
internal class Waiter {
private val channel = Channel<Unit>(0)
private val mutex = Mutex(locked = true)

// wait for the signal
suspend fun wait() { channel.receive() }
suspend fun wait() { mutex.lock() }

// give the signal to continue
fun signal() { channel.trySend(Unit).getOrThrow() }
fun signal() { mutex.unlock() }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/

package aws.smithy.kotlin.runtime.http.engine.ktor

import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.test.runBlockingTest
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.time.Duration.Companion.milliseconds
import kotlin.time.ExperimentalTime

@OptIn(ExperimentalCoroutinesApi::class, ExperimentalTime::class)
class WaiterTest {
@Test
fun testSignalWhenWaiting() = runBlockingTest {
val start = currentTime

val waiter = Waiter()
launch {
delay(500.milliseconds)
waiter.signal()
}
waiter.wait()

assertEquals(500, currentTime - start)
}

@Test
fun testSignalWhenNotWaiting() = runBlockingTest {
val start = currentTime

val waiter = Waiter()
launch {
delay(500.milliseconds)
waiter.signal()
}
delay(1000.milliseconds)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we might consider changing these to something smaller, no need to wait a full second in the test I don't think

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is in a runBlockingTest so time has no meaning.


assertEquals(1000, currentTime - start)
}
}