-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
115 additions
and
7 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
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,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) | ||
} | ||
} |