Skip to content

Commit

Permalink
Add tests for cancel invocation/get invocation id feature
Browse files Browse the repository at this point in the history
  • Loading branch information
slinkydeveloper committed Sep 18, 2024
1 parent 8172e1c commit ff525b6
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/main/kotlin/dev/restate/sdktesting/contracts/Proxy.kt
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ interface Proxy {
// Bytes are encoded as array of numbers
@Handler suspend fun call(context: Context, request: ProxyRequest): ByteArray

@Handler suspend fun oneWayCall(context: Context, request: ProxyRequest)
// Returns the invocation id of the call
@Handler suspend fun oneWayCall(context: Context, request: ProxyRequest): String

@Handler suspend fun manyCalls(context: Context, requests: List<ManyCallRequest>)
}
3 changes: 3 additions & 0 deletions src/main/kotlin/dev/restate/sdktesting/contracts/TestUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ interface TestUtilsService {
*/
@Handler suspend fun countExecutedSideEffects(context: Context, increments: Int): Int

/** Cancel invocation using the context. */
@Handler suspend fun cancelInvocation(context: Context, invocationId: String)

/** Read an environment variable */
@Handler suspend fun getEnvVariable(context: Context, env: String): String

Expand Down
48 changes: 45 additions & 3 deletions src/main/kotlin/dev/restate/sdktesting/tests/CancelInvocation.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import dev.restate.admin.api.InvocationApi
import dev.restate.admin.client.ApiClient
import dev.restate.admin.model.TerminationMode
import dev.restate.sdk.client.Client
import dev.restate.sdk.kotlin.KtSerdes
import dev.restate.sdktesting.contracts.*
import dev.restate.sdktesting.infra.*
import java.net.URL
Expand All @@ -34,13 +35,15 @@ class CancelInvocation {
.withServices(
CancelTestRunnerDefinitions.SERVICE_NAME,
CancelTestBlockingServiceDefinitions.SERVICE_NAME,
AwakeableHolderDefinitions.SERVICE_NAME))
AwakeableHolderDefinitions.SERVICE_NAME,
ProxyDefinitions.SERVICE_NAME,
TestUtilsServiceDefinitions.SERVICE_NAME))
}
}

@ParameterizedTest(name = "cancel blocked invocation on {0}")
@ParameterizedTest(name = "cancel blocked invocation on {0} from Admin API")
@EnumSource(value = BlockingOperation::class)
fun cancelInvocation(
fun cancelInvocationFromAdminAPI(
blockingOperation: BlockingOperation,
@InjectClient ingressClient: Client,
@InjectMetaURL metaURL: URL,
Expand Down Expand Up @@ -69,4 +72,43 @@ class CancelInvocation {
// Check that the singleton service is unlocked
blockingServiceClient.isUnlocked()
}

@ParameterizedTest(name = "cancel blocked invocation on {0} from Context")
@EnumSource(value = BlockingOperation::class)
fun cancelInvocationFromContext(
blockingOperation: BlockingOperation,
@InjectClient ingressClient: Client,
) = runTest {
val key = UUID.randomUUID().toString()
val cancelTestClient = CancelTestRunnerClient.fromClient(ingressClient, key)
val blockingServiceClient = CancelTestBlockingServiceClient.fromClient(ingressClient, key)
val proxyClient = ProxyClient.fromClient(ingressClient)
val testUtilsClient = TestUtilsServiceClient.fromClient(ingressClient)

val id =
proxyClient.oneWayCall(
ProxyRequest(
serviceName = CancelTestRunnerDefinitions.SERVICE_NAME,
virtualObjectKey = key,
handlerName = "startTest",
message = KtSerdes.json<BlockingOperation>().serialize(blockingOperation)))

val awakeableHolderClient = AwakeableHolderClient.fromClient(ingressClient, "cancel")

await until { runBlocking { awakeableHolderClient.hasAwakeable() } }

awakeableHolderClient.unlock("cancel")

// The termination signal might arrive before the blocking call to the cancel singleton was
// made, so we need to retry.
await.ignoreException(TimeoutCancellationException::class.java).until {
runBlocking {
testUtilsClient.cancelInvocation(id)
withTimeout(1.seconds) { cancelTestClient.verifyTest() }
}
}

// Check that the singleton service is unlocked
blockingServiceClient.isUnlocked()
}
}

0 comments on commit ff525b6

Please sign in to comment.