Skip to content

Commit

Permalink
Bump dependencies versions. Closes #15 (#18)
Browse files Browse the repository at this point in the history
* update dependencies

* update versions for manual testing
  • Loading branch information
mbobakov authored Jan 9, 2020
1 parent 3fae69f commit 6f34c3d
Show file tree
Hide file tree
Showing 9 changed files with 297 additions and 740 deletions.
618 changes: 0 additions & 618 deletions charlatan_for_test.go

This file was deleted.

4 changes: 3 additions & 1 deletion consul.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ func (r *resolvr) Close() {
r.cancelFunc()
}

//go:generate mockgen -package mocks -destination internal/mocks/resolverClientConn.go google.golang.org/grpc/resolver ClientConn
//go:generate mockgen -package mocks -destination internal/mocks/servicer.go -source consul.go servicer
type servicer interface {
Service(string, string, bool, *api.QueryOptions) ([]*api.ServiceEntry, *api.QueryMeta, error)
}
Expand Down Expand Up @@ -106,7 +108,7 @@ func populateEndpoints(ctx context.Context, clientConn resolver.ClientConn, inpu
conns = append(conns, resolver.Address{Addr: c})
}
sort.Sort(byAddressString(conns)) // Don't replace the same address list in the balancer
clientConn.NewAddress(conns)
clientConn.UpdateState(resolver.State{Addresses: conns})
case <-ctx.Done():
grpclog.Info("[Consul resolver] Watch has been finished")
return
Expand Down
122 changes: 68 additions & 54 deletions consul_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,48 +5,50 @@ import (
"testing"
"time"

"github.com/golang/mock/gomock"
"github.com/hashicorp/consul/api"
"github.com/pkg/errors"
"github.com/mbobakov/grpc-consul-resolver/internal/mocks"
"github.com/stretchr/testify/require"
"google.golang.org/grpc/resolver"
)

