-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Progress towards closing #81
- Loading branch information
1 parent
b40fd1c
commit 8e8b101
Showing
6 changed files
with
269 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ node_modules | |
|
||
# Python stuff | ||
__pycache__ | ||
pyrightconfig.json | ||
|
||
# Build stuff | ||
bin | ||
|
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,66 @@ | ||
import dataclasses | ||
|
||
from temporalio import workflow | ||
from temporalio.api.common.v1 import DataBlob | ||
from temporalio.client import WorkflowHandle | ||
from temporalio.converter import ( | ||
BinaryNullPayloadConverter, | ||
BinaryProtoPayloadConverter, | ||
CompositePayloadConverter, | ||
DataConverter, | ||
) | ||
|
||
from harness.python.feature import ( | ||
Runner, | ||
get_workflow_argument_payload, | ||
get_workflow_result_payload, | ||
register_feature, | ||
) | ||
|
||
EXPECTED_RESULT = DataBlob(data=bytes.fromhex("deadbeef")) | ||
|
||
# An echo workflow | ||
@workflow.defn | ||
class Workflow: | ||
@workflow.run | ||
async def run(self, res: DataBlob) -> DataBlob: | ||
return res | ||
|
||
|
||
async def check_result(_: Runner, handle: WorkflowHandle) -> None: | ||
# verify client result is DataBlob `0xdeadbeef` | ||
result = await handle.result() | ||
assert result == EXPECTED_RESULT | ||
payload = await get_workflow_result_payload(handle) | ||
|
||
encoding = payload.metadata["encoding"].decode("utf-8") | ||
assert encoding == "binary/protobuf" | ||
|
||
message_type = payload.metadata["messageType"].decode("utf-8") | ||
assert message_type == "temporal.api.common.v1.DataBlob" | ||
|
||
result_in_history = DataBlob() | ||
result_in_history.ParseFromString(payload.data) | ||
assert result == result_in_history | ||
|
||
payload_arg = await get_workflow_argument_payload(handle) | ||
assert payload == payload_arg | ||
|
||
|
||
class DefaultBinProtoPayloadConverter(CompositePayloadConverter): | ||
def __init__(self) -> None: | ||
super().__init__( | ||
# Disable ByteSlice, ProtoJSON, and JSON converters | ||
BinaryNullPayloadConverter(), | ||
BinaryProtoPayloadConverter(), | ||
) | ||
|
||
|
||
register_feature( | ||
workflows=[Workflow], | ||
check_result=check_result, | ||
start_options={"arg": EXPECTED_RESULT}, | ||
data_converter=dataclasses.replace( | ||
DataConverter.default, payload_converter_class=DefaultBinProtoPayloadConverter | ||
), | ||
) |
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,89 @@ | ||
import base64 | ||
import dataclasses | ||
import json | ||
from typing import Dict, List, Sequence | ||
|
||
from temporalio import workflow | ||
from temporalio.api.common.v1 import Payload | ||
from temporalio.client import WorkflowHandle | ||
from temporalio.converter import DataConverter, PayloadCodec | ||
|
||
from harness.python.feature import ( | ||
Runner, | ||
get_workflow_argument_payload, | ||
get_workflow_result_payload, | ||
register_feature, | ||
) | ||
|
||
Result = Dict[str, bool] | ||
|
||
EXPECTED_RESULT: Result = {"spec": True} | ||
|
||
CODEC_ENCODING = "my_encoding" | ||
|
||
# An echo workflow | ||
@workflow.defn | ||
class Workflow: | ||
@workflow.run | ||
async def run(self, res: Result) -> Result: | ||
return res | ||
|
||
|
||
async def check_result(_: Runner, handle: WorkflowHandle) -> None: | ||
# verify client result is `{"spec": true}` | ||
result = await handle.result() | ||
assert result == EXPECTED_RESULT | ||
payload = await get_workflow_result_payload(handle) | ||
|
||
encoding = payload.metadata["encoding"].decode("utf-8") | ||
assert encoding == CODEC_ENCODING | ||
|
||
extractedData = base64.b64decode(payload.data) | ||
innerPayload = Payload() | ||
innerPayload.ParseFromString(extractedData) | ||
|
||
encoding = innerPayload.metadata["encoding"].decode("utf-8") | ||
assert encoding == "json/plain" | ||
|
||
result_in_history = json.loads(innerPayload.data) | ||
assert result == result_in_history | ||
|
||
payload_arg = await get_workflow_argument_payload(handle) | ||
assert payload == payload_arg | ||
|
||
|
||
# Based on samples-python/encryption/codec.py | ||
class Base64PayloadCodec(PayloadCodec): | ||
def __init__(self) -> None: | ||
super().__init__() | ||
|
||
async def encode(self, payloads: Sequence[Payload]) -> List[Payload]: | ||
return [ | ||
Payload( | ||
metadata={ | ||
"encoding": b"my_encoding", | ||
}, | ||
data=base64.b64encode(p.SerializeToString()), | ||
) | ||
for p in payloads | ||
] | ||
|
||
async def decode(self, payloads: Sequence[Payload]) -> List[Payload]: | ||
ret: List[Payload] = [] | ||
for p in payloads: | ||
if p.metadata.get("encoding", b"").decode() != CODEC_ENCODING: | ||
ret.append(p) | ||
continue | ||
ret.append(Payload.FromString(base64.b64decode(p.data))) | ||
return ret | ||
|
||
|
||
register_feature( | ||
workflows=[Workflow], | ||
check_result=check_result, | ||
start_options={"arg": EXPECTED_RESULT}, | ||
data_converter=dataclasses.replace( | ||
DataConverter.default, | ||
payload_codec=Base64PayloadCodec(), | ||
), | ||
) |
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,46 @@ | ||
import json | ||
from typing import Dict | ||
|
||
from temporalio import workflow | ||
from temporalio.client import WorkflowHandle | ||
|
||
from harness.python.feature import ( | ||
Runner, | ||
get_workflow_argument_payload, | ||
get_workflow_result_payload, | ||
register_feature, | ||
) | ||
|
||
Result = Dict[str, bool] | ||
|
||
EXPECTED_RESULT: Result = {"spec": True} | ||
|
||
# An echo workflow | ||
@workflow.defn | ||
class Workflow: | ||
@workflow.run | ||
async def run(self, res: Result) -> Result: | ||
return res | ||
|
||
|
||
async def check_result(_: Runner, handle: WorkflowHandle) -> None: | ||
# verify client result is `{"spec": true}` | ||
result = await handle.result() | ||
assert result == EXPECTED_RESULT | ||
payload = await get_workflow_result_payload(handle) | ||
|
||
encoding = payload.metadata["encoding"].decode("utf-8") | ||
assert encoding == "json/plain" | ||
|
||
result_in_history = json.loads(payload.data) | ||
assert result == result_in_history | ||
|
||
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}, | ||
) |
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,47 @@ | ||
from temporalio import workflow | ||
from temporalio.api.common.v1 import DataBlob | ||
from temporalio.client import WorkflowHandle | ||
from temporalio.converter import JSONProtoPayloadConverter | ||
|
||
from harness.python.feature import ( | ||
Runner, | ||
get_workflow_argument_payload, | ||
get_workflow_result_payload, | ||
register_feature, | ||
) | ||
|
||
EXPECTED_RESULT = DataBlob(data=bytes.fromhex("deadbeef")) | ||
JSONP_decoder = JSONProtoPayloadConverter() | ||
|
||
# An echo workflow | ||
@workflow.defn | ||
class Workflow: | ||
@workflow.run | ||
async def run(self, res: DataBlob) -> DataBlob: | ||
return res | ||
|
||
|
||
async def check_result(_: Runner, handle: WorkflowHandle) -> None: | ||
# verify client result is DataBlob `0xdeadbeef` | ||
result = await handle.result() | ||
assert result == EXPECTED_RESULT | ||
payload = await get_workflow_result_payload(handle) | ||
|
||
encoding = payload.metadata["encoding"].decode("utf-8") | ||
assert encoding == "json/protobuf" | ||
|
||
message_type = payload.metadata["messageType"].decode("utf-8") | ||
assert message_type == "temporal.api.common.v1.DataBlob" | ||
|
||
result_in_history = JSONP_decoder.from_payload(payload) | ||
assert result == result_in_history | ||
|
||
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}, | ||
) |
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