github.com/thanos-io/thanos@v0.32.5/pkg/discovery/memcache/provider_test.go (about) 1 // Copyright (c) The Thanos Authors. 2 // Licensed under the Apache License 2.0. 3 4 package memcache 5 6 import ( 7 "context" 8 "sort" 9 "testing" 10 "time" 11 12 "github.com/pkg/errors" 13 14 "github.com/go-kit/log" 15 16 "github.com/efficientgo/core/testutil" 17 ) 18 19 func TestProviderUpdatesAddresses(t *testing.T) { 20 ctx := context.TODO() 21 clusters := []string{"memcached-cluster-1", "memcached-cluster-2"} 22 provider := NewProvider(log.NewNopLogger(), nil, 5*time.Second) 23 resolver := mockResolver{ 24 configs: map[string]*clusterConfig{ 25 "memcached-cluster-1": {nodes: []node{{dns: "dns-1", ip: "ip-1", port: 11211}}}, 26 "memcached-cluster-2": {nodes: []node{{dns: "dns-2", ip: "ip-2", port: 8080}}}, 27 }, 28 } 29 provider.resolver = &resolver 30 31 testutil.Ok(t, provider.Resolve(ctx, clusters)) 32 addresses := provider.Addresses() 33 sort.Strings(addresses) 34 testutil.Equals(t, []string{"dns-1:11211", "dns-2:8080"}, addresses) 35 36 resolver.configs = map[string]*clusterConfig{ 37 "memcached-cluster-1": {nodes: []node{{dns: "dns-1", ip: "ip-1", port: 11211}, {dns: "dns-3", ip: "ip-3", port: 11211}}}, 38 "memcached-cluster-2": {nodes: []node{{dns: "dns-2", ip: "ip-2", port: 8080}}}, 39 } 40 41 testutil.Ok(t, provider.Resolve(ctx, clusters)) 42 addresses = provider.Addresses() 43 sort.Strings(addresses) 44 testutil.Equals(t, []string{"dns-1:11211", "dns-2:8080", "dns-3:11211"}, addresses) 45 } 46 47 func TestProviderDoesNotUpdateAddressIfFailed(t *testing.T) { 48 ctx := context.TODO() 49 clusters := []string{"memcached-cluster-1", "memcached-cluster-2"} 50 provider := NewProvider(log.NewNopLogger(), nil, 5*time.Second) 51 resolver := mockResolver{ 52 configs: map[string]*clusterConfig{ 53 "memcached-cluster-1": {nodes: []node{{dns: "dns-1", ip: "ip-1", port: 11211}}}, 54 "memcached-cluster-2": {nodes: []node{{dns: "dns-2", ip: "ip-2", port: 8080}}}, 55 }, 56 } 57 provider.resolver = &resolver 58 59 testutil.Ok(t, provider.Resolve(ctx, clusters)) 60 addresses := provider.Addresses() 61 sort.Strings(addresses) 62 testutil.Equals(t, []string{"dns-1:11211", "dns-2:8080"}, addresses) 63 64 resolver.configs = nil 65 resolver.err = errors.New("oops") 66 67 testutil.NotOk(t, provider.Resolve(ctx, clusters)) 68 addresses = provider.Addresses() 69 sort.Strings(addresses) 70 testutil.Equals(t, []string{"dns-1:11211", "dns-2:8080"}, addresses) 71 } 72 73 type mockResolver struct { 74 configs map[string]*clusterConfig 75 err error 76 } 77 78 func (r *mockResolver) Resolve(_ context.Context, address string) (*clusterConfig, error) { 79 if r.err != nil { 80 return nil, r.err 81 } 82 return r.configs[address], nil 83 }