func TestPopulateEndpoints(t *testing.T) {
tests := []struct {
name string
input [][]string
want []resolver.Address
name string
input [][]string
wantCalls [][]resolver.Address
}{
{"one", [][]string{{"127.0.0.1:50051"}}, []resolver.Address{{Addr: "127.0.0.1:50051"}}},
{"sorted",
[][]string{
{"227.0.0.1:50051", "127.0.0.1:50051"},
},
[]resolver.Address{
{Addr: "127.0.0.1:50051"},
{Addr: "227.0.0.1:50051"},
{"one",
[][]string{{"127.0.0.1:50051"}},
[][]resolver.Address{
[]resolver.Address{
{Addr: "127.0.0.1:50051"},
},
},
},
{"multy",
{"sorted",
[][]string{
{"127.0.0.1:50051"},
{"127.0.0.1:50052"},
{"127.0.0.1:50053"},
{"227.0.0.1:50051", "127.0.0.1:50051"},
},
[]resolver.Address{
{Addr: "127.0.0.1:50053"},
[][]resolver.Address{
[]resolver.Address{
{Addr: "127.0.0.1:50051"},
{Addr: "227.0.0.1:50051"},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
var (
got []resolver.Address
in = make(chan []string, len(tt.input))
in = make(chan []string, len(tt.input))
)
fcc := NewFakeClientConnDefaultFatal(t)
fcc.NewAddressHook = func(cc []resolver.Address) {
got = cc

fcc := mocks.NewMockClientConn(ctrl)
for _, aa := range tt.wantCalls {
fcc.EXPECT().UpdateState(resolver.State{Addresses: aa}).Times(1)
}

ctx, cancel := context.WithCancel(context.Background())
Expand All @@ -56,8 +58,6 @@ func TestPopulateEndpoints(t *testing.T) {
in <- i
}
time.Sleep(time.Millisecond)
require.True(t, fcc.NewAddressCalledN(len(tt.input)))
require.Equal(t, tt.want, got)
})
}
}
Expand All @@ -66,21 +66,27 @@ func TestWatchConsulService(t *testing.T) {
tests := []struct {
name string
tgt target
addr []string // port increased with 1 per invocation
startPort int
times uint64
errorFromService bool
services []*api.ServiceEntry
errorFromService error
want []string
}{
{"simple", target{Service: "svc", Wait: time.Second}, []string{"127.0.0.1"}, 100, 3, false, []string{"127.0.0.1:102"}},
{"error", target{}, []string{"127.0.0.1"}, 100, 2, true, nil},
{"limit", target{Limit: 1}, []string{"127.0.0.1", "127.0.0.2", "127.0.0.3"}, 100, 1, false, []string{"127.0.0.1:100"}},
{"limitOver", target{Limit: 10}, []string{"127.0.0.1"}, 100, 1, false, []string{"127.0.0.1:100"}},
{"simple", target{Service: "svc", Wait: time.Second},
[]*api.ServiceEntry{
&api.ServiceEntry{
Service: &api.AgentService{Address: "127.0.0.1", Port: 1024},
},
},
nil,
[]string{"127.0.0.1:1024"},
},
// TODO: Add more tests-cases
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
ctrl := gomock.NewController(t)
defer ctrl.Finish()

var (
got []string
Expand All @@ -95,30 +101,38 @@ func TestWatchConsulService(t *testing.T) {
}
}
}()
fconsul := NewFakeservicerDefaultFatal(t)

fconsul.ServiceHook = func(s, tag string, hlz bool, q *api.QueryOptions) ([]*api.ServiceEntry, *api.QueryMeta, error) {
require.Equal(t, tt.tgt.Service, s)
require.Equal(t, tt.tgt.Tag, tag)
require.Equal(t, tt.tgt.Healthy, hlz)
require.Equal(t, tt.tgt.Wait, q.WaitTime)
if q.WaitIndex >= tt.times {
select {}
}
if tt.errorFromService {
return nil, &api.QueryMeta{LastIndex: q.WaitIndex + 1}, errors.New("Error")
}
var rr []*api.ServiceEntry
for _, a := range tt.addr {
rr = append(rr, &api.ServiceEntry{Service: &api.AgentService{Address: a, Port: tt.startPort + int(q.WaitIndex)}})
}
return rr, &api.QueryMeta{LastIndex: q.WaitIndex + 1}, nil
}
fconsul := mocks.NewMockservicer(ctrl)
fconsul.EXPECT().Service(tt.tgt.Service, tt.tgt.Tag, tt.tgt.Healthy, &api.QueryOptions{
WaitIndex: 0,
Near: tt.tgt.Near,
WaitTime: tt.tgt.Wait,
Datacenter: tt.tgt.Dc,
AllowStale: tt.tgt.AllowStale,
RequireConsistent: tt.tgt.RequireConsistent,
}).
Times(1).
Return(tt.services, &api.QueryMeta{LastIndex: 1}, tt.errorFromService)
fconsul.EXPECT().Service(tt.tgt.Service, tt.tgt.Tag, tt.tgt.Healthy, &api.QueryOptions{
WaitIndex: 1,
Near: tt.tgt.Near,
WaitTime: tt.tgt.Wait,
Datacenter: tt.tgt.Dc,
AllowStale: tt.tgt.AllowStale,
RequireConsistent: tt.tgt.RequireConsistent,
}).
Do(
func(svc string, tag string, h bool, opt *api.QueryOptions) ([]*api.ServiceEntry, *api.QueryMeta, error) {
if opt.WaitIndex > 0 {
select {}
}
return tt.services, &api.QueryMeta{LastIndex: 1}, tt.errorFromService
},
).Times(1).
Return(tt.services, &api.QueryMeta{LastIndex: 1}, tt.errorFromService)

go watchConsulService(ctx, fconsul, tt.tgt, out)
time.Sleep(15 * time.Millisecond)
time.Sleep(5 * time.Millisecond)

require.True(t, fconsul.ServiceCalledN(int(tt.times)))
require.Equal(t, tt.want, got)
})
}
Expand Down
28 changes: 15 additions & 13 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
module github.com/mbobakov/grpc-consul-resolver

go 1.13

require (
github.com/armon/go-metrics v0.3.0 // indirect
github.com/go-playground/form v3.1.3+incompatible
github.com/hashicorp/consul/api v1.1.0
github.com/hashicorp/go-msgpack v0.5.5 // indirect
github.com/hashicorp/memberlist v0.1.5 // indirect
github.com/jpillora/backoff v0.0.0-20180909062703-3050d21c67d7
github.com/go-playground/form v3.1.4+incompatible
github.com/golang/mock v1.1.1
github.com/google/btree v1.0.0 // indirect
github.com/hashicorp/consul/api v1.3.0
github.com/jpillora/backoff v1.0.0
github.com/kr/pretty v0.1.0 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/pkg/errors v0.8.1
github.com/sirupsen/logrus v1.1.1
github.com/stretchr/testify v1.3.0
golang.org/x/crypto v0.0.0-20190325154230-a5d413f7728c // indirect
golang.org/x/net v0.0.0-20190403144856-b630fd6fe46b // indirect
golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6 // indirect
golang.org/x/sys v0.0.0-20190403152447-81d4e9dc473e // indirect
google.golang.org/grpc v1.2.1-0.20181011201045-c05280cc73cd
github.com/sirupsen/logrus v1.4.2
github.com/stretchr/testify v1.4.0
golang.org/x/net v0.0.0-20190522155817-f3200d17e092 // indirect
google.golang.org/grpc v1.26.0
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
gopkg.in/go-playground/assert.v1 v1.2.1 // indirect
gopkg.in/yaml.v2 v2.2.4 // indirect
)
Loading

0 comments on commit 6f34c3d

Please sign in to comment.