2017-11-12 14:44:33 +00:00
|
|
|
package rpc
|
2020-03-10 13:09:03 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
2021-07-16 04:10:34 +00:00
|
|
|
"github.com/appleboy/gorush/config"
|
2021-07-13 08:32:39 +00:00
|
|
|
"github.com/appleboy/gorush/logx"
|
2020-03-10 13:10:18 +00:00
|
|
|
|
2020-03-10 13:09:03 +00:00
|
|
|
"google.golang.org/grpc"
|
|
|
|
"google.golang.org/grpc/connectivity"
|
|
|
|
)
|
|
|
|
|
|
|
|
const gRPCAddr = "localhost:9000"
|
|
|
|
|
2021-07-16 04:10:34 +00:00
|
|
|
func initTest() config.ConfYaml {
|
|
|
|
cfg, _ := config.LoadConf()
|
|
|
|
cfg.Core.Mode = "test"
|
|
|
|
return cfg
|
|
|
|
}
|
|
|
|
|
2020-03-10 13:09:03 +00:00
|
|
|
func TestGracefulShutDownGRPCServer(t *testing.T) {
|
2021-07-16 04:10:34 +00:00
|
|
|
cfg := initTest()
|
2020-03-10 13:09:03 +00:00
|
|
|
// server configs
|
2021-07-13 08:32:39 +00:00
|
|
|
logx.InitLog(
|
2021-07-16 04:10:34 +00:00
|
|
|
cfg.Log.AccessLevel,
|
|
|
|
cfg.Log.AccessLog,
|
|
|
|
cfg.Log.ErrorLevel,
|
|
|
|
cfg.Log.ErrorLog,
|
2021-07-13 08:32:39 +00:00
|
|
|
)
|
2021-07-16 04:10:34 +00:00
|
|
|
cfg.GRPC.Enabled = true
|
|
|
|
cfg.GRPC.Port = "9000"
|
|
|
|
cfg.Log.Format = "json"
|
2020-03-10 13:09:03 +00:00
|
|
|
|
|
|
|
// Run gRPC server
|
|
|
|
ctx, gRPCContextCancel := context.WithCancel(context.Background())
|
|
|
|
go func() {
|
2021-07-16 04:10:34 +00:00
|
|
|
if err := RunGRPCServer(ctx, cfg); err != nil {
|
2020-03-10 13:09:03 +00:00
|
|
|
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()
|
|
|
|
}
|