diff --git a/consul.go b/consul.go index 85b932d..c12ce12 100644 --- a/consul.go +++ b/consul.go @@ -33,7 +33,7 @@ func (r *resolvr) Close() { //go:generate ./bin/moq -out mocks_test.go . servicer type servicer interface { - Service(string, string, bool, *api.QueryOptions) ([]*api.ServiceEntry, *api.QueryMeta, error) + ServiceMultipleTags(string, []string, bool, *api.QueryOptions) ([]*api.ServiceEntry, *api.QueryMeta, error) } func watchConsulService(ctx context.Context, s servicer, tgt target, out chan<- []string) { @@ -48,9 +48,9 @@ func watchConsulService(ctx context.Context, s servicer, tgt target, out chan<- go func() { var lastIndex uint64 for { - ss, meta, err := s.Service( + ss, meta, err := s.ServiceMultipleTags( tgt.Service, - tgt.Tag, + tgt.Tags, tgt.Healthy, &api.QueryOptions{ WaitIndex: lastIndex, diff --git a/consul_test.go b/consul_test.go index 40501ef..ed69b33 100644 --- a/consul_test.go +++ b/consul_test.go @@ -62,7 +62,7 @@ func TestWatchConsulService(t *testing.T) { errorFromService error want []string }{ - {"simple", target{Service: "svc", Wait: time.Second}, + {"simple", target{Service: "svc", Tags: []string{"foo", "bar"}, Wait: time.Second}, []*api.ServiceEntry{ { Service: &api.AgentService{Address: "127.0.0.1", Port: 1024}, @@ -92,9 +92,9 @@ func TestWatchConsulService(t *testing.T) { } }() fconsul := &servicerMock{ - ServiceFunc: func(s1, s2 string, b bool, queryOptions *api.QueryOptions) ([]*api.ServiceEntry, *api.QueryMeta, error) { + ServiceMultipleTagsFunc: func(s1 string, s2 []string, b bool, queryOptions *api.QueryOptions) ([]*api.ServiceEntry, *api.QueryMeta, error) { require.Equal(t, tt.tgt.Service, s1) - require.Equal(t, tt.tgt.Tag, s2) + require.Equal(t, tt.tgt.Tags, s2) require.Equal(t, tt.tgt.Healthy, b) require.Equal(t, tt.tgt.Near, queryOptions.Near) require.Equal(t, tt.tgt.Wait, queryOptions.WaitTime) diff --git a/mocks_test.go b/mocks_test.go index 24bc811..61c7d9b 100644 --- a/mocks_test.go +++ b/mocks_test.go @@ -18,8 +18,8 @@ var _ servicer = &servicerMock{} // // // make and configure a mocked servicer // mockedservicer := &servicerMock{ -// ServiceFunc: func(s1 string, s2 string, b bool, queryOptions *api.QueryOptions) ([]*api.ServiceEntry, *api.QueryMeta, error) { -// panic("mock out the Service method") +// ServiceMultipleTagsFunc: func(s string, strings []string, b bool, queryOptions *api.QueryOptions) ([]*api.ServiceEntry, *api.QueryMeta, error) { +// panic("mock out the ServiceMultipleTags method") // }, // } // @@ -28,66 +28,66 @@ var _ servicer = &servicerMock{} // // } type servicerMock struct { - // ServiceFunc mocks the Service method. - ServiceFunc func(s1 string, s2 string, b bool, queryOptions *api.QueryOptions) ([]*api.ServiceEntry, *api.QueryMeta, error) + // ServiceMultipleTagsFunc mocks the ServiceMultipleTags method. + ServiceMultipleTagsFunc func(s string, strings []string, b bool, queryOptions *api.QueryOptions) ([]*api.ServiceEntry, *api.QueryMeta, error) // calls tracks calls to the methods. calls struct { - // Service holds details about calls to the Service method. - Service []struct { - // S1 is the s1 argument value. - S1 string - // S2 is the s2 argument value. - S2 string + // ServiceMultipleTags holds details about calls to the ServiceMultipleTags method. + ServiceMultipleTags []struct { + // S is the s argument value. + S string + // Strings is the strings argument value. + Strings []string // B is the b argument value. B bool // QueryOptions is the queryOptions argument value. QueryOptions *api.QueryOptions } } - lockService sync.RWMutex + lockServiceMultipleTags sync.RWMutex } -// Service calls ServiceFunc. -func (mock *servicerMock) Service(s1 string, s2 string, b bool, queryOptions *api.QueryOptions) ([]*api.ServiceEntry, *api.QueryMeta, error) { - if mock.ServiceFunc == nil { - panic("servicerMock.ServiceFunc: method is nil but servicer.Service was just called") +// ServiceMultipleTags calls ServiceMultipleTagsFunc. +func (mock *servicerMock) ServiceMultipleTags(s string, strings []string, b bool, queryOptions *api.QueryOptions) ([]*api.ServiceEntry, *api.QueryMeta, error) { + if mock.ServiceMultipleTagsFunc == nil { + panic("servicerMock.ServiceMultipleTagsFunc: method is nil but servicer.ServiceMultipleTags was just called") } callInfo := struct { - S1 string - S2 string + S string + Strings []string B bool QueryOptions *api.QueryOptions }{ - S1: s1, - S2: s2, + S: s, + Strings: strings, B: b, QueryOptions: queryOptions, } - mock.lockService.Lock() - mock.calls.Service = append(mock.calls.Service, callInfo) - mock.lockService.Unlock() - return mock.ServiceFunc(s1, s2, b, queryOptions) + mock.lockServiceMultipleTags.Lock() + mock.calls.ServiceMultipleTags = append(mock.calls.ServiceMultipleTags, callInfo) + mock.lockServiceMultipleTags.Unlock() + return mock.ServiceMultipleTagsFunc(s, strings, b, queryOptions) } -// ServiceCalls gets all the calls that were made to Service. +// ServiceMultipleTagsCalls gets all the calls that were made to ServiceMultipleTags. // Check the length with: // -// len(mockedservicer.ServiceCalls()) -func (mock *servicerMock) ServiceCalls() []struct { - S1 string - S2 string +// len(mockedservicer.ServiceMultipleTagsCalls()) +func (mock *servicerMock) ServiceMultipleTagsCalls() []struct { + S string + Strings []string B bool QueryOptions *api.QueryOptions } { var calls []struct { - S1 string - S2 string + S string + Strings []string B bool QueryOptions *api.QueryOptions } - mock.lockService.RLock() - calls = mock.calls.Service - mock.lockService.RUnlock() + mock.lockServiceMultipleTags.RLock() + calls = mock.calls.ServiceMultipleTags + mock.lockServiceMultipleTags.RUnlock() return calls } diff --git a/target.go b/target.go index 703bbad..26ce3e8 100644 --- a/target.go +++ b/target.go @@ -20,7 +20,7 @@ type target struct { Wait time.Duration `form:"wait"` Timeout time.Duration `form:"timeout"` MaxBackoff time.Duration `form:"max-backoff"` - Tag string `form:"tag"` + Tags []string `form:"tag"` Near string `form:"near"` Limit int `form:"limit"` Healthy bool `form:"healthy"` @@ -34,10 +34,11 @@ type target struct { } func (t *target) String() string { - return fmt.Sprintf("service='%s' healthy='%t' tag='%s'", t.Service, t.Healthy, t.Tag) + return fmt.Sprintf("service='%s' healthy='%t' tag='%+v'", t.Service, t.Healthy, t.Tags) } -// parseURL with parameters +// parseURL with parameters +// // see README.md for the actual format // URL schema will stay stable in the future for backward compatibility func parseURL(u string) (target, error) { diff --git a/target_test.go b/target_test.go index 8fd447c..71b9a7d 100644 --- a/target_test.go +++ b/target_test.go @@ -24,7 +24,7 @@ func Test_parseURL(t *testing.T) { }, false, }, - {"all-args", "consul://user:password@127.0.0.127:8555/my-service?wait=14s&near=host&insecure=true&limit=1&tag=production&token=test_token&max-backoff=2s&dc=xx&allow-stale=true&require-consistent=true", + {"all-args", "consul://user:password@127.0.0.127:8555/my-service?wait=14s&near=host&insecure=true&limit=1&tag=production&tag=extra_tag&token=test_token&max-backoff=2s&dc=xx&allow-stale=true&require-consistent=true", target{ Addr: "127.0.0.127:8555", User: "user", @@ -34,7 +34,7 @@ func Test_parseURL(t *testing.T) { Wait: 14 * time.Second, TLSInsecure: true, Limit: 1, - Tag: "production", + Tags: []string{"production", "extra_tag"}, Token: "test_token", MaxBackoff: 2 * time.Second, Dc: "xx",