github.com/ydb-platform/ydb-go-sdk/v3@v3.89.2/balancers/config_test.go (about) 1 package balancers 2 3 import ( 4 "testing" 5 6 "github.com/stretchr/testify/require" 7 8 balancerConfig "github.com/ydb-platform/ydb-go-sdk/v3/internal/balancer/config" 9 "github.com/ydb-platform/ydb-go-sdk/v3/internal/endpoint" 10 ) 11 12 func TestFromConfig(t *testing.T) { 13 for _, tt := range []struct { 14 name string 15 config string 16 res balancerConfig.Config 17 fail bool 18 }{ 19 { 20 name: "empty", 21 config: ``, 22 res: balancerConfig.Config{}, 23 fail: true, 24 }, 25 { 26 name: "disable", 27 config: `disable`, 28 res: balancerConfig.Config{SingleConn: true}, 29 }, 30 { 31 name: "single", 32 config: `single`, 33 res: balancerConfig.Config{SingleConn: true}, 34 }, 35 { 36 name: "single/JSON", 37 config: `{ 38 "type": "single" 39 }`, 40 res: balancerConfig.Config{SingleConn: true}, 41 }, 42 { 43 name: "round_robin", 44 config: `round_robin`, 45 res: balancerConfig.Config{}, 46 }, 47 { 48 name: "round_robin/JSON", 49 config: `{ 50 "type": "round_robin" 51 }`, 52 res: balancerConfig.Config{}, 53 }, 54 { 55 name: "random_choice", 56 config: `random_choice`, 57 res: balancerConfig.Config{}, 58 }, 59 { 60 name: "random_choice/JSON", 61 config: `{ 62 "type": "random_choice" 63 }`, 64 res: balancerConfig.Config{}, 65 }, 66 { 67 name: "prefer_local_dc", 68 config: `{ 69 "type": "random_choice", 70 "prefer": "local_dc" 71 }`, 72 res: balancerConfig.Config{ 73 DetectNearestDC: true, 74 Filter: filterFunc(func(info balancerConfig.Info, e endpoint.Info) bool { 75 // some non nil func 76 return false 77 }), 78 }, 79 }, 80 { 81 name: "prefer_nearest_dc", 82 config: `{ 83 "type": "random_choice", 84 "prefer": "nearest_dc" 85 }`, 86 res: balancerConfig.Config{ 87 DetectNearestDC: true, 88 Filter: filterFunc(func(info balancerConfig.Info, e endpoint.Info) bool { 89 // some non nil func 90 return false 91 }), 92 }, 93 }, 94 { 95 name: "prefer_unknown_type", 96 config: `{ 97 "type": "unknown_type", 98 "prefer": "local_dc" 99 }`, 100 fail: true, 101 }, 102 { 103 name: "prefer_local_dc_with_fallback", 104 config: `{ 105 "type": "random_choice", 106 "prefer": "local_dc", 107 "fallback": true 108 }`, 109 res: balancerConfig.Config{ 110 AllowFallback: true, 111 DetectNearestDC: true, 112 Filter: filterFunc(func(info balancerConfig.Info, e endpoint.Info) bool { 113 // some non nil func 114 return false 115 }), 116 }, 117 }, 118 { 119 name: "prefer_nearest_dc_with_fallback", 120 config: `{ 121 "type": "random_choice", 122 "prefer": "nearest_dc", 123 "fallback": true 124 }`, 125 res: balancerConfig.Config{ 126 AllowFallback: true, 127 DetectNearestDC: true, 128 Filter: filterFunc(func(info balancerConfig.Info, e endpoint.Info) bool { 129 // some non nil func 130 return false 131 }), 132 }, 133 }, 134 { 135 name: "prefer_locations", 136 config: `{ 137 "type": "random_choice", 138 "prefer": "locations", 139 "locations": ["AAA", "BBB", "CCC"] 140 }`, 141 res: balancerConfig.Config{ 142 Filter: filterFunc(func(info balancerConfig.Info, e endpoint.Info) bool { 143 // some non nil func 144 return false 145 }), 146 }, 147 }, 148 { 149 name: "prefer_locations_with_fallback", 150 config: `{ 151 "type": "random_choice", 152 "prefer": "locations", 153 "locations": ["AAA", "BBB", "CCC"], 154 "fallback": true 155 }`, 156 res: balancerConfig.Config{ 157 AllowFallback: true, 158 Filter: filterFunc(func(info balancerConfig.Info, e endpoint.Info) bool { 159 // some non nil func 160 return false 161 }), 162 }, 163 }, 164 } { 165 t.Run(tt.name, func(t *testing.T) { 166 var ( 167 actErr error 168 fallback = &balancerConfig.Config{} 169 ) 170 b := FromConfig( 171 tt.config, 172 WithParseErrorFallbackBalancer(fallback), 173 WithParseErrorHandler(func(err error) { 174 actErr = err 175 }), 176 ) 177 if tt.fail && actErr == nil { 178 t.Fatalf("expected error, but it not hanled") 179 } 180 if !tt.fail && actErr != nil { 181 t.Fatalf("unexpected error: %v", actErr) 182 } 183 if tt.fail && b != fallback { 184 t.Fatalf("unexpected balancer: %v", b) 185 } 186 187 // function pointers can check equal to nil only 188 if tt.res.Filter != nil { 189 require.NotNil(t, b.Filter) 190 b.Filter = nil 191 tt.res.Filter = nil 192 } 193 194 require.Equal(t, tt.res, *b) 195 }) 196 } 197 }