Skip to content

Commit

Permalink
test: thrift-streaming (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
Felix021 authored Jan 11, 2024
1 parent 191f8cf commit d09f7be
Show file tree
Hide file tree
Showing 63 changed files with 21,720 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,4 @@ grpc_gen/
go.mod
go.sum
bin
thrift_streaming/binaries
6 changes: 6 additions & 0 deletions .licenserc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,11 @@ header:

paths-ignore:
- 'kitex_gen/**/*.go;'
- 'thrift_streaming/kitex_gen/**/*.go'
- 'thrift_streaming/kitex_gen_slim/**/*.go'
- 'thrift_streaming/kitex_gen_old/**/*.go'
- 'thrift_streaming/kitex_gen_cross/**/*.go'
- 'thrift_streaming/thrift_slim_amd64_test.go'
- 'thrift_streaming/thrift_slim_others_test.go'

comment: on-failure
4 changes: 4 additions & 0 deletions run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,14 @@ packages=(
./generic/http/...
./generic/map/...
./grpc/...
./thrift_streaming/...
)

for pkg in ${packages[@]}
do
if [ "$pkg" == "./thrift_streaming/..." ]; then
./thrift_streaming/generate.sh
fi
if [[ -n $LOCAL_REPO ]]; then
go test -covermode=atomic -coverprofile=${LOCAL_REPO}/coverage.txt.tmp -coverpkg=github.com/cloudwego/kitex/... $pkg
if [[ "$OSTYPE" =~ ^darwin ]];
Expand Down
119 changes: 119 additions & 0 deletions thrift_streaming/generate.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
#!/bin/bash

# Copyright 2023 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.


# Regenerate kitex_gen* directories when there's any related change to codegen (both kitex&thriftgo)

cd `dirname $0`
ROOT=`pwd`

set -e

# Old binaries: kitex <= v0.8.0 && thriftgo <= v0.3.4
OLD=$ROOT/binaries/github-old

# New binaries: kitex >= v0.8.1 && thriftgo >= v0.3.5
NEW=$ROOT/binaries/github-new

# kitex >= v0.8.1 && thriftgo <= v0.3.4
NEW_THRIFTGO_OLD_KITEX=$ROOT/binaries/github-new-thriftgo-old-kitex

module='-module github.com/cloudwego/kitex-tests'
idl=idl/api.thrift

SAVE_PATH=$PATH

# generate with old kitex and thriftgo WITHOUT thrift streaming support
function generate_old() {
echo -e "\n\ngenerate_old\n"
dir=$OLD
export PATH=$OLD:$SAVE_PATH

mkdir -p $dir
GOBIN=$dir go install github.com/cloudwego/kitex/tool/cmd/[email protected]
GOBIN=$dir go install github.com/cloudwego/[email protected]
if [ ! -f "$dir/kitex" -o ! -f "$dir/thriftgo" ]; then
echo "[old] Unable to install kitex or thriftgo to $dir, please check before continue."
exit 1
fi

# Thrift Old
rm -rf kitex_gen_old
kitex -gen-path kitex_gen_old $module $idl
}

function generate_new() {
echo -e "\n\ngenerate_new\n"
dir=$NEW
export PATH=$dir:$SAVE_PATH

mkdir -p $dir
if [ -d "$LOCAL_REPO" ]; then
SAVE_DIR=`pwd`
cd $LOCAL_REPO/tool/cmd/kitex && go build && cp kitex $dir
cd $SAVE_DIR
else
echo -e "[ERROR] Please set local kitex directory in the environment variable $LOCAL_REPO\n"
exit 1
fi
GOBIN=$dir go install github.com/cloudwego/thriftgo@latest

if [ ! -f "$dir/kitex" -o ! -f "$dir/thriftgo" ]; then
echo "[new] Unable to install kitex or thriftgo to $dir, please check before continue."
exit 1
fi

rm -rf kitex_gen

# Thrift
kitex $module $idl

# Thrift Slim
kitex -thrift template=slim -gen-path kitex_gen_slim $module $idl

# KitexPB
kitex $module idl/api.proto

# GRPC
kitex $module idl/api_no_stream.proto
}

function generate_new_thriftgo_old_kitex() {
echo -e "\n\ngenerate_new_thriftgo_old_kitex\n"
dir=$NEW_THRIFTGO_OLD_KITEX
export PATH=$dir:$SAVE_PATH

mkdir -p $dir
GOBIN=$dir go install github.com/cloudwego/kitex/tool/cmd/[email protected]
GOBIN=$dir go install github.com/cloudwego/thriftgo@latest
if [ ! -f "$dir/kitex" -o ! -f "$dir/thriftgo" ]; then
echo "[cross] Unable to install kitex or thriftgo to $dir, please check before continue."
exit 1
fi

rm -rf kitex_gen_cross
# Thrift
kitex -gen-path kitex_gen_cross $module $idl
}

generate_new

generate_new_thriftgo_old_kitex

# regenerate kitex_gen_old (using kitex 0.8.0/thriftgo 0.3.4 without thrift-streaming support)
if [ ! -z "$TEST_GENERATE_OLD" ]; then
generate_old
fi
97 changes: 97 additions & 0 deletions thrift_streaming/grpcpb_handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// Copyright 2023 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 thrift_streaming

import (
"context"
"strconv"

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

"github.com/cloudwego/kitex-tests/thrift_streaming/kitex_gen/grpc_pb"
)

type GRPCPBServiceImpl struct{}

func (G *GRPCPBServiceImpl) Echo(stream grpc_pb.PBService_EchoServer) (err error) {
klog.Infof("[GRPCPBServiceImpl.Echo] Echo called")
count := GetInt(stream.Context(), KeyCount, 0)
klog.Infof("count: %d", count)
for i := 0; i < count; i++ {
var req *grpc_pb.Request
if req, err = stream.Recv(); err != nil {
klog.Infof("[GRPCPBServiceImpl.Echo] recv error: %v", err)
return err
}
klog.Infof("[GRPCPBServiceImpl.Echo] recv success: %s", req.Message)

resp := &grpc_pb.Response{
Message: req.Message,
}
if err = stream.Send(resp); err != nil {
klog.Infof("[GRPCPBServiceImpl.Echo] send error: %v", err)
return
}
klog.Infof("[GRPCPBServiceImpl.Echo] send success: %v", resp)
}
return
}

func (G *GRPCPBServiceImpl) EchoClient(stream grpc_pb.PBService_EchoClientServer) (err error) {
klog.Infof("[GRPCPBServiceImpl.EchoClient] EchoClient called")
count := GetInt(stream.Context(), KeyCount, 0)
klog.Infof("count: %d", count)
for i := 0; i < count; i++ {
var req *grpc_pb.Request
if req, err = stream.Recv(); err != nil {
klog.Infof("[GRPCPBServiceImpl.EchoClient] recv error: %v", err)
return err
}
klog.Infof("[GRPCPBServiceImpl.EchoClient] recv success: %s", req.Message)
}

resp := &grpc_pb.Response{
Message: strconv.Itoa(count),
}
err = stream.SendAndClose(resp)
klog.Infof("[GRPCPBServiceImpl.EchoClient] send: err = %v, resp = %v", err, resp)
return err
}

func (G *GRPCPBServiceImpl) EchoServer(req *grpc_pb.Request, stream grpc_pb.PBService_EchoServerServer) (err error) {
klog.Infof("[GRPCPBServiceImpl.EchoServer] recv req = %s", req.Message)
count := GetInt(stream.Context(), KeyCount, 0)
klog.Infof("count: %d", count)
for i := 0; i < count; i++ {
resp := &grpc_pb.Response{
Message: req.Message + "-" + strconv.Itoa(i),
}
if err = stream.Send(resp); err != nil {
klog.Infof("[GRPCPBServiceImpl.EchoServer] send error: %v", err)
return err
}
klog.Infof("[GRPCPBServiceImpl.EchoServer] send success: %s", resp.Message)
}
return nil
}

func (G *GRPCPBServiceImpl) EchoPingPong(ctx context.Context, req *grpc_pb.Request) (resp *grpc_pb.Response, err error) {
klog.Infof("[GRPCPBServiceImpl.EchoPingPong] recv: %s", req.Message)
resp = &grpc_pb.Response{
Message: req.Message,
}
klog.Infof("[GRPCPBServiceImpl.EchoPingPong] send: %s", resp.Message)
return resp, nil
}
Loading

0 comments on commit d09f7be

Please sign in to comment.