Repository navigation
feat: distinguish a timeout from other failures on shutdown and flush #1663
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
d433e63
6dbe45f
da9eebb
fc225cc
f73fe28
71369be
e31889f
c3c3ff6
a75b8e5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,9 @@ import io.opentelemetry.kotlin.ExperimentalApi | |
|
|
||
| /** | ||
| * Whether an operation was successful or not. | ||
| * | ||
| * `Failure` results may convey what went wrong via a subtype, e.g. [Timeout] when the operation was | ||
| * cut short by a timeout. `is` checks against [Failure] match those subtypes. | ||
| */ | ||
| @ExperimentalApi | ||
| public sealed class OperationResultCode { | ||
|
|
@@ -15,6 +18,22 @@ public sealed class OperationResultCode { | |
|
|
||
| /** | ||
| * Indicates that the operation failed. | ||
| * | ||
| * `Failure` itself stands for a generic failure. Subtypes such as [Timeout] convey the kind | ||
| * of failure. | ||
| */ | ||
| public object Failure : OperationResultCode() | ||
| public open class Failure public constructor() : OperationResultCode() { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated the JVM, Android and klib dumps in fc225cc. They were generated with the Kotlin 2.4.21 ABI tools, which reproduce the existing baselines exactly for the old On binary compatibility: c3c3ff6 keeps |
||
|
|
||
| /** | ||
| * The [Failure] result returned for a generic failure; the same instance every time. | ||
| */ | ||
| public companion object : Failure() { | ||
| override fun toString(): String = "Failure" | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Indicates that the operation failed because its timeout elapsed before it completed. | ||
| */ | ||
| public object Timeout : Failure() | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| package io.opentelemetry.kotlin.export | ||
|
|
||
| import io.opentelemetry.kotlin.ExperimentalApi | ||
| import io.opentelemetry.kotlin.error.FakeSdkErrorHandler | ||
| import kotlinx.coroutines.delay | ||
| import kotlinx.coroutines.test.runTest | ||
| import kotlin.test.Test | ||
| import kotlin.test.assertEquals | ||
| import kotlin.test.assertIs | ||
| import kotlin.test.assertTrue | ||
|
|
||
| @OptIn(ExperimentalApi::class) | ||
| internal class TimeoutOperationTest { | ||
|
|
||
| @Test | ||
| fun testReturnsActionResultWhenItCompletesInTime() = runTest { | ||
| assertEquals(OperationResultCode.Success, runWithTimeout(10) { OperationResultCode.Success }) | ||
| assertEquals(OperationResultCode.Failure, runWithTimeout(10) { OperationResultCode.Failure }) | ||
| } | ||
|
|
||
| @Test | ||
| fun testTimeoutIsReturnedWhenTheTimeoutElapses() = runTest { | ||
| val result = runWithTimeout(10) { | ||
| delay(20) | ||
| OperationResultCode.Success | ||
| } | ||
| assertEquals(OperationResultCode.Timeout, result) | ||
| } | ||
|
|
||
| @Test | ||
| fun testTimeoutIsAFailure() { | ||
| assertIs<OperationResultCode.Failure>(OperationResultCode.Timeout) | ||
| assertTrue(OperationResultCode.Failure != OperationResultCode.Timeout) | ||
|
Copilot marked this conversation as resolved.
Resolved
|
||
| } | ||
|
|
||
| @Test | ||
| fun testShutdownTimeoutIsDistinguishedFromFailure() = runTest { | ||
| val state = MutableShutdownState() | ||
| val result = state.shutdown(10) { | ||
| delay(20) | ||
| OperationResultCode.Success | ||
| } | ||
| assertEquals(OperationResultCode.Timeout, result) | ||
| assertTrue(state.isShutdown) | ||
| } | ||
|
|
||
| @Test | ||
| fun testCompositeFlushReportsTimeoutOverGenericFailure() = runTest { | ||
| val composite = CompositeTelemetryCloseable( | ||
| listOf(FailingCloseable, HangingCloseable), | ||
| FakeSdkErrorHandler(), | ||
| ) | ||
| assertEquals(OperationResultCode.Timeout, composite.forceFlush()) | ||
| } | ||
|
|
||
| private object FailingCloseable : TelemetryCloseable { | ||
| override suspend fun forceFlush(): OperationResultCode = OperationResultCode.Failure | ||
| override suspend fun shutdown(): OperationResultCode = OperationResultCode.Failure | ||
| } | ||
|
|
||
| private object HangingCloseable : TelemetryCloseable { | ||
| override suspend fun forceFlush(): OperationResultCode { | ||
| delay(6000) | ||
| return OperationResultCode.Success | ||
| } | ||
|
|
||
| override suspend fun shutdown(): OperationResultCode { | ||
| delay(6000) | ||
| return OperationResultCode.Success | ||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.