Skip to content

Commit

Permalink
Fix kotlin child workflow execute varargs (#2395)
Browse files Browse the repository at this point in the history
* Fix kotlin child workflow execute varargs

* Fix untyped activity execute kotlin varargs
  • Loading branch information
adamlehenbauer authored Feb 10, 2025
1 parent 76630fe commit 32fbf02
Show file tree
Hide file tree
Showing 4 changed files with 225 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import kotlin.reflect.typeOf
*/
@OptIn(ExperimentalStdlibApi::class)
inline fun <reified T> ActivityStub.execute(activityName: String, vararg args: Any?): T {
return execute(activityName, T::class.java, typeOf<T>().javaType, args)
return execute(activityName, T::class.java, typeOf<T>().javaType, *args)
}

/**
Expand All @@ -49,5 +49,5 @@ inline fun <reified T> ActivityStub.executeAsync(
activityName: String,
vararg args: Any?
): Promise<T> {
return executeAsync(activityName, T::class.java, typeOf<T>().javaType, args)
return executeAsync(activityName, T::class.java, typeOf<T>().javaType, *args)
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ import kotlin.reflect.typeOf
*/
@OptIn(ExperimentalStdlibApi::class)
inline fun <reified T> ChildWorkflowStub.execute(vararg args: Any?): T {
return execute(T::class.java, typeOf<T>().javaType, args)
return execute(T::class.java, typeOf<T>().javaType, *args)
}

/**
* @see ChildWorkflowStub.executeAsync
*/
@OptIn(ExperimentalStdlibApi::class)
inline fun <reified T> ChildWorkflowStub.executeAsync(vararg args: Any?): Promise<T> {
return executeAsync(T::class.java, typeOf<T>().javaType, args)
return executeAsync(T::class.java, typeOf<T>().javaType, *args)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/*
* Copyright (C) 2022 Temporal Technologies, Inc. All Rights Reserved.
*
* Copyright (C) 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Modifications copyright (C) 2017 Uber Technologies, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this material except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.temporal.workflow

import io.temporal.activity.ActivityInterface
import io.temporal.activity.ActivityOptions
import io.temporal.activity.setRetryOptions
import io.temporal.client.WorkflowClientOptions
import io.temporal.client.WorkflowOptions
import io.temporal.common.converter.DefaultDataConverter
import io.temporal.common.converter.JacksonJsonPayloadConverter
import io.temporal.common.converter.KotlinObjectMapperFactory
import io.temporal.testing.internal.SDKTestWorkflowRule
import org.junit.Rule
import org.junit.Test
import java.time.Duration

class KotlinActivityStubExtTest {

@Rule
@JvmField
var testWorkflowRule: SDKTestWorkflowRule = SDKTestWorkflowRule.newBuilder()
.setWorkflowTypes(
SyncWorkflowImpl::class.java,
AsyncWorkflowImpl::class.java
)
.setActivityImplementations(ActivityImpl())
.setWorkflowClientOptions(
WorkflowClientOptions.newBuilder()
.setDataConverter(DefaultDataConverter(JacksonJsonPayloadConverter(KotlinObjectMapperFactory.new())))
.build()
)
.build()

@WorkflowInterface
interface SyncWorkflow {
@WorkflowMethod
fun execute()
}

class SyncWorkflowImpl : SyncWorkflow {

override fun execute() {
val activity = Workflow.newUntypedActivityStub(
ActivityOptions {
setStartToCloseTimeout(Duration.ofSeconds(5))
setRetryOptions { setMaximumAttempts(1) }
}
)

activity.execute<Unit>("Run", "test-argument")
}
}

@WorkflowInterface
interface AsyncWorkflow {
@WorkflowMethod
fun execute()
}

class AsyncWorkflowImpl : AsyncWorkflow {
override fun execute() {
val activity = Workflow.newUntypedActivityStub(
ActivityOptions {
setStartToCloseTimeout(Duration.ofSeconds(5))
setRetryOptions { setMaximumAttempts(1) }
}
)

val promise = activity.executeAsync<Unit>("Run", "test-argument")
promise.get()
}
}

@ActivityInterface
interface TestActivity {
fun run(arg: String)
}

class ActivityImpl : TestActivity {
override fun run(arg: String) {
}
}

@Test
fun `execute on untyped activity stub should spread varargs`() {
val client = testWorkflowRule.workflowClient
val options = WorkflowOptions.newBuilder().setTaskQueue(testWorkflowRule.taskQueue).build()
val workflowStub = client.newWorkflowStub(SyncWorkflow::class.java, options)
workflowStub.execute()
}

@Test
fun `executeAsync on untyped activity stub should spread varargs`() {
val client = testWorkflowRule.workflowClient
val options = WorkflowOptions.newBuilder().setTaskQueue(testWorkflowRule.taskQueue).build()
val workflowStub = client.newWorkflowStub(AsyncWorkflow::class.java, options)
workflowStub.execute()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
* Copyright (C) 2022 Temporal Technologies, Inc. All Rights Reserved.
*
* Copyright (C) 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Modifications copyright (C) 2017 Uber Technologies, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this material except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.temporal.workflow

import io.temporal.client.WorkflowClientOptions
import io.temporal.client.WorkflowOptions
import io.temporal.common.converter.DefaultDataConverter
import io.temporal.common.converter.JacksonJsonPayloadConverter
import io.temporal.common.converter.KotlinObjectMapperFactory
import io.temporal.testing.internal.SDKTestWorkflowRule
import org.junit.Rule
import org.junit.Test

class KotlinChildWorkflowStubExtTest {

@Rule
@JvmField
var testWorkflowRule: SDKTestWorkflowRule = SDKTestWorkflowRule.newBuilder()
.setWorkflowTypes(
ParentWorkflowImpl::class.java,
AsyncParentWorkflowImpl::class.java,
ChildWorkflowImpl::class.java
)
.setWorkflowClientOptions(
WorkflowClientOptions.newBuilder()
.setDataConverter(DefaultDataConverter(JacksonJsonPayloadConverter(KotlinObjectMapperFactory.new())))
.build()
)
.build()

@WorkflowInterface
interface ChildWorkflow {
@WorkflowMethod
fun execute(argument: String): Int
}

class ChildWorkflowImpl : ChildWorkflow {
override fun execute(argument: String): Int {
return 0
}
}

@WorkflowInterface
interface ParentWorkflow {
@WorkflowMethod
fun execute()
}

class ParentWorkflowImpl : ParentWorkflow {
override fun execute() {
val childWorkflow = Workflow.newUntypedChildWorkflowStub("ChildWorkflow")
childWorkflow.execute<Any>("test-argument")
}
}

@WorkflowInterface
interface AsyncParentWorkflow {
@WorkflowMethod
fun execute()
}

class AsyncParentWorkflowImpl : AsyncParentWorkflow {
override fun execute() {
val childWorkflow = Workflow.newUntypedChildWorkflowStub("ChildWorkflow")
val promise = childWorkflow.executeAsync<Any>("test-argument")
promise.get()
}
}

@Test
fun `execute on untyped child workflow should spread varargs`() {
val client = testWorkflowRule.workflowClient
val options = WorkflowOptions.newBuilder().setTaskQueue(testWorkflowRule.taskQueue).build()
val workflowStub = client.newWorkflowStub(ParentWorkflow::class.java, options)
workflowStub.execute()
}

@Test
fun `executeAsync on untyped child workflow should spread varargs`() {
val client = testWorkflowRule.workflowClient
val options = WorkflowOptions.newBuilder().setTaskQueue(testWorkflowRule.taskQueue).build()
val workflowStub = client.newWorkflowStub(AsyncParentWorkflow::class.java, options)
workflowStub.execute()
}
}

0 comments on commit 32fbf02

Please sign in to comment.