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" "context"
"net" "net"
"strings" "strings"
"sync"
"testing" "testing"
"time" "time"
@ -77,18 +78,22 @@ func TestBuildDialOptions(t *testing.T) {
} }
func TestClientDial(t *testing.T) { func TestClientDial(t *testing.T) {
var addr string
var wg sync.WaitGroup
wg.Add(1)
server := grpc.NewServer() server := grpc.NewServer()
go func() { go func() {
lis, err := net.Listen("tcp", "localhost:54321") lis, err := net.Listen("tcp", "localhost:0")
assert.NoError(t, err) assert.NoError(t, err)
defer lis.Close() defer lis.Close()
addr = lis.Addr().String()
wg.Done()
server.Serve(lis) server.Serve(lis)
}() }()
time.Sleep(time.Millisecond) wg.Wait()
c, err := NewClient(addr, ClientMiddlewaresConf{
c, err := NewClient("localhost:54321", ClientMiddlewaresConf{
Trace: true, Trace: true,
Duration: true, Duration: true,
Prometheus: true, Prometheus: true,

@ -1,12 +1,58 @@
package internal package internal
import ( import (
"fmt"
"net/url"
"strings"
"testing" "testing"
"github.com/stretchr/testify/assert" "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) { func TestDiscovBuilder_Scheme(t *testing.T) {
var b discovBuilder var b discovBuilder
assert.Equal(t, DiscovScheme, b.Scheme()) 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