github.com/matrixorigin/matrixone@v1.2.0/pkg/config/configuration_test.go (about)

     1  // Copyright 2023 Matrix Origin
     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 config
    16  
    17  import (
    18  	"os"
    19  	"testing"
    20  	"time"
    21  
    22  	"github.com/stretchr/testify/assert"
    23  	"github.com/stretchr/testify/require"
    24  )
    25  
    26  func TestGetUnixSocketAddress(t *testing.T) {
    27  	assert.NoError(t, os.RemoveAll("/tmp/TestGetUnixSocketAddress/"))
    28  	f := &FrontendParameters{UnixSocketAddress: "/tmp/TestGetUnixSocketAddress/1"}
    29  	assert.Equal(t, f.UnixSocketAddress, f.GetUnixSocketAddress())
    30  }
    31  
    32  func TestIsFileExist(t *testing.T) {
    33  	existFile := "/tmp/TestIsFileExist/exist"
    34  	notExistFile := "/tmp/TestIsFileExist/not_exist"
    35  	assert.NoError(t, os.MkdirAll("/tmp/TestIsFileExist", 0755))
    36  	defer func() {
    37  		assert.NoError(t, os.RemoveAll("/tmp/TestIsFileExist"))
    38  	}()
    39  
    40  	f, err := os.Create(existFile)
    41  	assert.NoError(t, err)
    42  	assert.NoError(t, f.Close())
    43  
    44  	ok, err := isFileExist(existFile)
    45  	assert.NoError(t, err)
    46  	assert.True(t, ok)
    47  
    48  	ok, err = isFileExist(notExistFile)
    49  	assert.NoError(t, err)
    50  	assert.False(t, ok)
    51  }
    52  
    53  func TestObservabilityParameters_SetDefaultValues1(t *testing.T) {
    54  
    55  	tests := []struct {
    56  		name    string
    57  		prepare func() *ObservabilityParameters
    58  		check   func(t *testing.T, cfg *ObservabilityParameters)
    59  	}{
    60  		{
    61  			name: "longQueryTime_0_enableAggr_200ms",
    62  			prepare: func() *ObservabilityParameters {
    63  				cfg := &ObservabilityParameters{
    64  					LongQueryTime:          0.0,
    65  					DisableStmtAggregation: false,
    66  				}
    67  				cfg.SelectAggrThreshold.UnmarshalText([]byte("200ms"))
    68  				return cfg
    69  			},
    70  			check: func(t *testing.T, cfg *ObservabilityParameters) {
    71  				cfg.SetDefaultValues("test")
    72  				require.Equal(t, false, cfg.DisableStmtAggregation)
    73  				require.Equal(t, 200*time.Millisecond, cfg.SelectAggrThreshold.Duration)
    74  				require.Equal(t, 0.2, cfg.LongQueryTime)
    75  			},
    76  		},
    77  		{
    78  			name: "longQueryTime_1.0_enableAggr_200ms",
    79  			prepare: func() *ObservabilityParameters {
    80  				cfg := &ObservabilityParameters{
    81  					LongQueryTime:          1.0,
    82  					DisableStmtAggregation: false,
    83  				}
    84  				cfg.SelectAggrThreshold.UnmarshalText([]byte("200ms"))
    85  				return cfg
    86  			},
    87  			check: func(t *testing.T, cfg *ObservabilityParameters) {
    88  				cfg.SetDefaultValues("test")
    89  				require.Equal(t, false, cfg.DisableStmtAggregation)
    90  				require.Equal(t, 200*time.Millisecond, cfg.SelectAggrThreshold.Duration)
    91  				require.Equal(t, 1.0, cfg.LongQueryTime)
    92  			},
    93  		},
    94  		{
    95  			name: "longQueryTime_0_disable_200ms",
    96  			prepare: func() *ObservabilityParameters {
    97  				cfg := &ObservabilityParameters{
    98  					LongQueryTime:          0.0,
    99  					DisableStmtAggregation: true,
   100  				}
   101  				cfg.SelectAggrThreshold.UnmarshalText([]byte("200ms"))
   102  				return cfg
   103  			},
   104  			check: func(t *testing.T, cfg *ObservabilityParameters) {
   105  				cfg.SetDefaultValues("test")
   106  				require.Equal(t, true, cfg.DisableStmtAggregation)
   107  				require.Equal(t, 200*time.Millisecond, cfg.SelectAggrThreshold.Duration)
   108  				require.Equal(t, 0.0, cfg.LongQueryTime)
   109  			},
   110  		},
   111  		{
   112  			name: "longQueryTime_1.0_disableAggr_200ms",
   113  			prepare: func() *ObservabilityParameters {
   114  				cfg := &ObservabilityParameters{
   115  					LongQueryTime:          1.0,
   116  					DisableStmtAggregation: true,
   117  				}
   118  				cfg.SelectAggrThreshold.UnmarshalText([]byte("200ms"))
   119  				return cfg
   120  			},
   121  			check: func(t *testing.T, cfg *ObservabilityParameters) {
   122  				cfg.SetDefaultValues("test")
   123  				require.Equal(t, true, cfg.DisableStmtAggregation)
   124  				require.Equal(t, 200*time.Millisecond, cfg.SelectAggrThreshold.Duration)
   125  				require.Equal(t, 1.0, cfg.LongQueryTime)
   126  			},
   127  		},
   128  	}
   129  	for _, tt := range tests {
   130  		t.Run(tt.name, func(t *testing.T) {
   131  			cfg := tt.prepare()
   132  			tt.check(t, cfg)
   133  		})
   134  	}
   135  }