Skip to content

Commit

Permalink
Merge pull request #15 from dispatchrun/simplify-unit-testing
Browse files Browse the repository at this point in the history
Simplify unit testing using new `dispatchtest.Call` helper
  • Loading branch information
chriso authored Jul 2, 2024
2 parents 50e72c2 + 1fa9266 commit 42f61a4
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 36 deletions.
7 changes: 2 additions & 5 deletions dispatchtest/integration/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,9 @@ func run() error {
return strings.Repeat(stringified, doubled), nil
})

call, err := doubleAndRepeat.BuildCall(4)
if err != nil {
return fmt.Errorf("new call failed: %v", err)
}
runner := dispatchtest.NewRunner(stringify, double, doubleAndRepeat)

output, err := dispatchtest.Run[string](call, stringify, double, doubleAndRepeat)
output, err := dispatchtest.Call(runner, doubleAndRepeat, 4)
if err != nil {
return err
}
Expand Down
21 changes: 14 additions & 7 deletions dispatchtest/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,33 @@ import (
"github.com/dispatchrun/dispatch-go/dispatchproto"
)

// Run runs a function and returns its result.
func Run[O any](call dispatchproto.Call, functions ...dispatch.AnyFunction) (O, error) {
runner := NewRunner(functions...)
// Call calls a dispatch.Function using the specified Runner.
func Call[I, O any](runner *Runner, fn *dispatch.Function[I, O], input I) (O, error) {
// Note: runner.Call[I, O] isn't possible because Go doesn't support generic methods.

var zero O
call, err := fn.BuildCall(input)
if err != nil {
return zero, err
}

res := runner.Run(call.Request())

var output O
result, ok := res.Result()
if !ok {
if !res.OK() {
return output, dispatchproto.StatusError(res.Status())
return zero, dispatchproto.StatusError(res.Status())
}
return output, fmt.Errorf("unexpected response: %s", res)
return zero, fmt.Errorf("unexpected response: %s", res)
}
var err error

if resultErr, ok := result.Error(); ok {
err = resultErr
} else if !res.OK() {
err = dispatchproto.StatusError(res.Status())
}

var output O
boxedOutput, ok := res.Output()
if ok {
if unmarshalErr := boxedOutput.Unmarshal(&output); err == nil && unmarshalErr != nil {
Expand Down
34 changes: 10 additions & 24 deletions function_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,16 @@ func TestCoroutineReturn(t *testing.T) {
return strconv.Itoa(in), nil
})

call, err := stringify.BuildCall(11)
if err != nil {
t.Fatal(err)
}
output, err := dispatchtest.Run[string](call, stringify)
runner := dispatchtest.NewRunner(stringify)

output, err := dispatchtest.Call(runner, stringify, 11)
if err != nil {
t.Fatal(err)
}
if output != "11" {
} else if output != "11" {
t.Errorf("unexpected output: %s", output)
}

call2, err := stringify.BuildCall(-23)
if err != nil {
t.Fatal(err)
}
_, err = dispatchtest.Run[string](call2, stringify)
_, err = dispatchtest.Call(runner, stringify, -23)
if err == nil || !strings.Contains(err.Error(), "InvalidArgument: -23") {
t.Fatalf("unexpected error: %v", err)
}
Expand All @@ -72,23 +65,16 @@ func TestCoroutineExit(t *testing.T) {
panic("unreachable")
})

call, err := stringify.BuildCall(11)
if err != nil {
t.Fatal(err)
}
output, err := dispatchtest.Run[string](call, stringify)
runner := dispatchtest.NewRunner(stringify)

output, err := dispatchtest.Call(runner, stringify, 11)
if err != nil {
t.Fatal(err)
}
if output != "11" {
} else if output != "11" {
t.Errorf("unexpected output: %s", output)
}

call2, err := stringify.BuildCall(-23)
if err != nil {
t.Fatal(err)
}
_, err = dispatchtest.Run[string](call2, stringify)
_, err = dispatchtest.Call(runner, stringify, -23)
if err == nil || !strings.Contains(err.Error(), "InvalidArgument: -23") {
t.Fatalf("unexpected error: %v", err)
}
Expand Down

0 comments on commit 42f61a4

Please sign in to comment.