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"
|
2023-04-29 20:36:29 +08:00
|
|
|
"time"
|
2020-08-25 22:42:42 +08:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2023-01-24 13:43:13 +08:00
|
|
|
"github.com/zeromicro/go-zero/core/proc"
|
2023-04-22 13:23:47 +08:00
|
|
|
"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) {
|
2024-08-08 22:37:19 +08:00
|
|
|
server := NewRpcServer("localhost:54321", WithRpcHealth(true))
|
2021-10-08 17:31:37 +08:00
|
|
|
server.SetName("mock")
|
2023-02-11 14:21:39 +08:00
|
|
|
var wg, wgDone sync.WaitGroup
|
2021-10-08 17:31:37 +08:00
|
|
|
var grpcServer *grpc.Server
|
|
|
|
var lock sync.Mutex
|
|
|
|
wg.Add(1)
|
2023-02-11 14:21:39 +08:00
|
|
|
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)
|
2023-02-11 14:21:39 +08:00
|
|
|
wgDone.Done()
|
2021-10-08 17:31:37 +08:00
|
|
|
}()
|
|
|
|
|
|
|
|
wg.Wait()
|
2023-04-29 20:36:29 +08:00
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
|
2021-10-08 17:31:37 +08:00
|
|
|
lock.Lock()
|
|
|
|
grpcServer.GracefulStop()
|
|
|
|
lock.Unlock()
|
2023-02-11 14:21:39 +08:00
|
|
|
|
2023-10-23 21:03:05 +08:00
|
|
|
proc.Shutdown()
|
2023-02-11 14:21:39 +08:00
|
|
|
wgDone.Wait()
|
2021-10-08 17:31:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRpcServer_WithBadAddress(t *testing.T) {
|
2024-08-08 22:37:19 +08:00
|
|
|
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)
|
2023-02-11 14:21:39 +08:00
|
|
|
|
|
|
|
proc.WrapUp()
|
2020-08-25 22:42:42 +08:00
|
|
|
}
|