-
Notifications
You must be signed in to change notification settings - Fork 152
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add temporal-kotlin providing correct support for Kotlin in Async
- Loading branch information
1 parent
d38065d
commit cc8f368
Showing
10 changed files
with
216 additions
and
142 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
temporal-kotlin/src/main/kotlin/io/temporal/common/converter/KotlinObjectMapperFactory.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* Copyright (C) 2020 Temporal Technologies, Inc. All Rights Reserved. | ||
* | ||
* Copyright 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 file except in compliance with the License. A copy of the License is | ||
* located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file 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.common.converter | ||
|
||
import com.fasterxml.jackson.annotation.JsonAutoDetect | ||
import com.fasterxml.jackson.annotation.PropertyAccessor | ||
import com.fasterxml.jackson.databind.ObjectMapper | ||
import com.fasterxml.jackson.databind.SerializationFeature | ||
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule | ||
import com.fasterxml.jackson.module.kotlin.KotlinModule | ||
|
||
class KotlinObjectMapperFactory { | ||
companion object { | ||
@JvmStatic | ||
fun new(): ObjectMapper { | ||
val mapper = ObjectMapper() | ||
mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false) | ||
mapper.registerModule(JavaTimeModule()) | ||
mapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY) | ||
mapper.registerModule(KotlinModule()) | ||
return mapper | ||
} | ||
} | ||
} |
118 changes: 0 additions & 118 deletions
118
temporal-kotlin/src/test/kotlin/io/temporal/workflow/KotlinAsyncChildTest.kt
This file was deleted.
Oops, something went wrong.
86 changes: 86 additions & 0 deletions
86
temporal-kotlin/src/test/kotlin/io/temporal/workflow/KotlinAsyncChildWorkflowTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
* Copyright (C) 2020 Temporal Technologies, Inc. All Rights Reserved. | ||
* | ||
* Copyright 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 file except in compliance with the License. A copy of the License is | ||
* located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file 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.KotlinObjectMapperFactory | ||
import io.temporal.common.converter.JacksonJsonPayloadConverter | ||
import io.temporal.internal.async.FunctionWrappingUtil | ||
import io.temporal.internal.sync.AsyncInternal | ||
import io.temporal.testing.TestWorkflowRule | ||
import org.junit.Assert.assertTrue | ||
import org.junit.Rule | ||
import org.junit.Test | ||
|
||
class KotlinAsyncChildWorkflowTest { | ||
|
||
@Rule @JvmField | ||
var testWorkflowRule: TestWorkflowRule = TestWorkflowRule.newBuilder() | ||
.setWorkflowTypes(NaiveParentWorkflowImpl::class.java, ChildWorkflowImpl::class.java) | ||
.setWorkflowClientOptions( | ||
WorkflowClientOptions.newBuilder() | ||
.setDataConverter(DefaultDataConverter(JacksonJsonPayloadConverter(KotlinObjectMapperFactory.new()))) | ||
.build() | ||
) | ||
.build() | ||
|
||
@WorkflowInterface | ||
interface ChildWorkflow { | ||
@WorkflowMethod | ||
fun execute(): Int | ||
} | ||
|
||
class ChildWorkflowImpl : ChildWorkflow { | ||
override fun execute(): Int { | ||
return 0 | ||
} | ||
} | ||
|
||
@WorkflowInterface | ||
interface NaiveParentWorkflow { | ||
@WorkflowMethod | ||
fun execute() | ||
} | ||
|
||
class NaiveParentWorkflowImpl : NaiveParentWorkflow { | ||
override fun execute() { | ||
val childWorkflow = Workflow.newChildWorkflowStub(ChildWorkflow::class.java) | ||
assertTrue( | ||
"This has to be true to make Async.function(childWorkflow::execute) work correctly as expected", | ||
AsyncInternal.isAsync(childWorkflow::execute) | ||
) | ||
assertTrue( | ||
"This has to be true to make Async.function(childWorkflow::execute) work correctly as expected", | ||
AsyncInternal.isAsync(FunctionWrappingUtil.temporalJavaFunctionalWrapper(childWorkflow::execute)) | ||
) | ||
Async.function(childWorkflow::execute).get() | ||
} | ||
} | ||
|
||
@Test | ||
fun asyncChildWorkflowTest() { | ||
val client = testWorkflowRule.workflowClient | ||
val options = WorkflowOptions.newBuilder().setTaskQueue(testWorkflowRule.taskQueue).build() | ||
val workflowStub = client.newWorkflowStub(NaiveParentWorkflow::class.java, options) | ||
workflowStub.execute() | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
temporal-kotlin/src/test/kotlin/io/temporal/workflow/KotlinAsyncLambdaTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
* Copyright (C) 2020 Temporal Technologies, Inc. All Rights Reserved. | ||
* | ||
* Copyright 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 file except in compliance with the License. A copy of the License is | ||
* located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file 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.KotlinObjectMapperFactory | ||
import io.temporal.common.converter.JacksonJsonPayloadConverter | ||
import io.temporal.testing.TestWorkflowRule | ||
import junit.framework.Assert.assertTrue | ||
import org.junit.Rule | ||
import org.junit.Test | ||
import java.util.concurrent.atomic.AtomicBoolean | ||
|
||
class KotlinAsyncLambdaTest { | ||
|
||
companion object { | ||
private val success = AtomicBoolean(false) | ||
} | ||
|
||
@Rule @JvmField | ||
var testWorkflowRule: TestWorkflowRule = TestWorkflowRule.newBuilder() | ||
.setWorkflowTypes(NaiveParentWorkflowImpl::class.java) | ||
.setWorkflowClientOptions( | ||
WorkflowClientOptions.newBuilder() | ||
.setDataConverter(DefaultDataConverter(JacksonJsonPayloadConverter(KotlinObjectMapperFactory.new()))) | ||
.build() | ||
) | ||
.build() | ||
|
||
@WorkflowInterface | ||
interface NaiveParentWorkflow { | ||
@WorkflowMethod | ||
fun execute() | ||
} | ||
|
||
class NaiveParentWorkflowImpl : NaiveParentWorkflow { | ||
override fun execute() { | ||
Async.procedure { success.set(true) }.get() | ||
} | ||
} | ||
|
||
@Test | ||
fun asyncChildWorkflowTest() { | ||
val client = testWorkflowRule.workflowClient | ||
val options = WorkflowOptions.newBuilder().setTaskQueue(testWorkflowRule.taskQueue).build() | ||
val workflowStub = client.newWorkflowStub(NaiveParentWorkflow::class.java, options) | ||
workflowStub.execute() | ||
assertTrue(success.get()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.