#1 - fix after review
This commit is contained in:
@@ -29,7 +29,7 @@ func NewProtoSet() *ProtoSet {
|
||||
}
|
||||
|
||||
func (p *ProtoSet) GetMessage(addr, pkg, svc, mth string) (protoreflect.Message, protoreflect.Message, error) {
|
||||
if addr == "" || svc == "" || mth == "" {
|
||||
if addr == "" || svc == "" || mth == "" || pkg == "" {
|
||||
return nil, nil, errors.New("addr or service name is empty")
|
||||
}
|
||||
p.mu.Lock()
|
||||
@@ -41,7 +41,7 @@ func (p *ProtoSet) GetMessage(addr, pkg, svc, mth string) (protoreflect.Message,
|
||||
|
||||
pdesc, err := pfile.FindDescriptorByName(protoreflect.FullName(pkg + "." + svc))
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("failed to find service %s.%s, err: %s", pkg, svc, err)
|
||||
return nil, nil, fmt.Errorf("failed to find service %s.%s, err: %w", pkg, svc, err)
|
||||
}
|
||||
|
||||
sdesc, ok := pdesc.(protoreflect.ServiceDescriptor)
|
||||
@@ -63,13 +63,13 @@ func (p *ProtoSet) GetMessage(addr, pkg, svc, mth string) (protoreflect.Message,
|
||||
func (p *ProtoSet) AddProtoset(addr, svc string, data []byte) error {
|
||||
fdset := &descriptorpb.FileDescriptorSet{}
|
||||
if err := protocodec.NewCodec().Unmarshal(data, fdset); err != nil {
|
||||
return fmt.Errorf("failed to unmarshal protoset file: %s", err)
|
||||
return fmt.Errorf("failed to unmarshal protoset file: %w", err)
|
||||
}
|
||||
|
||||
pfileoptions := protodesc.FileOptions{AllowUnresolvable: true}
|
||||
pfiles, err := pfileoptions.NewFiles(fdset)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to use protoset file, err: %s", err)
|
||||
return fmt.Errorf("failed to use protoset file, err: %w", err)
|
||||
}
|
||||
|
||||
p.mu.Lock()
|
||||
|
@@ -1,50 +1,49 @@
|
||||
package protoset
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
. "github.com/smartystreets/goconvey/convey"
|
||||
"go.unistack.org/micro/v3/logger"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestProtoSet(t *testing.T) {
|
||||
Convey("test1", t, func() {
|
||||
ctx := context.Background()
|
||||
p := NewProtoSet(logger.DefaultLogger)
|
||||
data, err := os.ReadFile("/Users/kgorbunov/GolandProjects/src/card-proto/card-grpc.protoset")
|
||||
So(err, ShouldBeNil)
|
||||
func TestProtoSet_1(t *testing.T) {
|
||||
p := NewProtoSet()
|
||||
data, err := os.ReadFile("path to .protoset")
|
||||
assert.Nil(t, err)
|
||||
|
||||
err = p.AddProtoset(ctx, "localhost:9090", "CardService", data)
|
||||
So(err, ShouldBeNil)
|
||||
err = p.AddProtoset("localhost:9090", "CardService", data)
|
||||
assert.Nil(t, err)
|
||||
|
||||
req, rsp, err := p.GetMessage(ctx, "localhost:9090", "card_proto", "CardService", "GetCardList")
|
||||
So(err, ShouldBeNil)
|
||||
fmt.Printf("req: %v, rsp: %v", req, rsp)
|
||||
})
|
||||
|
||||
Convey("test2-bad", t, func() {
|
||||
ctx := context.Background()
|
||||
p := NewProtoSet(logger.DefaultLogger)
|
||||
data, err := os.ReadFile("/Users/kgorbunov/GolandProjects/src/card-proto/card-grpc.protoset")
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
err = p.AddProtoset(ctx, "localhost:9090", "CardService", data)
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
req, rsp, err := p.GetMessage(ctx, "localhost:9090", "card_proto", "Card", "GetCardList")
|
||||
So(err, ShouldBeError)
|
||||
fmt.Printf("req: %v, rsp: %v", req, rsp)
|
||||
})
|
||||
|
||||
Convey("test2-not-found", t, func() {
|
||||
ctx := context.Background()
|
||||
p := NewProtoSet(logger.DefaultLogger)
|
||||
|
||||
req, rsp, err := p.GetMessage(ctx, "localhost:9090", "card_proto", "CardService", "GetCardList")
|
||||
So(err, ShouldEqual, errNotFound)
|
||||
fmt.Printf("req: %v, rsp: %v", req, rsp)
|
||||
})
|
||||
req, rsp, err := p.GetMessage("localhost:9090", "card_proto", "CardService", "GetCardList")
|
||||
assert.Nil(t, err)
|
||||
assert.NotNil(t, req)
|
||||
assert.NotNil(t, rsp)
|
||||
fmt.Printf("req: %v, rsp: %v \n", req, rsp)
|
||||
}
|
||||
|
||||
func TestProtoSet_2_bad(t *testing.T) {
|
||||
p := NewProtoSet()
|
||||
data, err := os.ReadFile("path to .protoset")
|
||||
assert.Nil(t, err)
|
||||
|
||||
err = p.AddProtoset("localhost:9090", "CardService", data)
|
||||
assert.Nil(t, err)
|
||||
|
||||
req, rsp, err := p.GetMessage("localhost:9090", "card_proto", "Card", "GetCardList")
|
||||
assert.Error(t, err)
|
||||
assert.Nil(t, req)
|
||||
assert.Nil(t, rsp)
|
||||
fmt.Printf("req: %v, rsp: %v \n", req, rsp)
|
||||
}
|
||||
|
||||
func TestProtoSet_3_not_found(t *testing.T) {
|
||||
p := NewProtoSet()
|
||||
|
||||
req, rsp, err := p.GetMessage("localhost:9090", "card_proto", "CardService", "GetCardList")
|
||||
assert.ErrorIs(t, err, errNotFound)
|
||||
assert.Nil(t, req)
|
||||
assert.Nil(t, rsp)
|
||||
fmt.Printf("req: %v, rsp: %v \n", req, rsp)
|
||||
}
|
||||
|
Reference in New Issue
Block a user