github.com/matrixorigin/matrixone@v1.2.0/pkg/util/metric/config_test.go (about)

     1  // Copyright 2022 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 metric
    16  
    17  import (
    18  	"strconv"
    19  	"testing"
    20  
    21  	"github.com/stretchr/testify/assert"
    22  )
    23  
    24  func TestEnvOrDefaultBool(t *testing.T) {
    25  	key := "MO_TEST"
    26  	assert.Equal(t, EnvOrDefaultBool(key, 42), int32(42))
    27  	for _, s := range []string{"1", "True", "T", "true"} {
    28  		t.Setenv(key, s)
    29  		assert.Equal(t, EnvOrDefaultBool(key, 42), int32(1))
    30  	}
    31  	for _, s := range []string{"0", "f", "false"} {
    32  		t.Setenv(key, s)
    33  		assert.Equal(t, EnvOrDefaultBool(key, 42), int32(0))
    34  	}
    35  
    36  	for _, s := range []string{"", "nope", "stop"} {
    37  		t.Setenv(key, s)
    38  		assert.Equal(t, EnvOrDefaultBool(key, 42), int32(42))
    39  	}
    40  }
    41  
    42  func TestEnvOrDefaultInt(t *testing.T) {
    43  	key := "MO_TEST"
    44  	assert.Equal(t, EnvOrDefaultInt[int32](key, 42), int32(42))
    45  
    46  	for _, i := range []int{1, 2, 3, 5} {
    47  		t.Setenv(key, strconv.Itoa(i))
    48  		assert.Equal(t, EnvOrDefaultInt[int32](key, 42), int32(i))
    49  	}
    50  	for _, s := range []string{"x", "02f1", "ffs", "0x12"} {
    51  		t.Setenv(key, s)
    52  		assert.Equal(t, EnvOrDefaultInt[int64](key, 42), int64(42))
    53  	}
    54  }