Skip to content

Commit

Permalink
Add an input to the binary data converter feature (#312)
Browse files Browse the repository at this point in the history
  • Loading branch information
antlai-temporal authored Jul 14, 2023
1 parent 6385bb5 commit 0ec38b2
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 22 deletions.
5 changes: 4 additions & 1 deletion features/data_converter/binary/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ Binary values can be converted to and from `binary/plain` Payloads.

Steps:

- run a workflow that returns binary value `0xdeadbeef`
- run a echo workflow that accepts and returns binary value `0xdeadbeef`
- verify client result is binary `0xdeadbeef`
- get result payload of WorkflowExecutionCompleted event from workflow history
- load JSON payload from `./payload.json` and compare it to result payload
- get argument payload of WorkflowExecutionStarted event from workflow history
- verify that argument and result payloads are the same


# Detailed spec

Expand Down
16 changes: 13 additions & 3 deletions features/data_converter/binary/feature.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ var expectedResult = []byte{0xde, 0xad, 0xbe, 0xef}
var Feature = harness.Feature{
Workflows: Workflow,
CheckResult: CheckResult,
// ExecuteDefault does not support workflow arguments
Execute: harness.ExecuteWithArgs(Workflow, expectedResult),
}

// run a workflow that returns binary value `0xdeadbeef`
func Workflow(ctx workflow.Context) ([]byte, error) {
return expectedResult, nil
// run an echo workflow that returns binary value `0xdeadbeef`
func Workflow(ctx workflow.Context, res []byte) ([]byte, error) {
return res, nil
}

func CheckResult(ctx context.Context, runner *harness.Runner, run client.WorkflowRun) error {
Expand Down Expand Up @@ -54,5 +56,13 @@ func CheckResult(ctx context.Context, runner *harness.Runner, run client.Workflo
return err
}
runner.Require.Equal(expectedPayload, payload)

payloadArg, err := harness.GetWorkflowArgumentPayload(ctx, runner.Client, run.GetID())
if err != nil {
return err
}

runner.Require.Equal(payload, payloadArg)

return nil
}
16 changes: 12 additions & 4 deletions features/data_converter/binary/feature.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,18 @@ public interface feature extends Feature {
byte[] DEAD_BEEF = new byte[] {(byte) 0xde, (byte) 0xad, (byte) 0xbe, (byte) 0xef};

@WorkflowMethod
byte[] workflow();
byte[] workflow(byte[] res);

class Impl implements feature {
/** run a workflow that returns binary value `0xdeadbeef` */
/** run an echo workflow that returns binary value `0xdeadbeef` */
@Override
public byte[] workflow() {
return DEAD_BEEF;
public byte[] workflow(byte[] res) {
return res;
}

@Override
public Run execute(Runner runner) throws Exception {
return runner.executeSingleWorkflow(null, DEAD_BEEF);
}

@Override
Expand All @@ -50,6 +55,9 @@ public void checkResult(Runner runner, Run run) throws Exception {
JsonFormat.parser().merge(new String(content), builder);
var expected = builder.build();
assertEquals(expected, payload);

var payloadArg = runner.getWorkflowArgumentPayload(run);
assertEquals(payload, payloadArg);
}
}
}
27 changes: 16 additions & 11 deletions features/data_converter/binary/feature.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,16 @@
from google.protobuf.json_format import Parse
from temporalio import workflow
from temporalio.api.common.v1 import Payload
from temporalio.api.enums.v1 import EventType
from temporalio.client import WorkflowHandle

from harness.python.feature import Runner, register_feature
from harness.python.feature import (
Runner,
get_workflow_argument_payload,
get_workflow_result_payload,
register_feature,
)

EXPECTED_RESULT = bytes.fromhex("deadbeef")


@workflow.defn
Expand All @@ -16,22 +22,17 @@ class Workflow:
"""

@workflow.run
async def run(self) -> bytes:
return bytes.fromhex("deadbeef")
async def run(self, res: bytes) -> bytes:
return res


async def check_result(runner: Runner, handle: WorkflowHandle) -> None:
# verify client result is binary `0xdeadbeef`
result = await handle.result()
assert result == bytes.fromhex("deadbeef")
assert result == EXPECTED_RESULT

# get result payload of WorkflowExecutionCompleted event from workflow history
event = await anext(
e
async for e in handle.fetch_history_events()
if e.event_type == EventType.EVENT_TYPE_WORKFLOW_EXECUTION_COMPLETED
)
payload = event.workflow_execution_completed_event_attributes.result.payloads[0]
payload = await get_workflow_result_payload(handle)

# load JSON payload from `./payload.json` and compare it to JSON representation of result payload
with open(
Expand All @@ -41,8 +42,12 @@ async def check_result(runner: Runner, handle: WorkflowHandle) -> None:
expected_payload = Parse(f.read(), Payload())
assert payload == expected_payload

payload_arg = await get_workflow_argument_payload(handle)
assert payload == payload_arg


register_feature(
workflows=[Workflow],
check_result=check_result,
start_options={"arg": EXPECTED_RESULT},
)
14 changes: 11 additions & 3 deletions features/data_converter/binary/feature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ import expectedPayload from './payload.json';

const deadbeef = new Uint8Array([0xde, 0xad, 0xbe, 0xef]);

// run a workflow that returns binary value `0xdeadbeef`
export async function workflow(): Promise<Uint8Array> {
return deadbeef;
// run an echo workflow that returns binary value `0xdeadbeef`
export async function workflow(res: Uint8Array): Promise<Uint8Array> {
return res;
}

export const feature = new Feature({
workflow,
workflowStartOptions: {
args: [deadbeef],
},
async checkResult(runner, handle) {
// verify client result is binary `0xdeadbeef`
const result = await handle.result();
Expand All @@ -23,5 +26,10 @@ export const feature = new Feature({

// load JSON payload from `./payload.json` and compare it to result payload
assert.deepEqual(JSONToPayload(expectedPayload), payload);

const payloadArg = await runner.getWorkflowArgumentPayload(handle);
assert.ok(payloadArg);

assert.deepEqual(payload, payloadArg);
},
});

0 comments on commit 0ec38b2

Please sign in to comment.