From 6340e24c17a5fb895964b4584370762d191e8786 Mon Sep 17 00:00:00 2001 From: Kevin Wan Date: Mon, 9 Jan 2023 23:48:31 +0800 Subject: [PATCH] chore: add tests (#2774) --- zrpc/internal/client_test.go | 13 ++++-- zrpc/resolver/internal/discovbuilder_test.go | 46 ++++++++++++++++++++ 2 files changed, 55 insertions(+), 4 deletions(-) diff --git a/zrpc/internal/client_test.go b/zrpc/internal/client_test.go index 9587e9ae..969d6924 100644 --- a/zrpc/internal/client_test.go +++ b/zrpc/internal/client_test.go @@ -4,6 +4,7 @@ import ( "context" "net" "strings" + "sync" "testing" "time" @@ -77,18 +78,22 @@ func TestBuildDialOptions(t *testing.T) { } func TestClientDial(t *testing.T) { + var addr string + var wg sync.WaitGroup + wg.Add(1) server := grpc.NewServer() go func() { - lis, err := net.Listen("tcp", "localhost:54321") + lis, err := net.Listen("tcp", "localhost:0") assert.NoError(t, err) defer lis.Close() + addr = lis.Addr().String() + wg.Done() server.Serve(lis) }() - time.Sleep(time.Millisecond) - - c, err := NewClient("localhost:54321", ClientMiddlewaresConf{ + wg.Wait() + c, err := NewClient(addr, ClientMiddlewaresConf{ Trace: true, Duration: true, Prometheus: true, diff --git a/zrpc/resolver/internal/discovbuilder_test.go b/zrpc/resolver/internal/discovbuilder_test.go index 437c99cf..334e3563 100644 --- a/zrpc/resolver/internal/discovbuilder_test.go +++ b/zrpc/resolver/internal/discovbuilder_test.go @@ -1,12 +1,58 @@ package internal import ( + "fmt" + "net/url" + "strings" "testing" "github.com/stretchr/testify/assert" + "go.etcd.io/etcd/client/v3/mock/mockserver" + "google.golang.org/grpc/resolver" + "google.golang.org/grpc/serviceconfig" ) func TestDiscovBuilder_Scheme(t *testing.T) { var b discovBuilder assert.Equal(t, DiscovScheme, b.Scheme()) } + +func TestDiscovBuilder_Build(t *testing.T) { + servers, err := mockserver.StartMockServers(2) + assert.NoError(t, err) + t.Cleanup(func() { + servers.Stop() + }) + + var addrs []string + for _, server := range servers.Servers { + addrs = append(addrs, server.Address) + } + u, err := url.Parse(fmt.Sprintf("%s://%s", DiscovScheme, strings.Join(addrs, ","))) + assert.NoError(t, err) + + var b discovBuilder + _, err = b.Build(resolver.Target{ + URL: *u, + }, mockClientConn{}, resolver.BuildOptions{}) + assert.Error(t, err) +} + +type mockClientConn struct{} + +func (m mockClientConn) UpdateState(_ resolver.State) error { + return nil +} + +func (m mockClientConn) ReportError(_ error) { +} + +func (m mockClientConn) NewAddress(_ []resolver.Address) { +} + +func (m mockClientConn) NewServiceConfig(_ string) { +} + +func (m mockClientConn) ParseServiceConfig(_ string) *serviceconfig.ParseResult { + return nil +}