Skip to content

Commit

Permalink
add ftltest support for Next()
Browse files Browse the repository at this point in the history
  • Loading branch information
matt2e committed Aug 13, 2024
1 parent 7b68b92 commit 7b73b49
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 28 deletions.
36 changes: 36 additions & 0 deletions backend/controller/dal/testdata/go/fsmnext/fsmnext_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package fsmnext

import (
"testing"

"github.com/TBD54566975/ftl/go-runtime/ftl"
"github.com/TBD54566975/ftl/go-runtime/ftl/ftltest"
"github.com/alecthomas/assert/v2"
)

func TestProgression(t *testing.T) {
// Simple progression through each state
ctx := ftltest.Context()

assert.NoError(t, SendOne(ctx, Request{
State: A,
Event: Event{
Instance: "1",
NextStates: []State{B, C, D},
},
}))
}

func TestDoubleNext(t *testing.T) {
// Bad progression where fsm.Next() is called twice
ctx := ftltest.Context()

assert.Contains(t, SendOne(ctx, Request{
State: A,
Event: Event{
Instance: "1",
NextStates: []State{B},
NextAttempts: ftl.Some(2),
},
}).Error(), `fsm "fsm" instance "1" already has a pending event`)
}
31 changes: 24 additions & 7 deletions backend/controller/dal/testdata/go/fsmnext/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,62 @@ module ftl/fsmnext

go 1.22.5

require github.com/TBD54566975/ftl v1.1.5
require (
github.com/TBD54566975/ftl v1.1.5
github.com/alecthomas/assert/v2 v2.10.0
)

require (
connectrpc.com/connect v1.16.2 // indirect
connectrpc.com/grpcreflect v1.2.0 // indirect
connectrpc.com/otelconnect v0.7.1 // indirect
github.com/BurntSushi/toml v1.4.0 // indirect
github.com/TBD54566975/scaffolder v1.0.0 // indirect
github.com/alecthomas/atomic v0.1.0-alpha2 // indirect
github.com/alecthomas/concurrency v0.0.2 // indirect
github.com/alecthomas/participle/v2 v2.1.1 // indirect
github.com/alecthomas/repr v0.4.0 // indirect
github.com/alecthomas/types v0.16.0 // indirect
github.com/alessio/shellescape v1.4.2 // indirect
github.com/amacneil/dbmate/v2 v2.19.0 // indirect
github.com/aws/aws-sdk-go-v2 v1.30.3 // indirect
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.15 // indirect
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.15 // indirect
github.com/aws/aws-sdk-go-v2/service/secretsmanager v1.32.4 // indirect
github.com/aws/smithy-go v1.20.3 // indirect
github.com/benbjohnson/clock v1.3.5 // indirect
github.com/danieljoos/wincred v1.2.0 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/godbus/dbus/v5 v5.1.0 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/hashicorp/cronexpr v1.1.2 // indirect
github.com/hexops/gotextdiff v1.0.3 // indirect
github.com/jackc/pgerrcode v0.0.0-20240316143900-6e2875d9b438 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jackc/pgx/v5 v5.6.0 // indirect
github.com/jackc/puddle/v2 v2.2.1 // indirect
github.com/jpillora/backoff v1.0.0 // indirect
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
github.com/lib/pq v1.10.9 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/multiformats/go-base36 v0.2.0 // indirect
github.com/puzpuzpuz/xsync/v3 v3.4.0 // indirect
github.com/sqlc-dev/pqtype v0.3.0 // indirect
github.com/swaggest/jsonschema-go v0.3.72 // indirect
github.com/swaggest/refl v1.3.0 // indirect
github.com/zalando/go-keyring v0.2.5 // indirect
go.opentelemetry.io/otel v1.28.0 // indirect
go.opentelemetry.io/otel/metric v1.28.0 // indirect
go.opentelemetry.io/otel/trace v1.28.0 // indirect
golang.org/x/crypto v0.25.0 // indirect
golang.org/x/crypto v0.26.0 // indirect
golang.org/x/exp v0.0.0-20240707233637-46b078467d37 // indirect
golang.org/x/mod v0.19.0 // indirect
golang.org/x/net v0.27.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.22.0 // indirect
golang.org/x/text v0.16.0 // indirect
golang.org/x/mod v0.20.0 // indirect
golang.org/x/net v0.28.0 // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/sys v0.24.0 // indirect
golang.org/x/text v0.17.0 // indirect
google.golang.org/protobuf v1.34.2 // indirect
)

Expand Down
74 changes: 60 additions & 14 deletions backend/controller/dal/testdata/go/fsmnext/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 29 additions & 7 deletions go-runtime/ftl/ftltest/fsm.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import (
"fmt"
"reflect"

"github.com/TBD54566975/ftl/go-runtime/ftl"
"github.com/TBD54566975/ftl/go-runtime/ftl/reflection"
)

type fakeFSMInstance struct {
name string
terminated bool
state reflect.Value
next ftl.Option[any]
}

func newFakeFSMManager() *fakeFSMManager {
Expand Down Expand Up @@ -66,14 +68,17 @@ func (f *fakeFSMManager) SendEvent(ctx context.Context, fsm string, instance str
}

out := transition.To.Call([]reflect.Value{reflect.ValueOf(ctx), reflect.ValueOf(event)})
var err error
erri := out[0]
if erri.IsNil() {
fsmInstance.state = transition.To
} else {
err = erri.Interface().(error) //nolint:forcetypeassert
if !erri.IsNil() {
err := erri.Interface().(error) //nolint:forcetypeassert
fsmInstance.state = reflect.Value{}
fsmInstance.next = ftl.None[any]()
fsmInstance.terminated = true
return err
}

fsmInstance.state = transition.To

currentStateRef := reflection.FuncRef(fsmInstance.state.Interface()).ToSchema()

// Flag the FSM instance as terminated if the current state is a terminal state.
Expand All @@ -83,9 +88,26 @@ func (f *fakeFSMManager) SendEvent(ctx context.Context, fsm string, instance str
break
}
}
return err

if next, ok := fsmInstance.next.Get(); ok {
fsmInstance.next = ftl.None[any]()
return f.SendEvent(ctx, fsm, instance, next)
}
return nil
}

func (f *fakeFSMManager) SetNextFSMEvent(ctx context.Context, fsm string, instance string, event any) error {
panic("implement me")
key := fsmInstanceKey{fsm, instance}
fsmInstance, ok := f.instances[key]
if !ok {
return fmt.Errorf("fsm %q instance %q not found", fsm, instance)
}
if fsmInstance.terminated {
return fmt.Errorf("fsm %q instance %q is terminated", fsm, instance)
}
if _, ok := fsmInstance.next.Get(); ok {
return fmt.Errorf("fsm %q instance %q already has a pending event", fsm, instance)
}
fsmInstance.next = ftl.Some(event)
return nil
}

0 comments on commit 7b73b49

Please sign in to comment.