generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
migrating integration tests to their logical packages
- Loading branch information
1 parent
fba50cf
commit d7075cd
Showing
99 changed files
with
700 additions
and
315 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
File renamed without changes.
File renamed without changes.
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
File renamed without changes.
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,117 @@ | ||
//go:build integration | ||
|
||
package dal_test | ||
|
||
import ( | ||
"fmt" | ||
"path/filepath" | ||
"testing" | ||
"time" | ||
|
||
in "github.com/TBD54566975/ftl/integration" | ||
"github.com/alecthomas/assert/v2" | ||
) | ||
|
||
func TestFSM(t *testing.T) { | ||
logFilePath := filepath.Join(t.TempDir(), "fsm.log") | ||
t.Setenv("FSM_LOG_FILE", logFilePath) | ||
fsmInState := func(instance, status, state string) in.Action { | ||
return in.QueryRow("ftl", fmt.Sprintf(` | ||
SELECT status, current_state | ||
FROM fsm_instances | ||
WHERE fsm = 'fsm.fsm' AND key = '%s' | ||
`, instance), status, state) | ||
} | ||
in.Run(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.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.FileContains(logFilePath, "middle 1"), | ||
fsmInState("1", "running", "fsm.middle"), | ||
|
||
in.Call("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), | ||
"FSM instance 1 is already in state fsm.end"), | ||
|
||
// Invalid state transition | ||
in.Fail(in.Call("fsm", "sendTwo", in.Obj{"instance": "2"}, nil), | ||
"invalid state transition"), | ||
|
||
in.Call("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), | ||
"invalid state transition"), | ||
) | ||
} | ||
|
||
func TestFSMRetry(t *testing.T) { | ||
checkRetries := func(origin, verb string, delays []time.Duration) in.Action { | ||
return func(t testing.TB, ic in.TestContext) { | ||
results := []any{} | ||
for i := 0; i < len(delays); i++ { | ||
values := in.GetRow(t, ic, "ftl", fmt.Sprintf("SELECT scheduled_at FROM async_calls WHERE origin = '%s' AND verb = '%s' AND state = 'error' ORDER BY created_at LIMIT 1 OFFSET %d", origin, verb, i), 1) | ||
results = append(results, values[0]) | ||
} | ||
times := []time.Time{} | ||
for i, r := range results { | ||
ts, ok := r.(time.Time) | ||
assert.True(t, ok, "unexpected time value: %v", r) | ||
times = append(times, ts) | ||
if i > 0 { | ||
delay := times[i].Sub(times[i-1]) | ||
targetDelay := delays[i-1] | ||
assert.True(t, delay >= targetDelay && delay < time.Second+targetDelay, "unexpected time diff for %s retry %d: %v (expected %v - %v)", origin, i, delay, targetDelay, time.Second+targetDelay) | ||
} | ||
} | ||
} | ||
} | ||
|
||
in.Run(t, "", | ||
in.CopyModule("fsmretry"), | ||
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.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.Sleep(8*time.Second), //6s is longest run of retries | ||
|
||
// both FSMs instances should have failed | ||
in.QueryRow("ftl", "SELECT COUNT(*) FROM fsm_instances WHERE status = 'failed'", int64(2)), | ||
|
||
in.QueryRow("ftl", fmt.Sprintf("SELECT COUNT(*) FROM async_calls WHERE origin = '%s' AND verb = '%s'", "fsm:fsmretry.fsm:1", "fsmretry.state2"), int64(4)), | ||
checkRetries("fsm:fsmretry.fsm:1", "fsmretry.state2", []time.Duration{time.Second, time.Second, time.Second}), | ||
in.QueryRow("ftl", fmt.Sprintf("SELECT COUNT(*) FROM async_calls WHERE origin = '%s' AND verb = '%s'", "fsm:fsmretry.fsm:2", "fsmretry.state3"), int64(4)), | ||
checkRetries("fsm:fsmretry.fsm:2", "fsmretry.state3", []time.Duration{time.Second, 2 * time.Second, 3 * time.Second}), | ||
) | ||
} | ||
|
||
func TestFSMGoTests(t *testing.T) { | ||
logFilePath := filepath.Join(t.TempDir(), "fsm.log") | ||
t.Setenv("FSM_LOG_FILE", logFilePath) | ||
in.Run(t, "", | ||
in.CopyModule("fsm"), | ||
in.Build("fsm"), | ||
in.ExecModuleTest("fsm"), | ||
) | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
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
File renamed without changes.
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,59 @@ | ||
//go:build integration | ||
|
||
package leases_test | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
"testing" | ||
"time" | ||
|
||
"connectrpc.com/connect" | ||
"github.com/alecthomas/assert/v2" | ||
"golang.org/x/sync/errgroup" | ||
|
||
ftlv1 "github.com/TBD54566975/ftl/backend/protos/xyz/block/ftl/v1" | ||
schemapb "github.com/TBD54566975/ftl/backend/protos/xyz/block/ftl/v1/schema" | ||
in "github.com/TBD54566975/ftl/integration" | ||
) | ||
|
||
func TestLease(t *testing.T) { | ||
in.Run(t, "", | ||
in.CopyModule("leases"), | ||
in.Build("leases"), | ||
// checks if leases work in a unit test environment | ||
in.ExecModuleTest("leases"), | ||
in.Deploy("leases"), | ||
// checks if it leases work with a real controller | ||
func(t testing.TB, ic in.TestContext) { | ||
// Start a lease. | ||
wg := errgroup.Group{} | ||
wg.Go(func() error { | ||
in.Infof("Acquiring lease") | ||
resp, err := ic.Verbs.Call(ic, connect.NewRequest(&ftlv1.CallRequest{ | ||
Verb: &schemapb.Ref{Module: "leases", Name: "acquire"}, | ||
Body: []byte("{}"), | ||
})) | ||
if respErr := resp.Msg.GetError(); respErr != nil { | ||
return fmt.Errorf("received error on first call: %v", respErr) | ||
} | ||
return err | ||
}) | ||
|
||
time.Sleep(time.Second) | ||
|
||
in.Infof("Trying to acquire lease again") | ||
// Trying to obtain the lease again should fail. | ||
resp, err := ic.Verbs.Call(ic, connect.NewRequest(&ftlv1.CallRequest{ | ||
Verb: &schemapb.Ref{Module: "leases", Name: "acquire"}, | ||
Body: []byte("{}"), | ||
})) | ||
assert.NoError(t, err) | ||
if resp.Msg.GetError() == nil || !strings.Contains(resp.Msg.GetError().Message, "could not acquire lease") { | ||
t.Fatalf("expected error but got: %#v", resp.Msg.GetError()) | ||
} | ||
err = wg.Wait() | ||
assert.NoError(t, err) | ||
}, | ||
) | ||
} |
File renamed without changes.
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
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,25 @@ | ||
//go:build integration | ||
|
||
package sql_test | ||
|
||
import ( | ||
"testing" | ||
|
||
in "github.com/TBD54566975/ftl/integration" | ||
) | ||
|
||
func TestDatabase(t *testing.T) { | ||
in.Run(t, "database/ftl-project.toml", | ||
// deploy real module against "testdb" | ||
in.CopyModule("database"), | ||
in.CreateDBAction("database", "testdb", false), | ||
in.Deploy("database"), | ||
in.Call("database", "insert", in.Obj{"data": "hello"}, nil), | ||
in.QueryRow("testdb", "SELECT data FROM requests", "hello"), | ||
|
||
// run tests which should only affect "testdb_test" | ||
in.CreateDBAction("database", "testdb", true), | ||
in.ExecModuleTest("database"), | ||
in.QueryRow("testdb", "SELECT data FROM requests", "hello"), | ||
) | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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
File renamed without changes.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,12 +1,18 @@ | ||
module-dirs = ["examples/go"] | ||
external-dirs = [] | ||
ftl-min-version = "" | ||
|
||
[global] | ||
[global.configuration] | ||
key = "inline://InZhbHVlIg" | ||
|
||
[modules] | ||
[modules.echo] | ||
[modules.echo.configuration] | ||
default = "inline://ImFub255bW91cyI" | ||
|
||
[executables] | ||
ftl = "" | ||
|
||
[commands] | ||
startup = ["echo 'FTL startup command ⚡️'"] |
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,35 @@ | ||
//go:build integration | ||
|
||
package compile_test | ||
|
||
import ( | ||
"testing" | ||
|
||
in "github.com/TBD54566975/ftl/integration" | ||
) | ||
|
||
func TestNonExportedDecls(t *testing.T) { | ||
in.Run(t, "", | ||
in.CopyModule("time"), | ||
in.Deploy("time"), | ||
in.CopyModule("echo"), | ||
in.Deploy("echo"), | ||
in.CopyModule("notexportedverb"), | ||
in.ExpectError( | ||
in.ExecWithOutput("ftl", "deploy", "notexportedverb"), | ||
"call first argument must be a function but is an unresolved reference to echo.Echo, does it need to be exported?"), | ||
) | ||
} | ||
|
||
func TestUndefinedExportedDecls(t *testing.T) { | ||
in.Run(t, "", | ||
in.CopyModule("time"), | ||
in.Deploy("time"), | ||
in.CopyModule("echo"), | ||
in.Deploy("echo"), | ||
in.CopyModule("undefinedverb"), | ||
in.ExpectError( | ||
in.ExecWithOutput("ftl", "deploy", "undefinedverb"), | ||
"call first argument must be a function but is an unresolved reference to echo.Undefined"), | ||
) | ||
} |
Oops, something went wrong.