|
| 1 | +package balancer |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/stretchr/testify/assert" |
| 8 | + |
| 9 | + balancerConfig "github.com/ydb-platform/ydb-go-sdk/v3/internal/balancer/config" |
| 10 | + "github.com/ydb-platform/ydb-go-sdk/v3/internal/conn" |
| 11 | + "github.com/ydb-platform/ydb-go-sdk/v3/internal/endpoint" |
| 12 | + "github.com/ydb-platform/ydb-go-sdk/v3/internal/mock" |
| 13 | +) |
| 14 | + |
| 15 | +type fakePool struct { |
| 16 | + connections map[string]*mock.Conn |
| 17 | +} |
| 18 | + |
| 19 | +func (fp *fakePool) EndpointsToConnections(eps []endpoint.Endpoint) []conn.Conn { |
| 20 | + var conns []conn.Conn |
| 21 | + for _, ep := range eps { |
| 22 | + if c, ok := fp.connections[ep.Address()]; ok { |
| 23 | + conns = append(conns, c) |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + return conns |
| 28 | +} |
| 29 | + |
| 30 | +func (fp *fakePool) Allow(_ context.Context, _ conn.Conn) {} |
| 31 | + |
| 32 | +func (fp *fakePool) GetIfPresent(ep endpoint.Endpoint) conn.Conn { |
| 33 | + if c, ok := fp.connections[ep.Address()]; ok { |
| 34 | + return c |
| 35 | + } |
| 36 | + |
| 37 | + return nil |
| 38 | +} |
| 39 | + |
| 40 | +func TestBuildConnectionsState(t *testing.T) { |
| 41 | + ctx := context.Background() |
| 42 | + |
| 43 | + tests := []struct { |
| 44 | + name string |
| 45 | + newEndpoints []endpoint.Endpoint |
| 46 | + oldEndpoints []endpoint.Endpoint |
| 47 | + initialConns map[string]*mock.Conn |
| 48 | + conf balancerConfig.Config |
| 49 | + selfLoc balancerConfig.Info |
| 50 | + expectPinged []string |
| 51 | + expectClosed []string |
| 52 | + }{ |
| 53 | + { |
| 54 | + name: "single new and old endpoint", |
| 55 | + newEndpoints: []endpoint.Endpoint{&mock.Endpoint{AddrField: "127.0.0.1"}}, |
| 56 | + oldEndpoints: []endpoint.Endpoint{&mock.Endpoint{AddrField: "127.0.0.2"}}, |
| 57 | + initialConns: map[string]*mock.Conn{ |
| 58 | + "127.0.0.1": { |
| 59 | + AddrField: "127.0.0.1", |
| 60 | + State: conn.Online, |
| 61 | + }, |
| 62 | + "127.0.0.2": { |
| 63 | + AddrField: "127.0.0.2", |
| 64 | + State: conn.Offline, |
| 65 | + }, |
| 66 | + }, |
| 67 | + conf: balancerConfig.Config{ |
| 68 | + AllowFallback: true, |
| 69 | + DetectNearestDC: true, |
| 70 | + }, |
| 71 | + selfLoc: balancerConfig.Info{SelfLocation: "local"}, |
| 72 | + expectPinged: []string{"127.0.0.1"}, |
| 73 | + expectClosed: []string{"127.0.0.2"}, |
| 74 | + }, |
| 75 | + { |
| 76 | + newEndpoints: []endpoint.Endpoint{&mock.Endpoint{AddrField: "a1"}, &mock.Endpoint{AddrField: "a2"}}, |
| 77 | + oldEndpoints: []endpoint.Endpoint{&mock.Endpoint{AddrField: "a3"}}, |
| 78 | + initialConns: map[string]*mock.Conn{ |
| 79 | + "a1": { |
| 80 | + AddrField: "a1", |
| 81 | + LocationField: "local", |
| 82 | + State: conn.Offline, |
| 83 | + }, |
| 84 | + "a2": { |
| 85 | + AddrField: "a2", |
| 86 | + State: conn.Offline, |
| 87 | + }, |
| 88 | + "a3": { |
| 89 | + AddrField: "a3", |
| 90 | + State: conn.Online, |
| 91 | + }, |
| 92 | + }, |
| 93 | + conf: balancerConfig.Config{ |
| 94 | + AllowFallback: true, |
| 95 | + DetectNearestDC: true, |
| 96 | + }, |
| 97 | + selfLoc: balancerConfig.Info{SelfLocation: "local"}, |
| 98 | + expectPinged: []string{"a1", "a2"}, |
| 99 | + expectClosed: []string{"a3"}, |
| 100 | + }, |
| 101 | + } |
| 102 | + |
| 103 | + for _, tt := range tests { |
| 104 | + t.Run(tt.name, func(t *testing.T) { |
| 105 | + fp := &fakePool{connections: make(map[string]*mock.Conn)} |
| 106 | + for addr, c := range tt.initialConns { |
| 107 | + fp.connections[addr] = c |
| 108 | + } |
| 109 | + |
| 110 | + state := buildConnectionsState(ctx, fp, tt.newEndpoints, tt.oldEndpoints, tt.conf, tt.selfLoc) |
| 111 | + assert.NotNil(t, state) |
| 112 | + for _, addr := range tt.expectPinged { |
| 113 | + c := fp.connections[addr] |
| 114 | + assert.True(t, c.Pinged.Load(), "connection %s should be pinged", addr) |
| 115 | + assert.True(t, c.State == conn.Online || c.PingErr != nil) |
| 116 | + } |
| 117 | + for _, addr := range tt.expectClosed { |
| 118 | + c := fp.connections[addr] |
| 119 | + assert.True(t, c.Closed.Load(), "connection %s should be closed", addr) |
| 120 | + assert.True(t, c.State == conn.Offline, "connection %s should be offline", addr) |
| 121 | + } |
| 122 | + }) |
| 123 | + } |
| 124 | +} |
0 commit comments