2022-04-15 15:20:17 +03:00
|
|
|
package fsm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
2023-01-30 00:17:29 +03:00
|
|
|
|
2023-04-11 22:20:37 +03:00
|
|
|
"go.unistack.org/micro/v4/logger"
|
2022-04-15 15:20:17 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestFSMStart(t *testing.T) {
|
|
|
|
ctx := context.TODO()
|
2023-01-30 00:17:29 +03:00
|
|
|
|
|
|
|
if err := logger.DefaultLogger.Init(); err != nil {
|
|
|
|
t.Fatal(err)
|
2022-04-15 15:20:17 +03:00
|
|
|
}
|
2023-01-30 00:17:29 +03:00
|
|
|
|
|
|
|
wrapper := func(next StateFunc) StateFunc {
|
|
|
|
return func(sctx context.Context, s State, opts ...StateOption) (State, error) {
|
|
|
|
sctx = logger.NewContext(sctx, logger.Fields("state", s.Name()))
|
|
|
|
return next(sctx, s, opts...)
|
|
|
|
}
|
2022-04-15 15:20:17 +03:00
|
|
|
}
|
2023-01-30 00:17:29 +03:00
|
|
|
|
|
|
|
f := NewFSM(InitialState("1"), WrapState(wrapper))
|
|
|
|
f1 := func(sctx context.Context, s State, opts ...StateOption) (State, error) {
|
|
|
|
_, ok := logger.FromContext(sctx)
|
|
|
|
if !ok {
|
|
|
|
t.Fatal("f1 context does not have logger")
|
|
|
|
}
|
|
|
|
args := s.Body().(map[string]interface{})
|
2022-04-15 15:20:17 +03:00
|
|
|
if v, ok := args["request"].(string); !ok || v == "" {
|
2023-01-30 00:17:29 +03:00
|
|
|
return nil, fmt.Errorf("empty request")
|
2022-04-15 15:20:17 +03:00
|
|
|
}
|
2023-01-30 00:17:29 +03:00
|
|
|
return &state{name: "", body: map[string]interface{}{"response": "state1"}}, nil
|
2022-04-15 15:20:17 +03:00
|
|
|
}
|
2023-01-30 00:17:29 +03:00
|
|
|
f2 := func(sctx context.Context, s State, opts ...StateOption) (State, error) {
|
|
|
|
_, ok := logger.FromContext(sctx)
|
|
|
|
if !ok {
|
|
|
|
t.Fatal("f2 context does not have logger")
|
|
|
|
}
|
|
|
|
args := s.Body().(map[string]interface{})
|
2022-04-15 15:20:17 +03:00
|
|
|
if v, ok := args["response"].(string); !ok || v == "" {
|
2023-01-30 00:17:29 +03:00
|
|
|
return nil, fmt.Errorf("empty response")
|
2022-04-15 15:20:17 +03:00
|
|
|
}
|
2023-01-30 00:17:29 +03:00
|
|
|
return &state{name: "", body: map[string]interface{}{"response": "state2"}}, nil
|
2022-04-15 15:20:17 +03:00
|
|
|
}
|
2023-01-30 00:17:29 +03:00
|
|
|
f3 := func(sctx context.Context, s State, opts ...StateOption) (State, error) {
|
|
|
|
_, ok := logger.FromContext(sctx)
|
|
|
|
if !ok {
|
|
|
|
t.Fatal("f3 context does not have logger")
|
|
|
|
}
|
|
|
|
args := s.Body().(map[string]interface{})
|
2022-04-19 17:33:33 +03:00
|
|
|
if v, ok := args["response"].(string); !ok || v == "" {
|
2023-01-30 00:17:29 +03:00
|
|
|
return nil, fmt.Errorf("empty response")
|
2022-04-19 17:33:33 +03:00
|
|
|
}
|
2023-01-30 00:17:29 +03:00
|
|
|
return &state{name: StateEnd, body: map[string]interface{}{"response": "state3"}}, nil
|
2022-04-19 17:33:33 +03:00
|
|
|
}
|
2022-04-15 15:20:17 +03:00
|
|
|
f.State("1", f1)
|
|
|
|
f.State("2", f2)
|
2022-04-19 17:33:33 +03:00
|
|
|
f.State("3", f3)
|
2023-01-30 00:17:29 +03:00
|
|
|
rsp, err := f.Start(ctx, map[string]interface{}{"request": "state"})
|
2022-04-15 15:20:17 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
2022-04-19 17:33:33 +03:00
|
|
|
}
|
|
|
|
args := rsp.(map[string]interface{})
|
|
|
|
if v, ok := args["response"].(string); !ok || v == "" {
|
2022-04-15 15:20:17 +03:00
|
|
|
t.Fatalf("nil rsp: %#+v", args)
|
2023-01-30 00:17:29 +03:00
|
|
|
} else if v != "state3" {
|
2022-04-15 15:20:17 +03:00
|
|
|
t.Fatalf("invalid rsp %#+v", args)
|
|
|
|
}
|
|
|
|
}
|