github.com/dolthub/go-mysql-server@v0.18.0/server/server_config_test.go (about)

     1  // Copyright 2021 Dolthub, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package server
    16  
    17  import (
    18  	"fmt"
    19  	"reflect"
    20  	"testing"
    21  
    22  	"github.com/stretchr/testify/assert"
    23  
    24  	"github.com/dolthub/go-mysql-server/sql"
    25  	"github.com/dolthub/go-mysql-server/sql/types"
    26  	"github.com/dolthub/go-mysql-server/sql/variables"
    27  )
    28  
    29  func TestConfigWithDefaults(t *testing.T) {
    30  	tests := []struct {
    31  		Name        string
    32  		Scope       sql.SystemVariableScope
    33  		Type        sql.SystemVariableType
    34  		ConfigField string
    35  		Default     interface{}
    36  		ExpectedCmp interface{}
    37  	}{
    38  		{
    39  			Name:        "max_connections",
    40  			Scope:       sql.SystemVariableScope_Global,
    41  			Type:        types.NewSystemIntType("max_connections", 1, 100000, false),
    42  			ConfigField: "MaxConnections",
    43  			Default:     int64(1000),
    44  			ExpectedCmp: uint64(1000),
    45  		},
    46  		{
    47  			Name:        "net_write_timeout",
    48  			Scope:       sql.SystemVariableScope_Both,
    49  			Type:        types.NewSystemIntType("net_write_timeout", 1, 9223372036854775807, false),
    50  			ConfigField: "ConnWriteTimeout",
    51  			Default:     int64(76),
    52  			ExpectedCmp: int64(76000000),
    53  		}, {
    54  			Name:        "net_read_timeout",
    55  			Scope:       sql.SystemVariableScope_Both,
    56  			Type:        types.NewSystemIntType("net_read_timeout", 1, 9223372036854775807, false),
    57  			ConfigField: "ConnReadTimeout",
    58  			Default:     int64(67),
    59  			ExpectedCmp: int64(67000000),
    60  		},
    61  	}
    62  
    63  	for _, test := range tests {
    64  		t.Run(fmt.Sprintf("server config var: %s", test.Name), func(t *testing.T) {
    65  			variables.InitSystemVariables()
    66  			sql.SystemVariables.AddSystemVariables([]sql.SystemVariable{{
    67  				Name:    test.Name,
    68  				Scope:   test.Scope,
    69  				Dynamic: true,
    70  				Type:    test.Type,
    71  				Default: test.Default,
    72  			}})
    73  			serverConf := Config{}
    74  			serverConf, err := serverConf.NewConfig()
    75  			assert.NoError(t, err)
    76  
    77  			r := reflect.ValueOf(serverConf)
    78  			f := reflect.Indirect(r).FieldByName(test.ConfigField)
    79  			var res interface{}
    80  			switch f.Kind() {
    81  			case reflect.Int, reflect.Int64, reflect.Int32, reflect.Int16, reflect.Int8:
    82  				res = f.Int()
    83  			case reflect.Uint, reflect.Uint64, reflect.Uint32, reflect.Uint16, reflect.Uint8:
    84  				res = f.Uint()
    85  			default:
    86  			}
    87  			assert.Equal(t, test.ExpectedCmp, res)
    88  		})
    89  	}
    90  }