Skip to content

Commit

Permalink
add test for grpcmux server startup
Browse files Browse the repository at this point in the history
  • Loading branch information
likezhang committed Jun 7, 2022
1 parent d4c0537 commit d65bab3
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 4 deletions.
4 changes: 4 additions & 0 deletions grpcmux/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ func initComponents(conf *config.Config) {
}

func initLogging(conf *config.Config) {
if conf == nil {
return
}

level := conf.GetString("level", "debug")
encoding := conf.GetString("encoding", "console")
outputPath := conf.GetString("output", "")
Expand Down
2 changes: 1 addition & 1 deletion sample/grpc-server/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ module github.com/skema-dev/skema-go/sample/grpc-server
go 1.16

require (
github.com/skema-dev/skema-go v0.0.0-20220427180038-06103d6faceb
github.com/skema-dev/skema-go v1.0.0
google.golang.org/grpc v1.45.0
)
5 changes: 2 additions & 3 deletions sample/grpc-server/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -447,8 +447,8 @@ github.com/shopspring/decimal v1.2.0/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFR
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
github.com/sirupsen/logrus v1.4.1/go.mod h1:ni0Sbl8bgC9z8RoU9G6nDWqqs/fq4eDPysMBDgk/93Q=
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
github.com/skema-dev/skema-go v0.0.0-20220427180038-06103d6faceb h1:9sBFkRmMb19ZcWLghi4OeWzW0wc8vxwKHDvZrbv745g=
github.com/skema-dev/skema-go v0.0.0-20220427180038-06103d6faceb/go.mod h1:396A6TkYm98kH458fZwg+HnmPRxNNqrQrWJ2VgbaAN8=
github.com/skema-dev/skema-go v1.0.0 h1:n9uCZcy3BEpJJnpfrQAZ+k040JMRrDoLhFMMeG7c4yk=
github.com/skema-dev/skema-go v1.0.0/go.mod h1:SGdqe02Bdd3MzvbhnDBiFlJ16pXuDIoqcHHjzi2Cmno=
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
github.com/spf13/afero v1.8.2 h1:xehSyVa0YnHWsJ49JFljMpg1HX19V6NDZ1fkm1Xznbo=
github.com/spf13/afero v1.8.2/go.mod h1:CtAatgMJh6bJEIs48Ay/FOnkljP3WeGUG0MC1RfAqwo=
Expand All @@ -474,7 +474,6 @@ github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMT
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s=
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
github.com/traefik/yaegi v0.11.3/go.mod h1:RuCwD8/wsX7b6KoQHOaIFUfuH3gQIK4KWnFFmJMw5VA=
github.com/tv42/httpunix v0.0.0-20150427012821-b75d8614f926/go.mod h1:9ESjWnEqriFuLhtthL60Sar/7RFoluCcXsuvEwTV5KM=
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
Expand Down
File renamed without changes.
6 changes: 6 additions & 0 deletions test/grpcmux_config/conf/grpc_test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
port: 9991 # for grpc service
http:
port: 9992 # for http service
gateway:
path: "/" # this is the routing prefix if you need. You can force to append an extra path before all standard URLs

62 changes: 62 additions & 0 deletions test/grpcmux_config/grpcmux_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package main

import (
"context"
"fmt"

_ "embed"

"github.com/skema-dev/skema-go/grpcmux"
"github.com/skema-dev/skema-go/logging"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
)

func main() {
grpcSrv := grpcmux.NewServer(
grpc.ChainUnaryInterceptor(Interceptor1(), Interceptor2(), Interceptor3()),
)

if err := grpcSrv.Serve(); err != nil {
logging.Fatalf("Serve error %v", err.Error())
}
}

func Interceptor1() grpc.UnaryServerInterceptor {
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo,
handler grpc.UnaryHandler) (resp interface{}, err error) {
_, ok := metadata.FromIncomingContext(ctx)
if !ok {
return nil, fmt.Errorf("couldn't parse incoming context metadata")
}
logging.Infof("first interceptoer 11111")
h, err := handler(ctx, req)
return h, err
}
}

func Interceptor2() grpc.UnaryServerInterceptor {
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo,
handler grpc.UnaryHandler) (resp interface{}, err error) {
_, ok := metadata.FromIncomingContext(ctx)
if !ok {
return nil, fmt.Errorf("couldn't parse incoming context metadata")
}
logging.Infof(" interceptoer 222222")
h, err := handler(ctx, req)
return h, err
}
}

func Interceptor3() grpc.UnaryServerInterceptor {
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo,
handler grpc.UnaryHandler) (resp interface{}, err error) {
_, ok := metadata.FromIncomingContext(ctx)
if !ok {
return nil, fmt.Errorf("couldn't parse incoming context metadata")
}
logging.Infof(" interceptoer 333333")
h, err := handler(ctx, req)
return h, err
}
}

0 comments on commit d65bab3

Please sign in to comment.