square/workflow-kotlin

Passing the TestScope.coroutineContext to launchForTestingFromState/Start causes a crash

オープン

#983 opened on 2023/03/31

 (0 件のコメント) (0 件のリアクション) (0 人の担当者)Kotlin (112 件のフォーク)github user discovery
good first issue

Repository metrics

Stars
 (1,120 個のスター)
PR merge metrics
 (平均マージ 55d 8h) (30d で 9 merged PRs)

説明

workflow.launchForTestingFromStateWith(..., context = testContext, ...) fails with

Test finished
java.util.concurrent.CancellationException: Test finished
	at com.squareup.workflow1.testing.WorkflowTestRuntimeKt$launchForTestingWith$2$1.invoke(WorkflowTestRuntime.kt:285)
	at com.squareup.workflow1.testing.WorkflowTestRuntimeKt.unwrapCancellationCause(WorkflowTestRuntime.kt:322)
	at com.squareup.workflow1.testing.WorkflowTestRuntimeKt.access$unwrapCancellationCause(WorkflowTestRuntime.kt:1)
	at com.squareup.workflow1.testing.WorkflowTestRuntimeKt$launchForTestingWith$2.invoke(WorkflowTestRuntime.kt:276)
	at com.squareup.workflow1.internal.util.UncaughtExceptionGuard.runRethrowingUncaught(UncaughtExceptionGuard.kt:32)
	at com.squareup.workflow1.testing.WorkflowTestRuntimeKt.launchForTestingWith(WorkflowTestRuntime.kt:275)
	at com.squareup.workflow1.testing.WorkflowTestRuntimeKt.launchForTestingFromStateWith(WorkflowTestRuntime.kt:210)

, but workflow.launchForTestingFromStateWith(..., context = testContext[CoroutineDispatcher.Key]!!, ...) succeed.

It may be connected with the way uncaughtExceptionHandler is set for workflow test runtime:

  val uncaughtExceptionHandler = CoroutineExceptionHandler { _, throwable ->
    exceptionGuard.reportUncaught(throwable)
  }
....
  val workflowScope = CoroutineScope(Unconfined + context + uncaughtExceptionHandler)
  val outputs = Channel<OutputT>(capacity = UNLIMITED)
  workflowScope.coroutineContext[Job]!!.invokeOnCompletion {
    outputs.close(it)
  }

コントリビューターガイド