Skip to content

Commit b6a5110

Browse files
committed
add struct test
1 parent 6ced0d1 commit b6a5110

File tree

2 files changed

+52
-3
lines changed

2 files changed

+52
-3
lines changed

dbos/system_database.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1014,7 +1014,7 @@ func (s *systemDatabase) notificationListenerLoop(ctx context.Context) {
10141014
const DBOS_NULL_TOPIC = "__null__topic__"
10151015

10161016
// Send is a special type of step that sends a message to another workflow.
1017-
// Two differences with a normal steps: durability and the function run in the same transaction, and we forbid nested step execution
1017+
// Three differences with a normal steps: durability and the function run in the same transaction, and we forbid nested step execution
10181018
func (s *systemDatabase) Send(ctx context.Context, input WorkflowSendInput) error {
10191019
functionName := "DBOS.send"
10201020

dbos/workflows_test.go

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -980,8 +980,10 @@ func TestScheduledWorkflows(t *testing.T) {
980980
}
981981

982982
var (
983-
sendWf = WithWorkflow(sendWorkflow)
984-
receiveWf = WithWorkflow(receiveWorkflow)
983+
sendWf = WithWorkflow(sendWorkflow)
984+
receiveWf = WithWorkflow(receiveWorkflow)
985+
sendStructWf = WithWorkflow(sendStructWorkflow)
986+
receiveStructWf = WithWorkflow(receiveStructWorkflow)
985987
)
986988

987989
type sendWorkflowInput struct {
@@ -1023,6 +1025,20 @@ func receiveWorkflow(ctx context.Context, topic string) (string, error) {
10231025
return msg1 + "-" + msg2 + "-" + msg3, nil
10241026
}
10251027

1028+
func sendStructWorkflow(ctx context.Context, input sendWorkflowInput) (string, error) {
1029+
testStruct := sendRecvType{Value: "test-struct-value"}
1030+
err := Send(ctx, WorkflowSendInput{DestinationID: input.DestinationID, Topic: input.Topic, Message: testStruct})
1031+
return "", err
1032+
}
1033+
1034+
func receiveStructWorkflow(ctx context.Context, topic string) (sendRecvType, error) {
1035+
return Recv[sendRecvType](ctx, WorkflowRecvInput{Topic: topic, Timeout: 3 * time.Second})
1036+
}
1037+
1038+
type sendRecvType struct {
1039+
Value string
1040+
}
1041+
10261042
func TestSendRecv(t *testing.T) {
10271043
setupDBOS(t)
10281044

@@ -1102,6 +1118,39 @@ func TestSendRecv(t *testing.T) {
11021118
}
11031119
})
11041120

1121+
t.Run("SendRecvCustomStruct", func(t *testing.T) {
1122+
// Start the receive workflow
1123+
receiveHandle, err := receiveStructWf(context.Background(), "struct-topic")
1124+
if err != nil {
1125+
t.Fatalf("failed to start receive workflow: %v", err)
1126+
}
1127+
1128+
// Send the struct to the receive workflow
1129+
sendHandle, err := sendStructWf(context.Background(), sendWorkflowInput{
1130+
DestinationID: receiveHandle.GetWorkflowID(),
1131+
Topic: "struct-topic",
1132+
})
1133+
if err != nil {
1134+
t.Fatalf("failed to send struct: %v", err)
1135+
}
1136+
1137+
_, err = sendHandle.GetResult(context.Background())
1138+
if err != nil {
1139+
t.Fatalf("failed to get result from send workflow: %v", err)
1140+
}
1141+
1142+
// Get the result from receive workflow
1143+
result, err := receiveHandle.GetResult(context.Background())
1144+
if err != nil {
1145+
t.Fatalf("failed to get result from receive workflow: %v", err)
1146+
}
1147+
1148+
// Verify the struct was received correctly
1149+
if result.Value != "test-struct-value" {
1150+
t.Fatalf("expected received struct value to be 'test-struct-value', got '%s'", result.Value)
1151+
}
1152+
})
1153+
11051154
t.Run("SendToNonExistentUUID", func(t *testing.T) {
11061155
// Generate a non-existent UUID
11071156
destUUID := uuid.NewString()

0 commit comments

Comments
 (0)