github.com/ydb-platform/ydb-go-sdk/v3@v3.57.0/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/conn"
    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  				DetectLocalDC: true,
    74  				Filter: filterFunc(func(info balancerConfig.Info, c conn.Conn) bool {
    75  					// some non nil func
    76  					return false
    77  				}),
    78  			},
    79  		},
    80  		{
    81  			name: "prefer_unknown_type",
    82  			config: `{
    83  				"type": "unknown_type",
    84  				"prefer": "local_dc"
    85  			}`,
    86  			fail: true,
    87  		},
    88  		{
    89  			name: "prefer_local_dc_with_fallback",
    90  			config: `{
    91  				"type": "random_choice",
    92  				"prefer": "local_dc",
    93  				"fallback": true
    94  			}`,
    95  			res: balancerConfig.Config{
    96  				AllowFallback: true,
    97  				DetectLocalDC: true,
    98  				Filter: filterFunc(func(info balancerConfig.Info, c conn.Conn) bool {
    99  					// some non nil func
   100  					return false
   101  				}),
   102  			},
   103  		},
   104  		{
   105  			name: "prefer_locations",
   106  			config: `{
   107  				"type": "random_choice",
   108  				"prefer": "locations",
   109  				"locations": ["AAA", "BBB", "CCC"]
   110  			}`,
   111  			res: balancerConfig.Config{
   112  				Filter: filterFunc(func(info balancerConfig.Info, c conn.Conn) bool {
   113  					// some non nil func
   114  					return false
   115  				}),
   116  			},
   117  		},
   118  		{
   119  			name: "prefer_locations_with_fallback",
   120  			config: `{
   121  				"type": "random_choice",
   122  				"prefer": "locations",
   123  				"locations": ["AAA", "BBB", "CCC"],
   124  				"fallback": true
   125  			}`,
   126  			res: balancerConfig.Config{
   127  				AllowFallback: true,
   128  				Filter: filterFunc(func(info balancerConfig.Info, c conn.Conn) bool {
   129  					// some non nil func
   130  					return false
   131  				}),
   132  			},
   133  		},
   134  	} {
   135  		t.Run(tt.name, func(t *testing.T) {
   136  			var (
   137  				actErr   error
   138  				fallback = &balancerConfig.Config{}
   139  			)
   140  			b := FromConfig(
   141  				tt.config,
   142  				WithParseErrorFallbackBalancer(fallback),
   143  				WithParseErrorHandler(func(err error) {
   144  					actErr = err
   145  				}),
   146  			)
   147  			if tt.fail && actErr == nil {
   148  				t.Fatalf("expected error, but it not hanled")
   149  			}
   150  			if !tt.fail && actErr != nil {
   151  				t.Fatalf("unexpected error: %v", actErr)
   152  			}
   153  			if tt.fail && b != fallback {
   154  				t.Fatalf("unexpected balancer: %v", b)
   155  			}
   156  
   157  			// function pointers can check equal to nil only
   158  			if tt.res.Filter != nil {
   159  				require.NotNil(t, b.Filter)
   160  				b.Filter = nil
   161  				tt.res.Filter = nil
   162  			}
   163  
   164  			require.Equal(t, tt.res, *b)
   165  		})
   166  	}
   167  }