go-zero/zrpc/internal/rpcserver_test.go

55 lines
1.1 KiB
Go
Raw Permalink Normal View History

2020-08-25 22:42:42 +08:00
package internal
import (
2021-10-08 17:31:37 +08:00
"sync"
2020-08-25 22:42:42 +08:00
"testing"
"time"
2020-08-25 22:42:42 +08:00
"github.com/stretchr/testify/assert"
"github.com/zeromicro/go-zero/core/proc"
"github.com/zeromicro/go-zero/internal/mock"
2021-10-08 17:31:37 +08:00
"google.golang.org/grpc"
2020-08-25 22:42:42 +08:00
)
2021-10-08 17:31:37 +08:00
func TestRpcServer(t *testing.T) {
server := NewRpcServer("localhost:54321", WithRpcHealth(true))
2021-10-08 17:31:37 +08:00
server.SetName("mock")
var wg, wgDone sync.WaitGroup
2021-10-08 17:31:37 +08:00
var grpcServer *grpc.Server
var lock sync.Mutex
wg.Add(1)
wgDone.Add(1)
2021-10-08 17:31:37 +08:00
go func() {
err := server.Start(func(server *grpc.Server) {
lock.Lock()
mock.RegisterDepositServiceServer(server, new(mock.DepositServer))
grpcServer = server
lock.Unlock()
wg.Done()
})
assert.Nil(t, err)
wgDone.Done()
2021-10-08 17:31:37 +08:00
}()
wg.Wait()
time.Sleep(100 * time.Millisecond)
2021-10-08 17:31:37 +08:00
lock.Lock()
grpcServer.GracefulStop()
lock.Unlock()
proc.Shutdown()
wgDone.Wait()
2021-10-08 17:31:37 +08:00
}
func TestRpcServer_WithBadAddress(t *testing.T) {
server := NewRpcServer("localhost:111111", WithRpcHealth(true))
2021-10-08 17:31:37 +08:00
server.SetName("mock")
err := server.Start(func(server *grpc.Server) {
mock.RegisterDepositServiceServer(server, new(mock.DepositServer))
})
assert.NotNil(t, err)
proc.WrapUp()
2020-08-25 22:42:42 +08:00
}