2021-06-30 17:53:06 +03:00
|
|
|
package flow
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-07-16 00:41:10 +03:00
|
|
|
"path/filepath"
|
2021-06-30 17:53:06 +03:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2021-07-14 11:48:20 +03:00
|
|
|
httpcli "github.com/unistack-org/micro-client-http/v3"
|
2021-07-16 00:41:10 +03:00
|
|
|
jsoncodec "github.com/unistack-org/micro-codec-json/v3"
|
2021-07-14 11:48:20 +03:00
|
|
|
jsonpbcodec "github.com/unistack-org/micro-codec-jsonpb/v3"
|
|
|
|
httpsrv "github.com/unistack-org/micro-server-http/v3"
|
|
|
|
pb "github.com/unistack-org/micro-tests/flow/proto"
|
|
|
|
"github.com/unistack-org/micro/v3"
|
2021-06-30 17:53:06 +03:00
|
|
|
"github.com/unistack-org/micro/v3/client"
|
|
|
|
"github.com/unistack-org/micro/v3/flow"
|
|
|
|
"github.com/unistack-org/micro/v3/logger"
|
|
|
|
"github.com/unistack-org/micro/v3/meter"
|
2021-07-14 11:48:20 +03:00
|
|
|
"github.com/unistack-org/micro/v3/server"
|
2021-06-30 17:53:06 +03:00
|
|
|
"github.com/unistack-org/micro/v3/store"
|
|
|
|
"github.com/unistack-org/micro/v3/tracer"
|
|
|
|
)
|
|
|
|
|
2021-07-14 11:48:20 +03:00
|
|
|
type handler struct{}
|
|
|
|
|
2021-07-16 00:41:10 +03:00
|
|
|
func (h *handler) DeleteUser(ctx context.Context, req *pb.DeleteUserReq, rsp *pb.DeleteUserRsp) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *handler) UpdateUser(ctx context.Context, req *pb.UpdateUserReq, rsp *pb.UpdateUserRsp) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *handler) MailUser(ctx context.Context, req *pb.MailUserReq, rsp *pb.MailUserRsp) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-07-14 11:48:20 +03:00
|
|
|
func (h *handler) LookupUser(ctx context.Context, req *pb.LookupUserReq, rsp *pb.LookupUserRsp) error {
|
2021-07-16 00:41:10 +03:00
|
|
|
rsp.Birthday = "31.07.1986"
|
2021-07-14 11:48:20 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-06-30 17:53:06 +03:00
|
|
|
func TestFlow(t *testing.T) {
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
2021-07-16 00:41:10 +03:00
|
|
|
logger.DefaultLogger = logger.NewLogger(logger.WithLevel(logger.DebugLevel))
|
2021-07-14 11:48:20 +03:00
|
|
|
|
2021-06-30 17:53:06 +03:00
|
|
|
s := store.DefaultStore
|
2021-07-16 00:41:10 +03:00
|
|
|
if err := s.Init(store.Codec(jsoncodec.NewCodec())); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := s.Connect(ctx); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2021-07-14 11:48:20 +03:00
|
|
|
c := client.NewClientCallOptions(
|
|
|
|
httpcli.NewClient(
|
|
|
|
client.ContentType("application/json"),
|
|
|
|
client.Codec("application/json", jsonpbcodec.NewCodec()),
|
|
|
|
),
|
|
|
|
client.WithAddress("http://127.0.0.1:7989"),
|
|
|
|
)
|
2021-06-30 17:53:06 +03:00
|
|
|
m := meter.DefaultMeter
|
|
|
|
tr := tracer.DefaultTracer
|
|
|
|
l := logger.DefaultLogger
|
|
|
|
|
|
|
|
f := flow.NewFlow(flow.Context(ctx), flow.Store(s), flow.Client(c), flow.Meter(m), flow.Tracer(tr), flow.Logger(l))
|
|
|
|
|
|
|
|
if err := f.Init(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2021-07-14 11:48:20 +03:00
|
|
|
options := append([]micro.Option{},
|
|
|
|
micro.Server(
|
|
|
|
httpsrv.NewServer(
|
|
|
|
server.Codec("application/json", jsonpbcodec.NewCodec()),
|
|
|
|
server.Address("127.0.0.1:7989"),
|
2021-07-16 00:41:10 +03:00
|
|
|
httpsrv.RegisterRPCHandler(true),
|
2021-07-14 11:48:20 +03:00
|
|
|
),
|
|
|
|
),
|
|
|
|
micro.Context(ctx),
|
|
|
|
)
|
|
|
|
|
|
|
|
svc := micro.NewService(options...)
|
|
|
|
|
|
|
|
if err := svc.Init(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
h := &handler{}
|
|
|
|
|
|
|
|
if err := pb.RegisterTestServiceServer(svc.Server(), h); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
if err := svc.Run(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
time.Sleep(2 * time.Second)
|
2021-06-30 17:53:06 +03:00
|
|
|
steps := []flow.Step{
|
2021-07-14 11:48:20 +03:00
|
|
|
flow.NewCallStep("test", pb.TestServiceName, "LookupUser", flow.StepID("test.TestService.LookupUser")),
|
|
|
|
flow.NewCallStep("test", pb.TestServiceName, "UpdateUser", flow.StepRequires("test.TestService.LookupUser")),
|
2021-07-16 00:41:10 +03:00
|
|
|
flow.NewCallStep("test", pb.TestServiceName, "DeleteUser", flow.StepRequires("test.TestService.UpdateUser")),
|
2021-07-14 11:48:20 +03:00
|
|
|
flow.NewCallStep("test", pb.TestServiceName, "MailUser", flow.StepRequires("test.TestService.UpdateUser")),
|
2021-06-30 17:53:06 +03:00
|
|
|
}
|
|
|
|
w, err := f.WorkflowCreate(ctx, "test", steps...)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2021-07-16 00:41:10 +03:00
|
|
|
req, err := jsonpbcodec.NewCodec().Marshal(&pb.LookupUserReq{Name: "vtolstov"})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
id, err := w.Execute(ctx, &flow.Message{Body: req}, flow.ExecuteTimeout(2*time.Second))
|
|
|
|
keys, err := s.List(ctx)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
keys, err = store.NewNamespaceStore(s, filepath.Join("workflows", id)).List(ctx)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
keys, err = store.NewNamespaceStore(s, filepath.Join("steps", id)).List(ctx)
|
2021-06-30 17:53:06 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2021-07-16 00:41:10 +03:00
|
|
|
_ = keys
|
|
|
|
t.Logf("execution id: %s, result: %v", id, err)
|
2021-06-30 17:53:06 +03:00
|
|
|
|
|
|
|
}
|