Skip to content

Commit 42f61a4

Browse files
authored
Merge pull request #15 from dispatchrun/simplify-unit-testing
Simplify unit testing using new `dispatchtest.Call` helper
2 parents 50e72c2 + 1fa9266 commit 42f61a4

File tree

3 files changed

+26
-36
lines changed

3 files changed

+26
-36
lines changed

dispatchtest/integration/main.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,9 @@ func run() error {
4040
return strings.Repeat(stringified, doubled), nil
4141
})
4242

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

48-
output, err := dispatchtest.Run[string](call, stringify, double, doubleAndRepeat)
45+
output, err := dispatchtest.Call(runner, doubleAndRepeat, 4)
4946
if err != nil {
5047
return err
5148
}

dispatchtest/run.go

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,33 @@ import (
1111
"github.com/dispatchrun/dispatch-go/dispatchproto"
1212
)
1313

14-
// Run runs a function and returns its result.
15-
func Run[O any](call dispatchproto.Call, functions ...dispatch.AnyFunction) (O, error) {
16-
runner := NewRunner(functions...)
14+
// Call calls a dispatch.Function using the specified Runner.
15+
func Call[I, O any](runner *Runner, fn *dispatch.Function[I, O], input I) (O, error) {
16+
// Note: runner.Call[I, O] isn't possible because Go doesn't support generic methods.
17+
18+
var zero O
19+
call, err := fn.BuildCall(input)
20+
if err != nil {
21+
return zero, err
22+
}
1723

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

20-
var output O
2126
result, ok := res.Result()
2227
if !ok {
2328
if !res.OK() {
24-
return output, dispatchproto.StatusError(res.Status())
29+
return zero, dispatchproto.StatusError(res.Status())
2530
}
26-
return output, fmt.Errorf("unexpected response: %s", res)
31+
return zero, fmt.Errorf("unexpected response: %s", res)
2732
}
28-
var err error
33+
2934
if resultErr, ok := result.Error(); ok {
3035
err = resultErr
3136
} else if !res.OK() {
3237
err = dispatchproto.StatusError(res.Status())
3338
}
39+
40+
var output O
3441
boxedOutput, ok := res.Output()
3542
if ok {
3643
if unmarshalErr := boxedOutput.Unmarshal(&output); err == nil && unmarshalErr != nil {

function_test.go

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -36,23 +36,16 @@ func TestCoroutineReturn(t *testing.T) {
3636
return strconv.Itoa(in), nil
3737
})
3838

39-
call, err := stringify.BuildCall(11)
40-
if err != nil {
41-
t.Fatal(err)
42-
}
43-
output, err := dispatchtest.Run[string](call, stringify)
39+
runner := dispatchtest.NewRunner(stringify)
40+
41+
output, err := dispatchtest.Call(runner, stringify, 11)
4442
if err != nil {
4543
t.Fatal(err)
46-
}
47-
if output != "11" {
44+
} else if output != "11" {
4845
t.Errorf("unexpected output: %s", output)
4946
}
5047

51-
call2, err := stringify.BuildCall(-23)
52-
if err != nil {
53-
t.Fatal(err)
54-
}
55-
_, err = dispatchtest.Run[string](call2, stringify)
48+
_, err = dispatchtest.Call(runner, stringify, -23)
5649
if err == nil || !strings.Contains(err.Error(), "InvalidArgument: -23") {
5750
t.Fatalf("unexpected error: %v", err)
5851
}
@@ -72,23 +65,16 @@ func TestCoroutineExit(t *testing.T) {
7265
panic("unreachable")
7366
})
7467

75-
call, err := stringify.BuildCall(11)
76-
if err != nil {
77-
t.Fatal(err)
78-
}
79-
output, err := dispatchtest.Run[string](call, stringify)
68+
runner := dispatchtest.NewRunner(stringify)
69+
70+
output, err := dispatchtest.Call(runner, stringify, 11)
8071
if err != nil {
8172
t.Fatal(err)
82-
}
83-
if output != "11" {
73+
} else if output != "11" {
8474
t.Errorf("unexpected output: %s", output)
8575
}
8676

87-
call2, err := stringify.BuildCall(-23)
88-
if err != nil {
89-
t.Fatal(err)
90-
}
91-
_, err = dispatchtest.Run[string](call2, stringify)
77+
_, err = dispatchtest.Call(runner, stringify, -23)
9278
if err == nil || !strings.Contains(err.Error(), "InvalidArgument: -23") {
9379
t.Fatalf("unexpected error: %v", err)
9480
}

0 commit comments

Comments
 (0)