diff --git a/flow/flow_test.go b/flow/flow_test.go index b9d2a1d..3410659 100644 --- a/flow/flow_test.go +++ b/flow/flow_test.go @@ -5,21 +5,40 @@ import ( "testing" "time" - pb "github.com/unistack-org/micro-tests/client/http/proto" + httpcli "github.com/unistack-org/micro-client-http/v3" + 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" "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" + "github.com/unistack-org/micro/v3/server" "github.com/unistack-org/micro/v3/store" "github.com/unistack-org/micro/v3/tracer" ) +type handler struct{} + +func (h *handler) LookupUser(ctx context.Context, req *pb.LookupUserReq, rsp *pb.LookupUserRsp) error { + return nil +} + func TestFlow(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) defer cancel() + logger.DefaultLogger = logger.NewLogger(logger.WithLevel(logger.TraceLevel)) + s := store.DefaultStore - c := client.DefaultClient + c := client.NewClientCallOptions( + httpcli.NewClient( + client.ContentType("application/json"), + client.Codec("application/json", jsonpbcodec.NewCodec()), + ), + client.WithAddress("http://127.0.0.1:7989"), + ) m := meter.DefaultMeter tr := tracer.DefaultTracer l := logger.DefaultLogger @@ -30,18 +49,47 @@ func TestFlow(t *testing.T) { t.Fatal(err) } + options := append([]micro.Option{}, + micro.Server( + httpsrv.NewServer( + server.Codec("application/json", jsonpbcodec.NewCodec()), + server.Address("127.0.0.1:7989"), + ), + ), + 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) steps := []flow.Step{ - flow.NewCallStep("test", "Github.LookupUser", flow.StepID("test.Github.LookupUser")), - flow.NewCallStep("test", "Github.UpdateUser", flow.StepRequires("test.Github.LookupUser")), - flow.NewCallStep("test", "Github.RemoveUser", flow.StepRequires("test.Github.UpdateUser")), - flow.NewCallStep("test", "Github.MailUser", flow.StepRequires("test.Github.UpdateUser")), + flow.NewCallStep("test", pb.TestServiceName, "LookupUser", flow.StepID("test.TestService.LookupUser")), + flow.NewCallStep("test", pb.TestServiceName, "UpdateUser", flow.StepRequires("test.TestService.LookupUser")), + flow.NewCallStep("test", pb.TestServiceName, "RemoveUser", flow.StepRequires("test.TestService.UpdateUser")), + flow.NewCallStep("test", pb.TestServiceName, "MailUser", flow.StepRequires("test.TestService.UpdateUser")), } w, err := f.WorkflowCreate(ctx, "test", steps...) if err != nil { t.Fatal(err) } - req := &pb.LookupUserReq{Username: "vtolstov"} + req := &pb.LookupUserReq{Name: "vtolstov"} id, err := w.Execute(ctx, req, flow.ExecuteTimeout(2*time.Second)) if err != nil { t.Fatal(err) diff --git a/flow/generate.go b/flow/generate.go new file mode 100644 index 0000000..3136ded --- /dev/null +++ b/flow/generate.go @@ -0,0 +1,5 @@ +package flow + +//go:generate protoc -I./proto -I. -I/home/vtolstov/devel/projects/unistack/micro/micro-proto --go_out=paths=source_relative:./proto proto/test.proto + +//go:generate protoc -I./proto -I. -I/home/vtolstov/devel/projects/unistack/micro/micro-proto --go-micro_out=components=micro|http,debug=true,tag_path=./proto,paths=source_relative:./proto proto/test.proto diff --git a/flow/proto/test.pb.go b/flow/proto/test.pb.go new file mode 100644 index 0000000..9a5438e --- /dev/null +++ b/flow/proto/test.pb.go @@ -0,0 +1,291 @@ +// Code generated by protoc-gen-go. DO NOT EDIT. +// versions: +// protoc-gen-go v1.26.0 +// protoc v3.15.6 +// source: test.proto + +package pb + +import ( + _ "github.com/unistack-org/micro-proto/api" + _ "github.com/unistack-org/micro-proto/openapiv2" + _ "github.com/unistack-org/micro-proto/tag" + protoreflect "google.golang.org/protobuf/reflect/protoreflect" + protoimpl "google.golang.org/protobuf/runtime/protoimpl" + _ "google.golang.org/protobuf/types/known/wrapperspb" + reflect "reflect" + sync "sync" +) + +const ( + // Verify that this generated code is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion) + // Verify that runtime/protoimpl is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) +) + +type LookupUserReq struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Name string `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty"` +} + +func (x *LookupUserReq) Reset() { + *x = LookupUserReq{} + if protoimpl.UnsafeEnabled { + mi := &file_test_proto_msgTypes[0] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *LookupUserReq) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*LookupUserReq) ProtoMessage() {} + +func (x *LookupUserReq) ProtoReflect() protoreflect.Message { + mi := &file_test_proto_msgTypes[0] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use LookupUserReq.ProtoReflect.Descriptor instead. +func (*LookupUserReq) Descriptor() ([]byte, []int) { + return file_test_proto_rawDescGZIP(), []int{0} +} + +func (x *LookupUserReq) GetName() string { + if x != nil { + return x.Name + } + return "" +} + +type LookupUserRsp struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Birthday string `protobuf:"bytes,1,opt,name=birthday,proto3" json:"birthday,omitempty"` +} + +func (x *LookupUserRsp) Reset() { + *x = LookupUserRsp{} + if protoimpl.UnsafeEnabled { + mi := &file_test_proto_msgTypes[1] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *LookupUserRsp) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*LookupUserRsp) ProtoMessage() {} + +func (x *LookupUserRsp) ProtoReflect() protoreflect.Message { + mi := &file_test_proto_msgTypes[1] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use LookupUserRsp.ProtoReflect.Descriptor instead. +func (*LookupUserRsp) Descriptor() ([]byte, []int) { + return file_test_proto_rawDescGZIP(), []int{1} +} + +func (x *LookupUserRsp) GetBirthday() string { + if x != nil { + return x.Birthday + } + return "" +} + +type Error struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Msg string `protobuf:"bytes,1,opt,name=msg,proto3" json:"msg,omitempty"` +} + +func (x *Error) Reset() { + *x = Error{} + if protoimpl.UnsafeEnabled { + mi := &file_test_proto_msgTypes[2] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *Error) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*Error) ProtoMessage() {} + +func (x *Error) ProtoReflect() protoreflect.Message { + mi := &file_test_proto_msgTypes[2] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use Error.ProtoReflect.Descriptor instead. +func (*Error) Descriptor() ([]byte, []int) { + return file_test_proto_rawDescGZIP(), []int{2} +} + +func (x *Error) GetMsg() string { + if x != nil { + return x.Msg + } + return "" +} + +var File_test_proto protoreflect.FileDescriptor + +var file_test_proto_rawDesc = []byte{ + 0x0a, 0x0a, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x04, 0x74, 0x65, + 0x73, 0x74, 0x1a, 0x0d, 0x74, 0x61, 0x67, 0x2f, 0x74, 0x61, 0x67, 0x2e, 0x70, 0x72, 0x6f, 0x74, + 0x6f, 0x1a, 0x15, 0x61, 0x70, 0x69, 0x2f, 0x61, 0x6e, 0x6e, 0x6f, 0x74, 0x61, 0x74, 0x69, 0x6f, + 0x6e, 0x73, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x1a, 0x1b, 0x6f, 0x70, 0x65, 0x6e, 0x61, 0x70, + 0x69, 0x76, 0x32, 0x2f, 0x61, 0x6e, 0x6e, 0x6f, 0x74, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x2e, + 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x1a, 0x1e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2f, 0x70, 0x72, + 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2f, 0x77, 0x72, 0x61, 0x70, 0x70, 0x65, 0x72, 0x73, 0x2e, + 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22, 0x23, 0x0a, 0x0d, 0x4c, 0x6f, 0x6f, 0x6b, 0x75, 0x70, 0x55, + 0x73, 0x65, 0x72, 0x52, 0x65, 0x71, 0x12, 0x12, 0x0a, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x01, + 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x22, 0x2b, 0x0a, 0x0d, 0x4c, 0x6f, + 0x6f, 0x6b, 0x75, 0x70, 0x55, 0x73, 0x65, 0x72, 0x52, 0x73, 0x70, 0x12, 0x1a, 0x0a, 0x08, 0x62, + 0x69, 0x72, 0x74, 0x68, 0x64, 0x61, 0x79, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x08, 0x62, + 0x69, 0x72, 0x74, 0x68, 0x64, 0x61, 0x79, 0x22, 0x19, 0x0a, 0x05, 0x45, 0x72, 0x72, 0x6f, 0x72, + 0x12, 0x10, 0x0a, 0x03, 0x6d, 0x73, 0x67, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x6d, + 0x73, 0x67, 0x32, 0xab, 0x01, 0x0a, 0x0b, 0x54, 0x65, 0x73, 0x74, 0x53, 0x65, 0x72, 0x76, 0x69, + 0x63, 0x65, 0x12, 0x9b, 0x01, 0x0a, 0x0a, 0x4c, 0x6f, 0x6f, 0x6b, 0x75, 0x70, 0x55, 0x73, 0x65, + 0x72, 0x12, 0x13, 0x2e, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x4c, 0x6f, 0x6f, 0x6b, 0x75, 0x70, 0x55, + 0x73, 0x65, 0x72, 0x52, 0x65, 0x71, 0x1a, 0x13, 0x2e, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x4c, 0x6f, + 0x6f, 0x6b, 0x75, 0x70, 0x55, 0x73, 0x65, 0x72, 0x52, 0x73, 0x70, 0x22, 0x63, 0x92, 0x41, 0x3a, + 0x2a, 0x0a, 0x4c, 0x6f, 0x6f, 0x6b, 0x75, 0x70, 0x55, 0x73, 0x65, 0x72, 0x4a, 0x2c, 0x0a, 0x07, + 0x64, 0x65, 0x66, 0x61, 0x75, 0x6c, 0x74, 0x12, 0x21, 0x0a, 0x0e, 0x45, 0x72, 0x72, 0x6f, 0x72, + 0x20, 0x72, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x0f, 0x0a, 0x0d, 0x1a, 0x0b, 0x2e, + 0x74, 0x65, 0x73, 0x74, 0x2e, 0x45, 0x72, 0x72, 0x6f, 0x72, 0x82, 0xd3, 0xe4, 0x93, 0x02, 0x18, + 0x12, 0x16, 0x2f, 0x76, 0x31, 0x2f, 0x75, 0x73, 0x65, 0x72, 0x2f, 0x6c, 0x6f, 0x6f, 0x6b, 0x75, + 0x70, 0x2f, 0x7b, 0x6e, 0x61, 0x6d, 0x65, 0x7d, 0xba, 0xea, 0xff, 0xf9, 0x01, 0x02, 0x08, 0x05, + 0x42, 0x33, 0x5a, 0x31, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x75, + 0x6e, 0x69, 0x73, 0x74, 0x61, 0x63, 0x6b, 0x2d, 0x6f, 0x72, 0x67, 0x2f, 0x6d, 0x69, 0x63, 0x72, + 0x6f, 0x2d, 0x74, 0x65, 0x73, 0x74, 0x73, 0x2f, 0x66, 0x6c, 0x6f, 0x77, 0x2f, 0x70, 0x72, 0x6f, + 0x74, 0x6f, 0x3b, 0x70, 0x62, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, +} + +var ( + file_test_proto_rawDescOnce sync.Once + file_test_proto_rawDescData = file_test_proto_rawDesc +) + +func file_test_proto_rawDescGZIP() []byte { + file_test_proto_rawDescOnce.Do(func() { + file_test_proto_rawDescData = protoimpl.X.CompressGZIP(file_test_proto_rawDescData) + }) + return file_test_proto_rawDescData +} + +var file_test_proto_msgTypes = make([]protoimpl.MessageInfo, 3) +var file_test_proto_goTypes = []interface{}{ + (*LookupUserReq)(nil), // 0: test.LookupUserReq + (*LookupUserRsp)(nil), // 1: test.LookupUserRsp + (*Error)(nil), // 2: test.Error +} +var file_test_proto_depIdxs = []int32{ + 0, // 0: test.TestService.LookupUser:input_type -> test.LookupUserReq + 1, // 1: test.TestService.LookupUser:output_type -> test.LookupUserRsp + 1, // [1:2] is the sub-list for method output_type + 0, // [0:1] is the sub-list for method input_type + 0, // [0:0] is the sub-list for extension type_name + 0, // [0:0] is the sub-list for extension extendee + 0, // [0:0] is the sub-list for field type_name +} + +func init() { file_test_proto_init() } +func file_test_proto_init() { + if File_test_proto != nil { + return + } + if !protoimpl.UnsafeEnabled { + file_test_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*LookupUserReq); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_test_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*LookupUserRsp); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_test_proto_msgTypes[2].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*Error); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + } + type x struct{} + out := protoimpl.TypeBuilder{ + File: protoimpl.DescBuilder{ + GoPackagePath: reflect.TypeOf(x{}).PkgPath(), + RawDescriptor: file_test_proto_rawDesc, + NumEnums: 0, + NumMessages: 3, + NumExtensions: 0, + NumServices: 1, + }, + GoTypes: file_test_proto_goTypes, + DependencyIndexes: file_test_proto_depIdxs, + MessageInfos: file_test_proto_msgTypes, + }.Build() + File_test_proto = out.File + file_test_proto_rawDesc = nil + file_test_proto_goTypes = nil + file_test_proto_depIdxs = nil +} diff --git a/flow/proto/test.proto b/flow/proto/test.proto new file mode 100644 index 0000000..f6a0ea5 --- /dev/null +++ b/flow/proto/test.proto @@ -0,0 +1,44 @@ +syntax = "proto3"; + +package test; + +option go_package = "github.com/unistack-org/micro-tests/flow/proto;pb"; + +import "tag/tag.proto"; +import "api/annotations.proto"; +import "openapiv2/annotations.proto"; +import "google/protobuf/wrappers.proto"; + +service TestService { + //option (micro.api.micro_service) = { client_wrappers: ["one","two"]; }; + rpc LookupUser(LookupUserReq) returns (LookupUserRsp) { + option (micro.openapiv2.openapiv2_operation) = { + operation_id: "LookupUser"; + responses: { + key: "default"; + value: { + description: "Error response"; + schema: { + json_schema: { + ref: ".test.Error"; + } + } + } + } + }; + option (micro.api.http) = { get: "/v1/user/lookup/{name}"; }; + option (micro.api.micro_method) = { timeout: 5; }; + }; +}; + +message LookupUserReq { + string name = 1; +}; + +message LookupUserRsp { + string birthday = 1; +} + +message Error { + string msg = 1; +}; diff --git a/flow/proto/test_micro.pb.go b/flow/proto/test_micro.pb.go new file mode 100644 index 0000000..4d2d2ad --- /dev/null +++ b/flow/proto/test_micro.pb.go @@ -0,0 +1,36 @@ +// Code generated by protoc-gen-go-micro. DO NOT EDIT. +// protoc-gen-go-micro version: v3.4.1 +// source: test.proto + +package pb + +import ( + context "context" + api "github.com/unistack-org/micro/v3/api" + client "github.com/unistack-org/micro/v3/client" +) + +var ( + TestServiceName = "TestService" + + TestServiceEndpoints = []api.Endpoint{ + api.Endpoint{ + Name: "TestService.LookupUser", + Path: []string{"/v1/user/lookup/{name}"}, + Method: []string{"GET"}, + Handler: "rpc", + }, + } +) + +func NewTestServiceEndpoints() []api.Endpoint { + return TestServiceEndpoints +} + +type TestServiceClient interface { + LookupUser(ctx context.Context, req *LookupUserReq, opts ...client.CallOption) (*LookupUserRsp, error) +} + +type TestServiceServer interface { + LookupUser(ctx context.Context, req *LookupUserReq, rsp *LookupUserRsp) error +} diff --git a/flow/proto/test_micro_http.pb.go b/flow/proto/test_micro_http.pb.go new file mode 100644 index 0000000..99873b3 --- /dev/null +++ b/flow/proto/test_micro_http.pb.go @@ -0,0 +1,70 @@ +// Code generated by protoc-gen-go-micro. DO NOT EDIT. +// protoc-gen-go-micro version: v3.4.1 +// source: test.proto + +package pb + +import ( + context "context" + http "net/http" + time "time" + + v3 "github.com/unistack-org/micro-client-http/v3" + api "github.com/unistack-org/micro/v3/api" + client "github.com/unistack-org/micro/v3/client" + server "github.com/unistack-org/micro/v3/server" +) + +type testServiceClient struct { + c client.Client + name string +} + +func NewTestServiceClient(name string, c client.Client) TestServiceClient { + return &testServiceClient{c: c, name: name} +} + +func (c *testServiceClient) LookupUser(ctx context.Context, req *LookupUserReq, opts ...client.CallOption) (*LookupUserRsp, error) { + errmap := make(map[string]interface{}, 1) + errmap["default"] = &Error{} + opts = append(opts, + v3.ErrorMap(errmap), + ) + opts = append(opts, + v3.Method(http.MethodGet), + v3.Path("/v1/user/lookup/{name}"), + ) + opts = append(opts, client.WithRequestTimeout(time.Second*5)) + rsp := &LookupUserRsp{} + err := c.c.Call(ctx, c.c.NewRequest(c.name, "TestService.LookupUser", req), rsp, opts...) + if err != nil { + return nil, err + } + return rsp, nil +} + +type testServiceServer struct { + TestServiceServer +} + +func (h *testServiceServer) LookupUser(ctx context.Context, req *LookupUserReq, rsp *LookupUserRsp) error { + var cancel context.CancelFunc + ctx, cancel = context.WithTimeout(ctx, time.Second*5) + defer cancel() + return h.TestServiceServer.LookupUser(ctx, req, rsp) +} + +func RegisterTestServiceServer(s server.Server, sh TestServiceServer, opts ...server.HandlerOption) error { + type testService interface { + LookupUser(ctx context.Context, req *LookupUserReq, rsp *LookupUserRsp) error + } + type TestService struct { + testService + } + h := &testServiceServer{sh} + var nopts []server.HandlerOption + for _, endpoint := range TestServiceEndpoints { + nopts = append(nopts, api.WithEndpoint(&endpoint)) + } + return s.Handle(s.NewHandler(&TestService{h}, append(nopts, opts...)...)) +}