Skip to content

Commit

Permalink
feat: add kitex grpc abc test (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
YangruiEmma authored Jan 18, 2024
1 parent 41bc566 commit 75c4233
Show file tree
Hide file tree
Showing 108 changed files with 574 additions and 16,293 deletions.
32 changes: 32 additions & 0 deletions common/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright 2024 CloudWeGo Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package common

import (
"net"
"time"

"github.com/cloudwego/kitex/pkg/klog"
)

func WaitServer(hostPort string) {
for begin := time.Now(); time.Since(begin) < time.Second; {
if _, err := net.Dial("tcp", hostPort); err == nil {
klog.Infof("server %s is up", hostPort)
return
}
time.Sleep(time.Millisecond * 10)
}
}
67 changes: 67 additions & 0 deletions kitexgrpc/abc/abc_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright 2024 CloudWeGo Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package abc

import (
"testing"

"github.com/cloudwego/kitex-tests/common"
"github.com/cloudwego/kitex-tests/kitexgrpc/abc/consts"
"github.com/cloudwego/kitex-tests/kitexgrpc/abc/servicea"
"github.com/cloudwego/kitex-tests/kitexgrpc/abc/serviceb"
"github.com/cloudwego/kitex-tests/kitexgrpc/abc/servicec"
"github.com/cloudwego/kitex-tests/pkg/test"
)

func TestMain(m *testing.M) {
svrC := servicec.InitServiceCServer()
go func() {
defer svrC.Stop()
err := svrC.Run()
if err != nil {
panic(err)
}
}()
svrB := serviceb.InitServiceBServer()
go func() {
defer svrB.Stop()
err := svrB.Run()
if err != nil {
panic(err)
}
}()

common.WaitServer(consts.ServiceCAddr)
common.WaitServer(consts.ServiceBAddr)

m.Run()
}

func TestABC(t *testing.T) {
cli, err := servicea.InitServiceAClient()
test.Assert(t, err == nil, err)

err = servicea.SendUnary(cli)
test.Assert(t, err == nil, err)

err = servicea.SendClientStreaming(cli)
test.Assert(t, err == nil, err)

err = servicea.SendServerStreaming(cli)
test.Assert(t, err == nil, err)

err = servicea.SendBidiStreaming(cli)
test.Assert(t, err == nil, err)
}
20 changes: 20 additions & 0 deletions kitexgrpc/abc/consts/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright 2024 CloudWeGo Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package consts

const (
ServiceBAddr = "localhost:9990"
ServiceCAddr = "localhost:9991"
)
29 changes: 29 additions & 0 deletions kitexgrpc/abc/servicea/main/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright 2024 CloudWeGo Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import "github.com/cloudwego/kitex-tests/kitexgrpc/abc/servicea"

func main() {
cli, err := servicea.InitServiceAClient()
if err != nil {
panic(err)
}

servicea.SendUnary(cli)
servicea.SendClientStreaming(cli)
servicea.SendServerStreaming(cli)
servicea.SendBidiStreaming(cli)
}
114 changes: 114 additions & 0 deletions kitexgrpc/abc/servicea/servicea.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
// Copyright 2024 CloudWeGo Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package servicea

import (
"context"
"fmt"
"io"
"time"

"github.com/cloudwego/kitex-tests/kitex_gen/protobuf/grpc_demo"
"github.com/cloudwego/kitex-tests/kitex_gen/protobuf/grpc_demo/servicea"
"github.com/cloudwego/kitex-tests/kitexgrpc/abc/consts"
"github.com/cloudwego/kitex/client"
"github.com/cloudwego/kitex/pkg/endpoint"
"github.com/cloudwego/kitex/transport"
)

func myMiddleware(next endpoint.Endpoint) endpoint.Endpoint {
return func(ctx context.Context, req, resp interface{}) (err error) {
err = next(ctx, req, resp)
return err
}
}

func InitServiceAClient() (servicea.Client, error) {
cli, err := servicea.NewClient("serviceB",
client.WithRPCTimeout(1000*time.Millisecond),
client.WithHostPorts(consts.ServiceBAddr), client.WithTransportProtocol(transport.GRPC),
client.WithMiddleware(myMiddleware))
return cli, err
}

func SendUnary(cli servicea.Client) error {
// case 1: unary
req := &grpc_demo.Request{Name: "service_a_CallUnary"}
resp, err := cli.CallUnary(context.Background(), req)
fmt.Println(resp, err)
return err
}

func SendClientStreaming(cli servicea.Client) error {
// case 2: client streaming
req := &grpc_demo.Request{Name: "service_a_CallClientStream"}
stream1, err := cli.CallClientStream(context.Background())
if err != nil {
return err
}
for i := 0; i < 3; i++ {
if err := stream1.Send(req); err != nil {
return err
}
}
_, err = stream1.CloseAndRecv()
return err
}

func SendServerStreaming(cli servicea.Client) error {
// case 3: server streaming
req := &grpc_demo.Request{Name: "service_a_CallServerStream"}
stream2, err := cli.CallServerStream(context.Background(), req)
if err != nil {
return err
}
for {
reply, err := stream2.Recv()
if err != nil {
if err == io.EOF {
break
}
return err
}
fmt.Println(reply)
}
return err
}

func SendBidiStreaming(cli servicea.Client) error {
// case 4: bidi streaming
req := &grpc_demo.Request{Name: "service_a_CallBidiStream"}
stream3, err := cli.CallBidiStream(context.Background())
if err != nil {
return err
}
for i := 0; i < 3; i++ {
if err := stream3.Send(req); err != nil {
return err
}
}
stream3.Close()
for {
reply, err := stream3.Recv()
if err != nil {
if err == io.EOF {
break
}
return err
}
fmt.Println("serviceA", reply)
}
return err
}
24 changes: 24 additions & 0 deletions kitexgrpc/abc/serviceb/main/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright 2024 CloudWeGo Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import "github.com/cloudwego/kitex-tests/kitexgrpc/abc/serviceb"

func main() {
svr := serviceb.InitServiceBServer()
defer svr.Stop()
err := svr.Run()
panic(err)
}
Loading

0 comments on commit 75c4233

Please sign in to comment.