gorush/rpc/server_test.go

54 lines
1.1 KiB
Go
Raw Normal View History

package rpc
import (
"context"
"testing"
2020-03-10 13:10:18 +00:00
"github.com/appleboy/gorush/gorush"
2021-07-13 08:32:39 +00:00
"github.com/appleboy/gorush/logx"
2020-03-10 13:10:18 +00:00
"google.golang.org/grpc"
"google.golang.org/grpc/connectivity"
)
const gRPCAddr = "localhost:9000"
func TestGracefulShutDownGRPCServer(t *testing.T) {
// server configs
2021-07-13 08:32:39 +00:00
logx.InitLog(
gorush.PushConf.Log.AccessLevel,
gorush.PushConf.Log.AccessLog,
gorush.PushConf.Log.ErrorLevel,
gorush.PushConf.Log.ErrorLog,
)
gorush.PushConf.GRPC.Enabled = true
gorush.PushConf.GRPC.Port = "9000"
gorush.PushConf.Log.Format = "json"
// Run gRPC server
ctx, gRPCContextCancel := context.WithCancel(context.Background())
go func() {
if err := RunGRPCServer(ctx); err != nil {
panic(err)
}
}()
// gRPC client conn
conn, err := grpc.Dial(
gRPCAddr,
grpc.WithInsecure(),
grpc.WithDefaultCallOptions(grpc.WaitForReady(true)),
) // wait for server ready
if err != nil {
t.Error(err)
}
// Stop gRPC server
go gRPCContextCancel()
// wait for client connection would be closed
for conn.GetState() != connectivity.TransientFailure {
}
conn.Close()
}