Skip to content

Commit

Permalink
Merge pull request #63 from UnderTreeTech/enhancement/test_coverage
Browse files Browse the repository at this point in the history
Enhancement/test coverage
  • Loading branch information
UnderTreeTech committed Nov 16, 2020
2 parents c2c9acc + f3535a4 commit 18581e2
Show file tree
Hide file tree
Showing 49 changed files with 1,347 additions and 321 deletions.
30 changes: 17 additions & 13 deletions examples/app/cmd/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,21 @@ import (
"fmt"
"time"

rpcConfig "github.com/UnderTreeTech/waterdrop/pkg/server/rpc/config"

httpConfig "github.com/UnderTreeTech/waterdrop/pkg/server/http/config"

rpcClient "github.com/UnderTreeTech/waterdrop/pkg/server/rpc/client"

httpClient "github.com/UnderTreeTech/waterdrop/pkg/server/http/client"

"github.com/UnderTreeTech/waterdrop/pkg/server/rpc/interceptors"

"github.com/UnderTreeTech/protobuf/demo"
"github.com/UnderTreeTech/waterdrop/pkg/server/rpc"

"github.com/UnderTreeTech/protobuf/user"

"github.com/UnderTreeTech/waterdrop/pkg/log"

"github.com/UnderTreeTech/waterdrop/pkg/server/http"

"github.com/UnderTreeTech/waterdrop/pkg/trace/jaeger"

"google.golang.org/grpc/resolver"
Expand All @@ -59,13 +63,13 @@ func main() {
etcd := etcd.New(etcdConf)
resolver.Register(etcd)

httpCliConf := &http.ClientConfig{}
httpCliConf := &httpConfig.ClientConfig{}
if err := conf.Unmarshal("client.http.app", httpCliConf); err != nil {
panic(fmt.Sprintf("unmarshal http client config fail, err msg %s", err.Error()))
}
fmt.Println("http client conf", httpCliConf)
httpClient := http.NewClient(httpCliConf)
r := &http.Request{
httpCli := httpClient.New(httpCliConf)
r := &httpClient.Request{
URI: "/api/app/validate/{id}",
PathParams: map[string]string{"id": "1"},
//Body: `{"email":"example@example.com","name":"John&Sun","password":"styd.cn","sex":2,"age":12,"addr":[{"mobile":"涓婃捣甯傚緪姹囧尯","address":"<a onblur='alert(secret)' href='http://www.google.com'>Google</a>","app":{"sappkey":"<p>md5hash</p>"},"reply":{"urls":["www.&baidu.com","www.g&oogle.com","&#x6a;&#x61;&#x76;&#x61;&#x73;&#x63;&#x72;&#x69;&#x70;&#x74;&#x3a;&#x61;&#x6c;&#x65;&#x72;&#x74;&#x28;&#x31;&#x29;&#x3b;","u003cimg src=1 onerror=alert(/xss/)u003e"]},"resp":[{"app_key":"sha1hash","app_secret":"<href>rsa</href>"}]}]}`,
Expand Down Expand Up @@ -104,24 +108,24 @@ func main() {
for i := 0; i < 1; i++ {
go func() {
for j := 0; j < 1; j++ {
err := httpClient.Post(context.Background(), r, &result)
err := httpCli.Post(context.Background(), r, &result)
if err != nil {
fmt.Println("response", err)
}
}
}()
}

cliConf := &rpc.ClientConfig{}
cliConf := &rpcConfig.ClientConfig{}
if err := conf.Unmarshal("client.rpc.stardust", cliConf); err != nil {
panic(fmt.Sprintf("unmarshal demo client config fail, err msg %s", err.Error()))
}
fmt.Println(cliConf)
client := rpc.NewClient(cliConf)
client.Use(interceptors.GoogleSREBreaker(client.GetBreakers()))
demoRPC := demo.NewDemoClient(client.GetConn())
rpcCli := rpcClient.New(cliConf)
rpcCli.Use(interceptors.GoogleSREBreaker(rpcCli.GetBreakers()))
demoRPC := demo.NewDemoClient(rpcCli.GetConn())
now := time.Now()
for i := 0; i < 1000; i++ {
for i := 0; i < 10; i++ {
_, err := demoRPC.SayHelloURL(context.Background(), &demo.HelloReq{Name: "John Sun"})
if err != nil {
//fmt.Println("err", status.ExtractStatus(err).Message())
Expand Down
4 changes: 2 additions & 2 deletions examples/app/cmd/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"time"

"github.com/UnderTreeTech/waterdrop/pkg/stats"
"github.com/UnderTreeTech/waterdrop/pkg/trace/jaeger"

"github.com/UnderTreeTech/waterdrop/examples/app/internal/server/grpc"

Expand All @@ -41,8 +42,6 @@ import (
"github.com/UnderTreeTech/waterdrop/examples/app/internal/dao"
"github.com/UnderTreeTech/waterdrop/pkg/conf"

"github.com/UnderTreeTech/waterdrop/pkg/trace/jaeger"

"github.com/UnderTreeTech/waterdrop/pkg/log"
)

Expand All @@ -53,6 +52,7 @@ func main() {

conf.Init()
defer initLog().Sync()
// you can commnet this line, then it will use default mock trace
defer jaeger.Init()()
defer dao.NewDao().Close()

Expand Down
15 changes: 9 additions & 6 deletions examples/app/internal/server/grpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ import (
"fmt"
"net"

"github.com/UnderTreeTech/waterdrop/pkg/server/rpc/config"

"github.com/UnderTreeTech/waterdrop/pkg/server/rpc/server"

"github.com/UnderTreeTech/waterdrop/pkg/server/rpc/interceptors"

"github.com/UnderTreeTech/waterdrop/pkg/utils/xnet"
Expand All @@ -33,27 +37,26 @@ import (
"github.com/UnderTreeTech/protobuf/demo"
"github.com/UnderTreeTech/waterdrop/examples/app/internal/service"
"github.com/UnderTreeTech/waterdrop/pkg/registry"
"github.com/UnderTreeTech/waterdrop/pkg/server/rpc"
)

type ServerInfo struct {
Server *rpc.Server
Server *server.Server
ServiceInfo *registry.ServiceInfo
}

func New() *ServerInfo {
srvConfig := &rpc.ServerConfig{}
srvConfig := &config.ServerConfig{}
parseConfig("server.rpc", srvConfig)
if srvConfig.WatchConfig {
conf.OnChange(func(config *conf.Config) {
parseConfig("server.rpc", srvConfig)
})
}

server := rpc.NewServer(srvConfig)
server := server.New(srvConfig)
registerServers(server.Server(), &service.Service{})

server.Use(interceptors.Validate())
server.Use(interceptors.ValidateForUnaryServer())

addr := server.Start()
_, port, _ := net.SplitHostPort(addr.String())
Expand All @@ -71,7 +74,7 @@ func registerServers(g *grpc.Server, s *service.Service) {
demo.RegisterDemoServer(g, s)
}

func parseConfig(configName string, srvConfig *rpc.ServerConfig) {
func parseConfig(configName string, srvConfig *config.ServerConfig) {
if err := conf.Unmarshal(configName, srvConfig); err != nil {
panic(fmt.Sprintf("unmarshal grpc server config fail, err msg %s", err.Error()))
}
Expand Down
28 changes: 16 additions & 12 deletions examples/app/internal/server/http/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ import (
"fmt"
"net"

"github.com/UnderTreeTech/waterdrop/pkg/server/http/config"

"github.com/UnderTreeTech/waterdrop/pkg/server/http/server"

"github.com/UnderTreeTech/waterdrop/pkg/server/http/middlewares"

"github.com/gin-gonic/gin/binding"
"github.com/go-playground/validator/v10"

Expand All @@ -32,27 +38,25 @@ import (
"github.com/UnderTreeTech/waterdrop/pkg/utils/xnet"

"github.com/UnderTreeTech/waterdrop/pkg/registry"

"github.com/UnderTreeTech/waterdrop/pkg/server/http"
)

type ServerInfo struct {
Server *http.Server
Server *server.Server
ServiceInfo *registry.ServiceInfo
}

func New() *ServerInfo {
srvConfig := &http.ServerConfig{}
srvConfig := &config.ServerConfig{}
parseConfig("server.http", srvConfig)
if srvConfig.WatchConfig {
conf.OnChange(func(config *conf.Config) {
parseConfig("server.http", srvConfig)
})
}

server := http.NewServer(srvConfig)
server := server.New(srvConfig)

//middlewares(server)
registerMiddlewares(server)
router(server)

addr := server.Start()
Expand All @@ -67,26 +71,26 @@ func New() *ServerInfo {
return &ServerInfo{Server: server, ServiceInfo: serviceInfo}
}

func parseConfig(configName string, srvConfig *http.ServerConfig) {
func parseConfig(configName string, srvConfig *config.ServerConfig) {
if err := conf.Unmarshal(configName, srvConfig); err != nil {
panic(fmt.Sprintf("unmarshal http server config fail, err msg %s", err.Error()))
}
}

func middlewares(s *http.Server) {
func registerMiddlewares(s *server.Server) {
//jwt token middleware
//s.Use(jwt.JWT())
s.Use(s.Header())
s.Use(middlewares.Header())

signClientConfig := &http.ClientConfig{}
signClientConfig := &config.ClientConfig{}
if err := conf.Unmarshal("client.http.app", signClientConfig); err != nil {
panic(fmt.Sprintf("unmarshal signature client config fail, err msg %s", err.Error()))
}
signVerify := http.NewSignatureVerify(signClientConfig, dao.NewRedis())
signVerify := middlewares.NewSignatureVerify(signClientConfig, dao.NewRedis())
s.Use(signVerify.Signature())
}

func router(s *http.Server) {
func router(s *server.Server) {
g := s.Group("/api")
{
g.GET("/app/secrets", getAppInfo)
Expand Down
7 changes: 4 additions & 3 deletions examples/http/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,16 @@ import (
"context"
"time"

"github.com/UnderTreeTech/waterdrop/pkg/server/http/server"

"github.com/alibaba/sentinel-golang/core/flow"

"github.com/UnderTreeTech/waterdrop/pkg/ratelimit/setinel"

"github.com/UnderTreeTech/waterdrop/pkg/server/http/ratelimit/sentinel"
"github.com/UnderTreeTech/waterdrop/pkg/server/http/middlewares/ratelimit/sentinel"

"github.com/UnderTreeTech/waterdrop/pkg/log"

"github.com/UnderTreeTech/waterdrop/pkg/server/http"
"github.com/gin-gonic/gin"
)

Expand All @@ -52,7 +53,7 @@ func main() {
)
setinel.InitSentinel(config)

srv := http.NewServer(nil)
srv := server.New(nil)
srv.Use(sentinel.Sentinel())

g := srv.Group("/api")
Expand Down
11 changes: 6 additions & 5 deletions examples/rpc/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ import (
"flag"
"fmt"

"github.com/UnderTreeTech/protobuf/demo"
"github.com/UnderTreeTech/waterdrop/pkg/server/rpc"
"github.com/UnderTreeTech/waterdrop/pkg/server/rpc/client"
"github.com/UnderTreeTech/waterdrop/pkg/server/rpc/config"

"github.com/UnderTreeTech/protobuf/demo"
"github.com/UnderTreeTech/waterdrop/pkg/log"

"github.com/UnderTreeTech/waterdrop/pkg/conf"
Expand All @@ -46,13 +47,13 @@ func main() {
etcd := etcd.New(etcdConf)
resolver.Register(etcd)

cliConf := &rpc.ClientConfig{}
cliConf := &config.ClientConfig{}
if err := conf.Unmarshal("client.rpc.demo", cliConf); err != nil {
panic(fmt.Sprintf("unmarshal demo client config fail, err msg %s", err.Error()))
}

client := demo.NewDemoClient(rpc.NewClient(cliConf).GetConn())
for i := 0; i < 100; i++ {
client := demo.NewDemoClient(client.New(cliConf).GetConn())
for i := 0; i < 10; i++ {
reply, err := client.SayHelloURL(context.Background(), &demo.HelloReq{Name: "John Sun"})
if err != nil {
fmt.Println("error", err)
Expand Down
11 changes: 7 additions & 4 deletions examples/rpc/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ import (
"os/signal"
"syscall"

"github.com/UnderTreeTech/waterdrop/pkg/server/rpc/config"

"github.com/UnderTreeTech/waterdrop/pkg/server/rpc/server"

"github.com/UnderTreeTech/waterdrop/pkg/stats"

"google.golang.org/grpc/resolver"
Expand All @@ -45,7 +49,6 @@ import (

"github.com/UnderTreeTech/protobuf/demo"
"github.com/UnderTreeTech/waterdrop/pkg/registry"
"github.com/UnderTreeTech/waterdrop/pkg/server/rpc"
)

func main() {
Expand All @@ -56,15 +59,15 @@ func main() {
conf.Init()
defer log.New(nil).Sync()

srvConfig := &rpc.ServerConfig{}
srvConfig := &config.ServerConfig{}
parseConfig("server.rpc", srvConfig)
if srvConfig.WatchConfig {
conf.OnChange(func(config *conf.Config) {
parseConfig("server.rpc", srvConfig)
})
}

server := rpc.NewServer(srvConfig)
server := server.New(srvConfig)
registerServers(server.Server(), &Service{})

addr := server.Start()
Expand Down Expand Up @@ -95,7 +98,7 @@ func registerServers(g *grpc.Server, s *Service) {
demo.RegisterDemoServer(g, s)
}

func parseConfig(configName string, srvConfig *rpc.ServerConfig) {
func parseConfig(configName string, srvConfig *config.ServerConfig) {
if err := conf.Unmarshal(configName, srvConfig); err != nil {
panic(fmt.Sprintf("unmarshal grpc server config fail, err msg %s", err.Error()))
}
Expand Down
13 changes: 8 additions & 5 deletions examples/websocket/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,16 @@ package main

import (
"fmt"
"github.com/UnderTreeTech/waterdrop/pkg/log"
"github.com/UnderTreeTech/waterdrop/pkg/server/http"
"github.com/gorilla/websocket"
"os"
"os/signal"
"syscall"
"time"

"github.com/UnderTreeTech/waterdrop/pkg/server/http/server"

"github.com/UnderTreeTech/waterdrop/pkg/log"
ws "github.com/UnderTreeTech/waterdrop/pkg/server/http/websocket"
"github.com/gorilla/websocket"
)

var pong = []byte("pong")
Expand All @@ -37,8 +40,8 @@ func main() {

defer log.New(nil).Sync()

srv := http.NewServer(nil)
srv.Upgrade(http.NewWebSocket("/ws", func(ws *http.WebSocket) {
srv := server.New(nil)
srv.Upgrade(ws.NewWebSocket("/ws", func(ws *ws.WebSocket) {
ws.SetPingHandler(func(message string) error {
ws.SetReadDeadline(time.Now().Add(time.Second * 10))
return ws.WriteControl(websocket.PongMessage, pong, time.Now().Add(time.Second))
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA
github.com/cespare/xxhash/v2 v2.1.1 h1:6MnRN8NT7+YBpUIWxHtefFZOKTAPgGjpQSxqLNn0+qY=
github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f h1:WBZRG4aNOuI15bLRrCgN8fCq8E5Xuty6jGbmSNEvSsU=
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
github.com/cockroachdb/datadriven v0.0.0-20190809214429-80d97fb3cbaa h1:OaNxuTZr7kxeODyLWsRMC+OD03aFUH+mW6r2d+MWa5Y=
github.com/cockroachdb/datadriven v0.0.0-20190809214429-80d97fb3cbaa/go.mod h1:zn76sxSg3SzpJ0PPJaLDCu+Bu0Lg3sKTORVIj19EIF8=
Expand Down Expand Up @@ -62,7 +63,9 @@ github.com/emirpasic/gods v1.12.0 h1:QAUIPSaCu4G+POclxeqb3F+WPpdKqFGlw36+yOzGlrg
github.com/emirpasic/gods v1.12.0/go.mod h1:YfzfFFoVP/catgzJb4IKIqXjX78Ha8FMSDh3ymbK86o=
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.4 h1:rEvIZUSZ3fx39WIi3JkQqQBitGwpELBIYWeBVh6wn+E=
github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=
github.com/envoyproxy/protoc-gen-validate v0.1.0 h1:EQciDnbrYxy13PgWoY8AqoxGiPrpgBZ1R8UNe3ddc+A=
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/fortytw2/leaktest v1.3.0 h1:u8491cBMTQ8ft8aeV+adlcytMZylmA5nnwwkRZjI8vw=
Expand Down

0 comments on commit 18581e2

Please sign in to comment.