2022-04-15 15:20:17 +03:00
|
|
|
package fsm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestFSMStart(t *testing.T) {
|
|
|
|
ctx := context.TODO()
|
|
|
|
buf := bytes.NewBuffer(nil)
|
2022-04-19 17:33:33 +03:00
|
|
|
pfb := func(_ context.Context, state string, _ interface{}) {
|
2022-04-15 15:20:17 +03:00
|
|
|
fmt.Fprintf(buf, "before state %s\n", state)
|
|
|
|
}
|
2022-04-19 17:33:33 +03:00
|
|
|
pfa := func(_ context.Context, state string, _ interface{}) {
|
2022-04-15 15:20:17 +03:00
|
|
|
fmt.Fprintf(buf, "after state %s\n", state)
|
|
|
|
}
|
2022-04-19 18:42:30 +03:00
|
|
|
f := New(InitialState("1"), HookBefore(pfb), HookAfter(pfa))
|
|
|
|
f1 := func(_ context.Context, req interface{}, _ ...StateOption) (string, interface{}, error) {
|
2022-04-19 17:33:33 +03:00
|
|
|
args := req.(map[string]interface{})
|
2022-04-15 15:20:17 +03:00
|
|
|
if v, ok := args["request"].(string); !ok || v == "" {
|
|
|
|
return "", nil, fmt.Errorf("empty request")
|
|
|
|
}
|
|
|
|
return "2", map[string]interface{}{"response": "test2"}, nil
|
|
|
|
}
|
2022-04-19 18:42:30 +03:00
|
|
|
f2 := func(_ context.Context, req interface{}, _ ...StateOption) (string, interface{}, error) {
|
2022-04-19 17:33:33 +03:00
|
|
|
args := req.(map[string]interface{})
|
2022-04-15 15:20:17 +03:00
|
|
|
if v, ok := args["response"].(string); !ok || v == "" {
|
|
|
|
return "", nil, fmt.Errorf("empty response")
|
|
|
|
}
|
|
|
|
return "", map[string]interface{}{"response": "test"}, nil
|
|
|
|
}
|
2022-04-19 18:42:30 +03:00
|
|
|
f3 := func(_ context.Context, req interface{}, _ ...StateOption) (string, interface{}, error) {
|
2022-04-19 17:33:33 +03:00
|
|
|
args := req.(map[string]interface{})
|
|
|
|
if v, ok := args["response"].(string); !ok || v == "" {
|
|
|
|
return "", nil, fmt.Errorf("empty response")
|
|
|
|
}
|
|
|
|
return StateEnd, map[string]interface{}{"response": "test_last"}, nil
|
|
|
|
}
|
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)
|
|
|
|
rsp, err := f.Start(ctx, map[string]interface{}{"request": "test1"})
|
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)
|
2022-04-19 17:33:33 +03:00
|
|
|
} else if v != "test_last" {
|
2022-04-15 15:20:17 +03:00
|
|
|
t.Fatalf("invalid rsp %#+v", args)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !bytes.Contains(buf.Bytes(), []byte(`before state 1`)) ||
|
|
|
|
!bytes.Contains(buf.Bytes(), []byte(`before state 2`)) ||
|
|
|
|
!bytes.Contains(buf.Bytes(), []byte(`after state 1`)) ||
|
2022-04-19 17:33:33 +03:00
|
|
|
!bytes.Contains(buf.Bytes(), []byte(`after state 2`)) ||
|
|
|
|
!bytes.Contains(buf.Bytes(), []byte(`after state 3`)) ||
|
|
|
|
!bytes.Contains(buf.Bytes(), []byte(`after state 3`)) {
|
2022-04-15 15:20:17 +03:00
|
|
|
t.Fatalf("fsm not works properly or hooks error, buf: %s", buf.Bytes())
|
|
|
|
}
|
|
|
|
}
|