diff --git a/go.mod b/go.mod index 6669142..e28023a 100644 --- a/go.mod +++ b/go.mod @@ -16,10 +16,12 @@ require ( github.com/sirupsen/logrus v1.9.3 github.com/stretchr/testify v1.9.0 github.com/teran/go-docker-testsuite v0.0.6 + github.com/teran/go-grpctest v0.0.6 github.com/teran/go-ptr v1.1.0 github.com/teran/go-time v0.0.2 golang.org/x/sync v0.7.0 google.golang.org/grpc v1.65.0 + google.golang.org/protobuf v1.34.2 ) require ( @@ -87,6 +89,5 @@ require ( golang.org/x/time v0.5.0 // indirect golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d // indirect google.golang.org/genproto/googleapis/rpc v0.0.0-20240528184218-531527333157 // indirect - google.golang.org/protobuf v1.34.2 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 9a94a32..d774b79 100644 --- a/go.sum +++ b/go.sum @@ -243,6 +243,8 @@ github.com/teran/echo-grpc-server v0.0.0-20240512211903-7c0d3d091c93 h1:kiYDPhhM github.com/teran/echo-grpc-server v0.0.0-20240512211903-7c0d3d091c93/go.mod h1:ffluio+EtHNzo+6Pd4d0FnEXHnkMZhOTOiWS0FkpbzA= github.com/teran/go-docker-testsuite v0.0.6 h1:itW2lpW2tzLMgFBbHDGGCfop+eCy7Lroq/PTq1j5iNs= github.com/teran/go-docker-testsuite v0.0.6/go.mod h1:KO9qMSZPAXkaceW10dAmDBL04bWSItnn0f9p16p/YzE= +github.com/teran/go-grpctest v0.0.6 h1:q34p64c8icQq5TFgLsDa9VM8BThhesN43XTS8PJAh38= +github.com/teran/go-grpctest v0.0.6/go.mod h1:rNcMrEhPlNH7DFpQbcyl2vFzvfrx4UsxGuf3i/W77d0= github.com/teran/go-ptr v1.1.0 h1:c/5SPr+FB4zfmmKVB+KTCNKpgC/zWKBY5JHReL84lKU= github.com/teran/go-ptr v1.1.0/go.mod h1:2gyUP12bjbvBfyXm/NflOYJHONy415pNVeSTOZHna+A= github.com/teran/go-random v0.0.1 h1:WQ3cF7UUQb/Q0oGAKcYeLeUIcMzD4vSs18oU1pkU/Mo= diff --git a/presenter/manager/grpc/handlers_test.go b/presenter/manager/grpc/handlers_test.go index 1ed33ad..a84dea2 100644 --- a/presenter/manager/grpc/handlers_test.go +++ b/presenter/manager/grpc/handlers_test.go @@ -6,6 +6,7 @@ import ( "time" "github.com/stretchr/testify/suite" + grpctest "github.com/teran/go-grpctest" v1pb "github.com/teran/archived/presenter/manager/grpc/proto/v1" "github.com/teran/archived/service" @@ -87,7 +88,7 @@ type manageHandlersTestSuite struct { svcMock *service.Mock handlers ManageServerInterface - srv testServer + srv grpctest.Server client v1pb.ManageServiceClient } @@ -95,7 +96,7 @@ type manageHandlersTestSuite struct { func (s *manageHandlersTestSuite) SetupTest() { s.svcMock = service.NewMock() s.handlers = New(s.svcMock) - s.srv = newTestServer() + s.srv = grpctest.New() s.handlers.Register(s.srv.Server()) err := s.srv.Run() diff --git a/presenter/manager/grpc/testserver.go b/presenter/manager/grpc/testserver.go deleted file mode 100644 index 3a4dee1..0000000 --- a/presenter/manager/grpc/testserver.go +++ /dev/null @@ -1,68 +0,0 @@ -package grpc - -import ( - "context" - "net" - - "google.golang.org/grpc" - "google.golang.org/grpc/test/bufconn" -) - -type testServer interface { - DialContext(ctx context.Context) (*grpc.ClientConn, error) - Run() error - Close() error - Server() *grpc.Server -} - -type server struct { - srv *grpc.Server - listener net.Listener -} - -// New ... -func newTestServer(opts ...grpc.ServerOption) testServer { - return &server{ - srv: grpc.NewServer(opts...), - listener: bufconn.Listen(2 * 1024 * 1024), - } -} - -// Close ... -func (s *server) Close() error { - err := s.listener.Close() - if err != nil { - return err - } - - s.srv.Stop() - - return nil -} - -// DialContext ... -func (s *server) DialContext(ctx context.Context) (*grpc.ClientConn, error) { - return grpc.DialContext( - ctx, - "bufnet", - grpc.WithContextDialer(s.dial), - grpc.WithInsecure(), - ) -} - -func (s *server) dial(context.Context, string) (net.Conn, error) { - return s.listener.(*bufconn.Listener).Dial() -} - -// Run ... -func (s *server) Run() error { - go func() { - _ = s.srv.Serve(s.listener) - }() - - return nil -} - -func (s *server) Server() *grpc.Server { - return s.srv -}