Skip to content

Commit

Permalink
refactor(account): account tested with native gRPC
Browse files Browse the repository at this point in the history
  • Loading branch information
xmlking committed Jun 18, 2020
1 parent 782f26b commit 8552c2a
Show file tree
Hide file tree
Showing 51 changed files with 998 additions and 17,862 deletions.
5 changes: 5 additions & 0 deletions docs/introduction/prerequisites.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,12 @@ go install github.com/markbates/pkger/cmd/pkger
# fetch protoc plugins into $GOPATH
GO111MODULE=off go get github.com/golang/protobuf/{proto,protoc-gen-go}
GO111MODULE=on go get github.com/micro/micro/v2/cmd/protoc-gen-micro@master

GO111MODULE=on go get github.com/gogo/protobuf/protoc-gen-gofast
GO111MODULE=on go get github.com/gogo/protobuf/protoc-gen-gogofast
GO111MODULE=on go get github.com/gogo/protobuf/protoc-gen-gogofaster
GO111MODULE=on go get github.com/gogo/protobuf/protoc-gen-gogoslick

# GO111MODULE=off go get -u github.com/envoyproxy/protoc-gen-validate
# GO111MODULE=off go get -u github.com/infobloxopen/protoc-gen-gorm
# getting official micro cli
Expand Down
53 changes: 28 additions & 25 deletions e2e/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,55 +2,58 @@
package e2e

import (
"context"
"testing"
"context"
"testing"

"github.com/golang/protobuf/ptypes/wrappers"
"github.com/micro/go-micro/v2/client"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"github.com/golang/protobuf/ptypes/wrappers"
"github.com/rs/zerolog/log"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"google.golang.org/grpc"
"google.golang.org/grpc/balancer/roundrobin"

// "github.com/xmlking/grpc-starter-kit/shared/micro/client/selector/static"
profilePB "github.com/xmlking/grpc-starter-kit/service/account/proto/profile"
userPB "github.com/xmlking/grpc-starter-kit/service/account/proto/user"
"github.com/xmlking/grpc-starter-kit/mkit/service/account/profile/v1"
"github.com/xmlking/grpc-starter-kit/mkit/service/account/user/v1"
"github.com/xmlking/grpc-starter-kit/shared/config"
)

/**
* set envelopment variables for CI e2e tests with `memory` registry.
* - export MICRO_REGISTRY=memory
* - export MICRO_SELECTOR=static
* (Or) Set envelopment variables for CI e2e tests via gRPC Proxy
* - MICRO_PROXY_ADDRESS="localhost:8081"
* You can also run this test againest your local running service with mDNS. i.e., `make run-account`
**/

// Define the suite, and absorb the built-in basic suite
// functionality from testify - including a T() method which
// returns the current testing context
type AccountTestSuite struct {
suite.Suite
user userPB.UserService
profile profilePB.ProfileService
conn *grpc.ClientConn
userClient userv1.UserServiceClient
profileClient profilev1.ProfileServiceClient
}

// SetupSuite implements suite.SetupAllSuite
func (suite *AccountTestSuite) SetupSuite() {
cfg := config.GetConfig()
suite.T().Log("in SetupSuite")
suite.user = userPB.NewUserService("mkit.service.account", client.NewClient())
suite.profile = profilePB.NewProfileService("mkit.service.account", client.NewClient())
var err error
suite.conn, err = grpc.Dial(cfg.Services.Account.Endpoint, grpc.WithInsecure(), grpc.WithBalancerName(roundrobin.Name))
if err != nil {
log.Fatal().Msgf("did not connect: %s", err)
}

println(suite.conn.Target())
suite.userClient = userv1.NewUserServiceClient(suite.conn)
suite.profileClient = profilev1.NewProfileServiceClient(suite.conn)
}

// TearDownSuite implements suite.TearDownAllSuite
func (suite *AccountTestSuite) TearDownSuite() {
suite.T().Log("in TearDownSuite")
_ = suite.conn.Close()
}

