|
| 1 | +package io.temporal.worker |
| 2 | + |
| 3 | +import io.temporal.activity.ActivityInterface |
| 4 | +import io.temporal.api.worker.v1.EnvironmentInfo |
| 5 | +import io.temporal.api.worker.v1.EnvironmentInfo.Runtime.RuntimeType |
| 6 | +import io.temporal.client.WorkflowClientOptions |
| 7 | +import io.temporal.testing.TestEnvironmentOptions |
| 8 | +import io.temporal.testing.TestWorkflowEnvironment |
| 9 | +import io.temporal.workflow.WorkflowInterface |
| 10 | +import io.temporal.workflow.WorkflowMethod |
| 11 | +import org.junit.After |
| 12 | +import org.junit.Assert.assertEquals |
| 13 | +import org.junit.Assert.assertFalse |
| 14 | +import org.junit.Assert.assertTrue |
| 15 | +import org.junit.Before |
| 16 | +import org.junit.Test |
| 17 | +import java.time.Duration |
| 18 | + |
| 19 | +class KotlinWorkerHeartbeatTest { |
| 20 | + private lateinit var testEnvironment: TestWorkflowEnvironment |
| 21 | + |
| 22 | + private val jvmRuntime = EnvironmentInfo.Runtime.newBuilder() |
| 23 | + .setType(RuntimeType.RUNTIME_TYPE_JVM) |
| 24 | + .setVersion("test-jvm-version") |
| 25 | + .build() |
| 26 | + private val environment = EnvironmentInfo.newBuilder() |
| 27 | + .addRuntimes(jvmRuntime) |
| 28 | + .build() |
| 29 | + |
| 30 | + @Before |
| 31 | + fun setUp() { |
| 32 | + // Exercise callbacks directly without a background heartbeat accepting the environment. |
| 33 | + testEnvironment = TestWorkflowEnvironment.newInstance( |
| 34 | + TestEnvironmentOptions.newBuilder() |
| 35 | + .setWorkflowClientOptions( |
| 36 | + WorkflowClientOptions.newBuilder() |
| 37 | + .setWorkerHeartbeatInterval(Duration.ofSeconds(-1)) |
| 38 | + .build() |
| 39 | + ) |
| 40 | + .build() |
| 41 | + ) |
| 42 | + } |
| 43 | + |
| 44 | + @After |
| 45 | + fun tearDown() { |
| 46 | + testEnvironment.close() |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + fun kotlinWorkflowRegistrationReportsKotlin() { |
| 51 | + val worker = testEnvironment.newWorker("kotlin-workflow") |
| 52 | + worker.registerWorkflowImplementationTypes(KotlinWorkflowImpl::class.java) |
| 53 | + testEnvironment.start() |
| 54 | + |
| 55 | + assertKotlinEnvironment(worker.buildHeartbeatCallback("group", environment).get().environment) |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + fun kotlinWorkflowRegistrationWithOptionsReportsKotlin() { |
| 60 | + val worker = testEnvironment.newWorker("kotlin-workflow-options") |
| 61 | + worker.registerWorkflowImplementationTypes( |
| 62 | + WorkflowImplementationOptions.newBuilder().build(), |
| 63 | + KotlinWorkflowImpl::class.java |
| 64 | + ) |
| 65 | + testEnvironment.start() |
| 66 | + |
| 67 | + assertKotlinEnvironment(worker.buildHeartbeatCallback("group", environment).get().environment) |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + fun kotlinActivityRegistrationReportsKotlinForLocalOnlyWorker() { |
| 72 | + val worker = testEnvironment.newWorker( |
| 73 | + "kotlin-local-activity", |
| 74 | + WorkerOptions.newBuilder().setLocalActivityWorkerOnly(true).build() |
| 75 | + ) |
| 76 | + worker.registerActivitiesImplementations(KotlinActivityImpl()) |
| 77 | + testEnvironment.start() |
| 78 | + |
| 79 | + assertKotlinEnvironment(worker.buildHeartbeatCallback("group", environment).get().environment) |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + fun kotlinDetectionIsPerWorkerNotPerClasspathOrFactory() { |
| 84 | + val kotlinWorker = testEnvironment.newWorker("kotlin") |
| 85 | + kotlinWorker.registerWorkflowImplementationTypes(KotlinWorkflowImpl::class.java) |
| 86 | + val javaWorker = testEnvironment.newWorker("java") |
| 87 | + javaWorker.registerWorkflowImplementationTypes(JavaHeartbeatImplementations.JavaWorkflowImpl::class.java) |
| 88 | + javaWorker.registerActivitiesImplementations(JavaHeartbeatImplementations.JavaActivityImpl()) |
| 89 | + val unregisteredWorker = testEnvironment.newWorker("unregistered") |
| 90 | + testEnvironment.start() |
| 91 | + |
| 92 | + assertKotlinEnvironment(kotlinWorker.buildHeartbeatCallback("group", environment).get().environment) |
| 93 | + assertEquals(environment, javaWorker.buildHeartbeatCallback("group", environment).get().environment) |
| 94 | + assertEquals(environment, unregisteredWorker.buildHeartbeatCallback("group", environment).get().environment) |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + fun environmentIsRetriedUntilHeartbeatAccepted() { |
| 99 | + val worker = testEnvironment.newWorker("retry") |
| 100 | + worker.registerWorkflowImplementationTypes(KotlinWorkflowImpl::class.java) |
| 101 | + testEnvironment.start() |
| 102 | + val heartbeat = worker.buildHeartbeatCallback("group", environment) |
| 103 | + |
| 104 | + val first = heartbeat.get() |
| 105 | + assertTrue(first.hasEnvironment()) |
| 106 | + assertKotlinEnvironment(first.environment) |
| 107 | + repeat(3) { |
| 108 | + val retry = heartbeat.get() |
| 109 | + assertTrue(retry.hasEnvironment()) |
| 110 | + assertEquals(first.environment, retry.environment) |
| 111 | + } |
| 112 | + |
| 113 | + worker.onHeartbeatAccepted() |
| 114 | + repeat(3) { |
| 115 | + assertFalse(heartbeat.get().hasEnvironment()) |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + @Test |
| 120 | + fun nullEnvironmentOptsOutEvenForKotlinImplementations() { |
| 121 | + val worker = testEnvironment.newWorker("optout") |
| 122 | + worker.registerWorkflowImplementationTypes(KotlinWorkflowImpl::class.java) |
| 123 | + worker.registerActivitiesImplementations(KotlinActivityImpl()) |
| 124 | + testEnvironment.start() |
| 125 | + val heartbeat = worker.buildHeartbeatCallback("group", null) |
| 126 | + |
| 127 | + repeat(3) { |
| 128 | + assertFalse(heartbeat.get().hasEnvironment()) |
| 129 | + } |
| 130 | + worker.onHeartbeatAccepted() |
| 131 | + assertFalse(heartbeat.get().hasEnvironment()) |
| 132 | + } |
| 133 | + |
| 134 | + private fun assertKotlinEnvironment(actual: EnvironmentInfo) { |
| 135 | + assertEquals(2, actual.runtimesCount) |
| 136 | + assertEquals(jvmRuntime, actual.runtimesList.single { it.type == RuntimeType.RUNTIME_TYPE_JVM }) |
| 137 | + val kotlinRuntime = actual.runtimesList.single { it.type == RuntimeType.RUNTIME_TYPE_KOTLIN } |
| 138 | + assertEquals(KotlinVersion.CURRENT.toString(), kotlinRuntime.version) |
| 139 | + // Augmenting the heartbeat must not mutate the caller's environment. |
| 140 | + assertEquals(listOf(jvmRuntime), environment.runtimesList) |
| 141 | + } |
| 142 | + |
| 143 | + @WorkflowInterface |
| 144 | + interface KotlinWorkflow { |
| 145 | + @WorkflowMethod |
| 146 | + fun execute() |
| 147 | + } |
| 148 | + |
| 149 | + class KotlinWorkflowImpl : KotlinWorkflow { |
| 150 | + override fun execute() {} |
| 151 | + } |
| 152 | + |
| 153 | + @ActivityInterface |
| 154 | + interface KotlinActivity { |
| 155 | + fun executeActivity() |
| 156 | + } |
| 157 | + |
| 158 | + class KotlinActivityImpl : KotlinActivity { |
| 159 | + override fun executeActivity() {} |
| 160 | + } |
| 161 | +} |
0 commit comments