From 5bcadb8bb828c9df73b99244681994aed35e889b Mon Sep 17 00:00:00 2001 From: eson <474420502@qq.com> Date: Thu, 5 Mar 2020 05:21:29 +0800 Subject: [PATCH] fix some test bug --- api.go | 27 ++++++++++++++++++++++----- api_test.go | 15 ++++++++------- go.mod | 2 +- go.sum | 6 ++++++ main.go | 22 +--------------------- 5 files changed, 38 insertions(+), 34 deletions(-) diff --git a/api.go b/api.go index 788b25d..f088605 100644 --- a/api.go +++ b/api.go @@ -2,18 +2,35 @@ package main import ( context "context" - proto "edb/proto" + "edb/goproto" + "log" + "net" + + grpc "google.golang.org/grpc" ) // server is used to implement type server struct{} // SayHello implements -func (s *server) SayHello(ctx context.Context, in *proto.TableRequest) (*proto.TableReply, error) { - return &proto.TableReply{Msg: "Hello " + in.Name}, nil +func (s *server) SayHello(ctx context.Context, in *goproto.Request) (*goproto.Reply, error) { + return &goproto.Reply{Msg: "Hello " + in.GetWhat()}, nil } // SayHello implements -func (s *server) CreateTable(ctx context.Context, in *proto.TableRequest) (*proto.TableReply, error) { - return &proto.TableReply{Msg: "Hello " + in.Name}, nil +func (s *server) CreateTable(ctx context.Context, in *goproto.TableRequest) (*goproto.TableReply, error) { + return &goproto.TableReply{Msg: "Hello " + in.Name}, nil +} + +func createTestServer() { + //起服务 + listen, err := net.Listen("tcp", "localhost:"+port) + if err != nil { + log.Fatalf("failed to listen: %v", err) + } + gserver := grpc.NewServer() + goproto.RegisterEasyDataServer(gserver, &server{}) + goproto.RegisterTestServer(gserver, &server{}) + log.Println("create test service : ", port) + gserver.Serve(listen) } diff --git a/api_test.go b/api_test.go index 32b0a2d..d3e094d 100644 --- a/api_test.go +++ b/api_test.go @@ -2,7 +2,7 @@ package main import ( "context" - proto "edb/goproto" + "edb/goproto" "log" "net" "runtime" @@ -25,7 +25,8 @@ func createServer() { log.Fatalf("failed to listen: %v", err) } gserver := grpc.NewServer() - proto.RegisterEasyDataServer(gserver, &server{}) + goproto.RegisterEasyDataServer(gserver, &server{}) + goproto.RegisterTestServer(gserver, &server{}) go gserver.Serve(listen) time.Sleep(time.Millisecond * 50) } @@ -38,10 +39,10 @@ func ClientCall(wg *sync.WaitGroup, COUNT int) { panic(err) } defer conn.Close() - client := proto.NewEasyDataClient(conn) + client := goproto.NewTestClient(conn) for i := 0; i < COUNT; i++ { - reply, err := client.CreateTable(context.Background(), &proto.TableRequest{Name: "user"}) + reply, err := client.SayHello(context.Background(), &goproto.Request{What: "user"}) if err == nil { reply.GetMsg() } @@ -54,7 +55,7 @@ func TestSayHello(t *testing.T) { wg := &sync.WaitGroup{} - GCOUNT := 400000 + GCOUNT := 100000 COUNT := 10 wg.Add(GCOUNT) @@ -75,10 +76,10 @@ func BenchmarkCreateTable(b *testing.B) { panic(err) } defer conn.Close() - client := proto.NewEasyDataClient(conn) + client := goproto.NewEasyDataClient(conn) for i := 0; i < b.N; i++ { - reply, err := client.CreateTable(context.Background(), &proto.TableRequest{Name: "user"}) + reply, err := client.CreateTable(context.Background(), &goproto.TableRequest{Name: "user"}) if err != nil { panic(err) } diff --git a/go.mod b/go.mod index e572630..edac122 100644 --- a/go.mod +++ b/go.mod @@ -7,6 +7,6 @@ require ( golang.org/x/net v0.0.0-20200301022130-244492dfa37a // indirect golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527 // indirect golang.org/x/text v0.3.2 // indirect - google.golang.org/genproto v0.0.0-20200303153909-beee998c1893 // indirect + google.golang.org/genproto v0.0.0-20200304201815-d429ff31ee6c // indirect google.golang.org/grpc v1.27.1 ) diff --git a/go.sum b/go.sum index 0f938b6..d667ee6 100644 --- a/go.sum +++ b/go.sum @@ -1,4 +1,5 @@ cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= +github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= @@ -19,6 +20,7 @@ golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACk golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU= +golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3 h1:XQyxROzUlZH+WIQwySDgnISgOivlhjIEwaQaJEJrrN0= golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= @@ -44,6 +46,7 @@ golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGm golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY= golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= +golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135 h1:5Beo0mZN8dRzgrMMkDp0jc8YXQKx9DiJ2k1dkvGsn5A= golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= @@ -52,10 +55,13 @@ google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55 h1:gSJIx1SDwno+2El google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc= google.golang.org/genproto v0.0.0-20200303153909-beee998c1893 h1:OTjq5CN+5TpMIvzqxSFCjbBX3jNKjX0XOPi4SdBxQU8= google.golang.org/genproto v0.0.0-20200303153909-beee998c1893/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c= +google.golang.org/genproto v0.0.0-20200304201815-d429ff31ee6c h1:Mm69MraVZ+yh1vw8pQOUW4uJkkSEQbbTr076A94lvqs= +google.golang.org/genproto v0.0.0-20200304201815-d429ff31ee6c/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c= google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg= google.golang.org/grpc v1.27.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk= google.golang.org/grpc v1.27.1 h1:zvIju4sqAGvwKspUQOhwnpcqSbzi7/H6QomNNjTL4sk= google.golang.org/grpc v1.27.1/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk= honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= +honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc h1:/hemPrYIhOhy8zYrNj+069zDB68us2sMGsfkFJO0iZs= honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= diff --git a/main.go b/main.go index fdf4cf3..fb5cfc9 100644 --- a/main.go +++ b/main.go @@ -1,34 +1,14 @@ package main import ( - "log" - "net" "runtime" - - proto "edb/goproto" - - grpc "google.golang.org/grpc" ) const ( port = "20051" ) -func CreateServer() { - //起服务 - listen, err := net.Listen("tcp", "localhost:"+port) - if err != nil { - log.Fatalf("failed to listen: %v", err) - } - gserver := grpc.NewServer() - proto.RegisterEasyDataServer(gserver, &server{}) - gserver.Serve(listen) - // time.Sleep(time.Millisecond * 50) -} - func main() { runtime.GOMAXPROCS(runtime.NumCPU()) - // test. - CreateServer() - + createTestServer() }