// before each test
func (suite *AccountTestSuite) SetupTest() {
t := suite.T()
t.Log("in SetupTest - creating user")
_, err := suite.user.Create(context.TODO(), &userPB.CreateRequest{
_, err := suite.userClient.Create(context.TODO(), &userv1.CreateRequest{
Username: &wrappers.StringValue{Value: "sumo"},
FirstName: &wrappers.StringValue{Value: "sumo"},
LastName: &wrappers.StringValue{Value: "demo"},
Expand All @@ -69,7 +72,7 @@ func (suite *AccountTestSuite) TestUserHandler_Exist_E2E() {
t := suite.T()
t.Log("in TestUserHandler_Exist_E2E, checking if user Exist")

rsp, err := suite.user.Exist(context.TODO(), &userPB.ExistRequest{
rsp, err := suite.userClient.Exist(context.TODO(), &userv1.ExistRequest{
Username: &wrappers.StringValue{Value: "sumo"},
})
require.Nil(t, err)
Expand Down
18 changes: 10 additions & 8 deletions e2e/emailer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/rs/zerolog/log"

"github.com/xmlking/grpc-starter-kit/mkit/service/emailer/v1"
"github.com/xmlking/grpc-starter-kit/shared/config"
_ "github.com/xmlking/grpc-starter-kit/shared/constants"
"github.com/xmlking/grpc-starter-kit/shared/eventing"
_ "github.com/xmlking/grpc-starter-kit/shared/logger"
Expand All @@ -22,8 +23,9 @@ func TestEmailSubscriber_Handle_Send_E2E(t *testing.T) {
t.Skip("skipping e2e test")
}

client := eventing.NewSourceClient()
topic := "mkit.service.emailer"
cfg := config.GetConfig()
topic := cfg.Services.Emailer.Endpoint
client := eventing.NewSourceClient(topic)

// Create an Event.
event := cloudevents.NewEvent()
Expand All @@ -33,8 +35,8 @@ func TestEmailSubscriber_Handle_Send_E2E(t *testing.T) {

// Set a target.
// ctx := cecontext.WithTopic(context.Background(), topic) // for GCP PubSub
ctx := cloudevents.ContextWithTarget(context.Background(), "http://localhost:8080/")
ctxWithRetries := cloudevents.ContextWithRetriesLinearBackoff(ctx, 10*time.Millisecond, 3)
//ctx := cloudevents.ContextWithTarget(context.Background(), "http://localhost:8082/")
ctxWithRetries := cloudevents.ContextWithRetriesLinearBackoff(context.Background(), 10*time.Millisecond, 3)
// if you want to send raw like Avro or protobuf
// ctx = cloudevents.WithEncodingBinary(ctx)

Expand All @@ -51,8 +53,9 @@ func TestEmailSubscriber_Handle_Request_E2E(t *testing.T) {
t.Skip("skipping e2e test")
}

client := eventing.NewSourceClient()
topic := "mkit.service.emailer"
cfg := config.GetConfig()
topic := cfg.Services.Emailer.Endpoint
client := eventing.NewSourceClient(topic)

// Create an Event.
event := cloudevents.NewEvent()
Expand All @@ -62,8 +65,7 @@ func TestEmailSubscriber_Handle_Request_E2E(t *testing.T) {

// Set a target.
// ctx := cecontext.WithTopic(context.Background(), topic) // for GCP PubSub
ctx := cloudevents.ContextWithTarget(context.Background(), "http://localhost:8080/")
ctxWithRetries := cloudevents.ContextWithRetriesLinearBackoff(ctx, 10*time.Millisecond, 3)
ctxWithRetries := cloudevents.ContextWithRetriesLinearBackoff(context.Background(), 10*time.Millisecond, 3)
// if you want to send raw like Avro or protobuf
// ctx = cloudevents.WithEncodingBinary(ctx)

Expand Down
2 changes: 0 additions & 2 deletions e2e/greeter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ func TestGreeter_Hello_E2E(t *testing.T) {
serviceName := constants.GREETER_SERVICE
cfg := config.GetConfig()

var conn *grpc.ClientConn

conn, err := grpc.Dial(cfg.Services.Greeter.Endpoint, grpc.WithInsecure(), grpc.WithBalancerName(roundrobin.Name))
if err != nil {
log.Fatal().Msgf("did not connect: %s", err)
Expand Down
45 changes: 45 additions & 0 deletions micro/micro.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package micro



import (
"net"

"github.com/pkg/errors"
"github.com/soheilhy/cmux"
"google.golang.org/grpc/grpclog"
)

type MicroServer struct {
mux cmux.CMux
lis net.Listener
}
type Interface interface {
Serve(l net.Listener) error
Shutdown()
}

func NewService(mux cmux.CMux, lis net.Listener) Interface {
return &MicroServer{
mux: mux,
lis: lis,
}
}
// Serve implements Server.Serve
func (s *MicroServer) Serve(net.Listener) error {
grpclog.Info("mux is starting %s", s.lis.Addr())

err := s.mux.Serve()

grpclog.Infof("mux is closed: %v", err)

return errors.Wrap(err, "failed to serve cmux server")
}

// Shutdown implements Server.Shutdown
func (s *MicroServer) Shutdown() {
err := s.lis.Close()
if err != nil {
grpclog.Errorf("failed to close cmux's listener: %v", err)
}
}
Loading

0 comments on commit 8552c2a

Please sign in to comment.