Skip to content

Commit

Permalink
Test exit directive
Browse files Browse the repository at this point in the history
  • Loading branch information
chriso committed Jun 18, 2024
1 parent 78bba6b commit 7f863e8
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 7 deletions.
23 changes: 16 additions & 7 deletions coroutine.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,23 @@ func (c *GenericCoroutine[I, O]) Run(ctx context.Context, req Request) Response
// Run the coroutine until it yields or returns.
if coro.Next() {
// The coroutine yielded and is now paused.
// Serialize the coroutine.
state, err := c.serialize(id, coro)
if err != nil {
return NewResponseError(err)
yield := coro.Recv()

// Serialize the coroutine, unless it's a terminal Exit directive.
var state Any
if _, terminal := yield.directive.(Exit); !terminal {
var err error
state, err = c.serialize(id, coro)
if err != nil {
return NewResponseError(err)
}
} else {
coro.Stop()
coro.Next()
}
// Yield to Dispatch with the directive from the coroutine.
result := coro.Recv()
return NewResponse(result.status, result.directive, CoroutineState(state))

// Yield to Dispatch with the directive.
return NewResponse(yield.status, yield.directive, CoroutineState(state))
}

// The coroutine returned. Serialize the output / error.
Expand Down
99 changes: 99 additions & 0 deletions coroutine_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package dispatch_test

import (
"context"
"fmt"
"strconv"
"testing"

"github.com/dispatchrun/coroutine"
"github.com/dispatchrun/dispatch-go"
)

func logMode(t *testing.T) {
t.Helper()

if coroutine.Durable {
t.Log("running in durable mode")
} else {
t.Log("running in volatile mode")
}
}

func TestCoroutineReturnOnly(t *testing.T) {
logMode(t)

coro := dispatch.NewCoroutine("stringify", func(ctx context.Context, in int) (string, error) {
if in < 0 {
return "", fmt.Errorf("%w: %d", dispatch.ErrInvalidArgument, in)
}
return strconv.Itoa(in), nil
})
defer coro.Close()

res := coro.Run(context.Background(), dispatch.NewRequest("stringify", dispatch.Input(dispatch.Int(11))))
if res.Status() != dispatch.OKStatus {
t.Errorf("unexpected status: %s", res.Status())
}
output, ok := res.Output()
if !ok {
t.Errorf("expected output, got: %s", res)
}
var got string
if err := output.Unmarshal(&got); err != nil {
t.Fatal(err)
} else if got != "11" {
t.Errorf("unexpected output: %s", got)
}

res = coro.Run(context.Background(), dispatch.NewRequest("stringify", dispatch.Input(dispatch.Int(-23))))
if res.Status() != dispatch.InvalidArgumentStatus {
t.Errorf("unexpected status: %s", res.Status())
}
if err, ok := res.Error(); !ok {
t.Errorf("expected error, got: %s", res)
} else if got := err.Message(); got != "InvalidArgument: -23" {
t.Errorf("unexpected error: %s", got)
}
}

func TestCoroutineExit(t *testing.T) {
logMode(t)

coro := dispatch.NewCoroutine("stringify", func(ctx context.Context, in int) (string, error) {
if in < 0 {
err := fmt.Errorf("%w: %d", dispatch.ErrInvalidArgument, in)
dispatch.Yield[string](dispatch.InvalidArgumentStatus, dispatch.NewExit(dispatch.NewError(err)))
} else {
output := dispatch.String(strconv.Itoa(in))
dispatch.Yield[string](dispatch.OKStatus, dispatch.NewExit(dispatch.Output(output)))
}
panic("unreachable")
})
defer coro.Close()

res := coro.Run(context.Background(), dispatch.NewRequest("stringify", dispatch.Input(dispatch.Int(11))))
if res.Status() != dispatch.OKStatus {
t.Errorf("unexpected status: %s", res.Status())
}
output, ok := res.Output()
if !ok {
t.Errorf("expected output, got: %s", res)
}
var got string
if err := output.Unmarshal(&got); err != nil {
t.Fatal(err)
} else if got != "11" {
t.Errorf("unexpected output: %s", got)
}

res = coro.Run(context.Background(), dispatch.NewRequest("stringify", dispatch.Input(dispatch.Int(-23))))
if res.Status() != dispatch.InvalidArgumentStatus {
t.Errorf("unexpected status: %s", res.Status())
}
if err, ok := res.Error(); !ok {
t.Errorf("expected error, got: %s", res)
} else if got := err.Message(); got != "InvalidArgument: -23" {
t.Errorf("unexpected error: %s", got)
}
}

0 comments on commit 7f863e8

Please sign in to comment.