go-zero/zrpc/internal/rpcserver_test.go

46 lines
1017 B
Go
Raw 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"
"github.com/stretchr/testify/assert"
"github.com/zeromicro/go-zero/core/stat"
"github.com/zeromicro/go-zero/zrpc/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) {
2020-08-25 22:42:42 +08:00
metrics := stat.NewMetrics("foo")
2021-10-08 17:31:37 +08:00
server := NewRpcServer("localhost:54321", WithMetrics(metrics))
server.SetName("mock")
var wg sync.WaitGroup
var grpcServer *grpc.Server
var lock sync.Mutex
wg.Add(1)
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)
}()
wg.Wait()
lock.Lock()
grpcServer.GracefulStop()
lock.Unlock()
}
func TestRpcServer_WithBadAddress(t *testing.T) {
server := NewRpcServer("localhost:111111")
server.SetName("mock")
err := server.Start(func(server *grpc.Server) {
mock.RegisterDepositServiceServer(server, new(mock.DepositServer))
})
assert.NotNil(t, err)
2020-08-25 22:42:42 +08:00
}