chore: add tests (#2774)

master
Kevin Wan 2 years ago committed by GitHub
parent 74e0676617
commit 6340e24c17
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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,

@ -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
}

Loading…
Cancel
Save