Skip to content

Commit

Permalink
add integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
matt2e committed Jul 15, 2024
1 parent 0e30991 commit 7f0c29a
Show file tree
Hide file tree
Showing 14 changed files with 44 additions and 22 deletions.
24 changes: 12 additions & 12 deletions backend/controller/dal/fsm_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,34 +26,34 @@ func TestFSM(t *testing.T) {
in.CopyModule("fsm"),
in.Deploy("fsm"),

in.Call("fsm", "sendOne", in.Obj{"instance": "1"}, nil),
in.Call("fsm", "sendOne", in.Obj{"instance": "2"}, nil),
in.Call[in.Obj, in.Obj]("fsm", "sendOne", in.Obj{"instance": "1"}, nil),
in.Call[in.Obj, in.Obj]("fsm", "sendOne", in.Obj{"instance": "2"}, nil),
in.FileContains(logFilePath, "start 1"),
in.FileContains(logFilePath, "start 2"),
fsmInState("1", "running", "fsm.start"),
fsmInState("2", "running", "fsm.start"),

in.Call("fsm", "sendOne", in.Obj{"instance": "1"}, nil),
in.Call[in.Obj, in.Obj]("fsm", "sendOne", in.Obj{"instance": "1"}, nil),
in.FileContains(logFilePath, "middle 1"),
fsmInState("1", "running", "fsm.middle"),

in.Call("fsm", "sendOne", in.Obj{"instance": "1"}, nil),
in.Call[in.Obj, in.Obj]("fsm", "sendOne", in.Obj{"instance": "1"}, nil),
in.FileContains(logFilePath, "end 1"),
fsmInState("1", "completed", "fsm.end"),

in.Fail(in.Call("fsm", "sendOne", in.Obj{"instance": "1"}, nil),
in.Fail(in.Call[in.Obj, in.Obj]("fsm", "sendOne", in.Obj{"instance": "1"}, nil),
"FSM instance 1 is already in state fsm.end"),

// Invalid state transition
in.Fail(in.Call("fsm", "sendTwo", in.Obj{"instance": "2"}, nil),
in.Fail(in.Call[in.Obj, in.Obj]("fsm", "sendTwo", in.Obj{"instance": "2"}, nil),
"invalid state transition"),

in.Call("fsm", "sendOne", in.Obj{"instance": "2"}, nil),
in.Call[in.Obj, in.Obj]("fsm", "sendOne", in.Obj{"instance": "2"}, nil),
in.FileContains(logFilePath, "middle 2"),
fsmInState("2", "running", "fsm.middle"),

// Invalid state transition
in.Fail(in.Call("fsm", "sendTwo", in.Obj{"instance": "2"}, nil),
in.Fail(in.Call[in.Obj, in.Obj]("fsm", "sendTwo", in.Obj{"instance": "2"}, nil),
"invalid state transition"),
)
}
Expand Down Expand Up @@ -86,14 +86,14 @@ func TestFSMRetry(t *testing.T) {
in.Build("fsmretry"),
in.Deploy("fsmretry"),
// start 2 FSM instances
in.Call("fsmretry", "start", in.Obj{"id": "1"}, func(t testing.TB, response in.Obj) {}),
in.Call("fsmretry", "start", in.Obj{"id": "2"}, func(t testing.TB, response in.Obj) {}),
in.Call("fsmretry", "start", in.Obj{"id": "1"}, func(t testing.TB, response any) {}),
in.Call("fsmretry", "start", in.Obj{"id": "2"}, func(t testing.TB, response any) {}),

in.Sleep(2*time.Second),

// transition the FSM, should fail each time.
in.Call("fsmretry", "startTransitionToTwo", in.Obj{"id": "1"}, func(t testing.TB, response in.Obj) {}),
in.Call("fsmretry", "startTransitionToThree", in.Obj{"id": "2"}, func(t testing.TB, response in.Obj) {}),
in.Call("fsmretry", "startTransitionToTwo", in.Obj{"id": "1"}, func(t testing.TB, response any) {}),
in.Call("fsmretry", "startTransitionToThree", in.Obj{"id": "2"}, func(t testing.TB, response any) {}),

in.Sleep(8*time.Second), //5s is longest run of retries

Expand Down
2 changes: 1 addition & 1 deletion backend/controller/sql/database_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func TestDatabase(t *testing.T) {
in.CopyModule("database"),
in.CreateDBAction("database", "testdb", false),
in.Deploy("database"),
in.Call("database", "insert", in.Obj{"data": "hello"}, nil),
in.Call[in.Obj, in.Obj]("database", "insert", in.Obj{"data": "hello"}, nil),
in.QueryRow("testdb", "SELECT data FROM requests", "hello"),

// run tests which should only affect "testdb_test"
Expand Down
2 changes: 1 addition & 1 deletion cmd/ftl/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestBox(t *testing.T) {
CopyModule("echo"),
Exec("ftl", "box", "echo", "--compose=echo-compose.yml"),
Exec("docker", "compose", "-f", "echo-compose.yml", "up", "--wait"),
Call("echo", "echo", Obj{"name": "Alice"}, nil),
Call[Obj, Obj]("echo", "echo", Obj{"name": "Alice"}, nil),
Exec("docker", "compose", "-f", "echo-compose.yml", "down", "--rmi", "local"),
)
}
Expand Down
13 changes: 13 additions & 0 deletions go-runtime/compile/compile_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package compile_test

import (
"testing"
"time"

"github.com/alecthomas/assert/v2"

Expand Down Expand Up @@ -47,3 +48,15 @@ func TestNonFTLTypes(t *testing.T) {
}),
)
}

func TestNonStructRequestResponse(t *testing.T) {
in.Run(t, "",
in.CopyModule("two"),
in.Deploy("two"),
in.CopyModule("one"),
in.Deploy("one"),
in.Call("one", "stringToTime", "1985-04-12T23:20:50.52Z", func(t testing.TB, response time.Time) {
assert.Equal(t, time.Date(1985, 04, 12, 23, 20, 50, 520_000_000, time.UTC), response)
}),
)
}
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module ftl/one

go 1.22.2

replace github.com/TBD54566975/ftl => ../../../..
replace github.com/TBD54566975/ftl => ../../../../..

require github.com/TBD54566975/ftl v0.150.3

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package one

import (
"context"
"errors"
"time"

"ftl/builtin"
Expand Down Expand Up @@ -186,10 +185,18 @@ func (NonFTLStruct) NonFTLInterface() {}

//ftl:verb
func StringToTime(ctx context.Context, input string) (time.Time, error) {
return time.Time{}, errors.New("not implemented")
return time.Parse(time.RFC3339, input)
}

//ftl:verb
func BatchStringToTime(ctx context.Context, input []string) ([]time.Time, error) {
return nil, errors.New("not implemented")
var output = []time.Time{}
for _, s := range input {
t, err := time.Parse(time.RFC3339, s)
if err != nil {
return nil, err
}
output = append(output, t)
}
return output, nil
}
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module ftl/two

go 1.22.2

replace github.com/TBD54566975/ftl => ../../../..
replace github.com/TBD54566975/ftl => ../../../../..

require github.com/TBD54566975/ftl v0.150.3

Expand Down
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion go-runtime/internal/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestRealMap(t *testing.T) {
Call("mapper", "get", Obj{}, func(t testing.TB, response Obj) {
assert.Equal(t, Obj{"underlyingCounter": 2.0, "mapCounter": 1.0, "mapped": "0"}, response)
}),
Call("mapper", "inc", Obj{}, nil),
Call[Obj, Obj]("mapper", "inc", Obj{}, nil),
Call("mapper", "get", Obj{}, func(t testing.TB, response Obj) {
assert.Equal(t, Obj{"underlyingCounter": 3.0, "mapCounter": 2.0, "mapped": "1"}, response)
}),
Expand Down
6 changes: 4 additions & 2 deletions integration/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"strings"
"testing"
"time"
"unicode"

"connectrpc.com/connect"
"github.com/alecthomas/assert/v2"
Expand Down Expand Up @@ -284,17 +285,18 @@ type Obj map[string]any
// Call a verb.
//
// "check" may be nil
func Call(module, verb string, request Obj, check func(t testing.TB, response Obj)) Action {
func Call[Req any, Resp any](module, verb string, request Req, check func(t testing.TB, response Resp)) Action {
return func(t testing.TB, ic TestContext) {
Infof("Calling %s.%s", module, verb)
assert.False(t, unicode.IsUpper([]rune(verb)[0]), "verb %q must start with an lowercase letter", verb)
data, err := json.Marshal(request)
assert.NoError(t, err)
resp, err := ic.Verbs.Call(ic, connect.NewRequest(&ftlv1.CallRequest{
Verb: &schemapb.Ref{Module: module, Name: verb},
Body: data,
}))
assert.NoError(t, err)
var response Obj
var response Resp
assert.Zero(t, resp.Msg.GetError(), "verb failed: %s", resp.Msg.GetError().GetMessage())
err = json.Unmarshal(resp.Msg.GetBody(), &response)
assert.NoError(t, err)
Expand Down

0 comments on commit 7f0c29a

Please sign in to comment.