github.com/kubewharf/katalyst-core@v0.5.3/pkg/util/general/log_test.go (about)

     1  /*
     2  Copyright 2022 The Katalyst Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package general
    18  
    19  import (
    20  	"fmt"
    21  	"testing"
    22  	"time"
    23  
    24  	"github.com/stretchr/testify/require"
    25  )
    26  
    27  type testLogging struct{}
    28  
    29  func (_ testLogging) log(message string, params ...interface{}) string {
    30  	return logging(message, params...)
    31  }
    32  
    33  func (_ testLogging) logPath(pkg LoggingPKG, message string, params ...interface{}) string {
    34  	return loggingPath(pkg, message, params...)
    35  }
    36  
    37  type testLogger struct {
    38  	klog Logger // klog for katalyst-log
    39  }
    40  
    41  func (t testLogger) log(message string, params ...interface{}) string {
    42  	return t.klog.logging(message, params...)
    43  }
    44  
    45  func TestLogging(t *testing.T) {
    46  	t.Parallel()
    47  
    48  	loggingWithoutStruct := logging("extra %v %v", 1, "test")
    49  	require.Equal(t, "[testing.tRunner] extra 1 test", loggingWithoutStruct)
    50  
    51  	loggingWithStruct := testLogging{}.log("extra %v %v", 1, "test")
    52  	require.Equal(t, "[katalyst-core/pkg/util/general.TestLogging] extra 1 test", loggingWithStruct)
    53  
    54  	loggingPathWithStruct := testLogging{}.logPath(LoggingPKGShort, "extra %v %v", 1, "test")
    55  	require.Equal(t, "[general.TestLogging] extra 1 test", loggingPathWithStruct)
    56  
    57  	loggerWithoutStruct := LoggerWithPrefix("p-test", LoggingPKGNone).logging("extra %v %v", 1, "test")
    58  	require.Equal(t, "[p-test: tRunner] extra 1 test", loggerWithoutStruct)
    59  
    60  	loggerWithoutStruct = LoggerWithPrefix("p-test", LoggingPKGShort).logging("extra %v %v", 1, "test")
    61  	require.Equal(t, "[p-test: testing.tRunner] extra 1 test", loggerWithoutStruct)
    62  
    63  	loggerWithoutStruct = LoggerWithPrefix("p-test", LoggingPKGFull).logging("extra %v %v", 1, "test")
    64  	require.Equal(t, "[p-test: testing.tRunner] extra 1 test", loggerWithoutStruct)
    65  
    66  	loggerWithStruct := testLogger{klog: LoggerWithPrefix("p-test", LoggingPKGNone)}.log("extra %v %v", 1, "test")
    67  	require.Equal(t, "[p-test: TestLogging] extra 1 test", loggerWithStruct)
    68  
    69  	loggerWithStruct = testLogger{klog: LoggerWithPrefix("p-test", LoggingPKGShort)}.log("extra %v %v", 1, "test")
    70  	require.Equal(t, "[p-test: general.TestLogging] extra 1 test", loggerWithStruct)
    71  
    72  	loggerWithStruct = testLogger{klog: LoggerWithPrefix("p-test", LoggingPKGFull)}.log("extra %v %v", 1, "test")
    73  	require.Equal(t, "[p-test: katalyst-core/pkg/util/general.TestLogging] extra 1 test", loggerWithStruct)
    74  
    75  	InfoS("test-InfoS", "param-key", "param-InfoS")
    76  	Infof("test-Infof %v", "extra-Infof")
    77  	InfofV(1, "test-InfofV %v", "extra-InfofV")
    78  	Warningf("test-Warningf %v", "extra-Warningf")
    79  	Errorf("test-Errorf %v", "extra-Errorf")
    80  	ErrorS(fmt.Errorf("err"), "test-ErrorS", "param-key", "param-ErrorS")
    81  
    82  	InfoSPath(LoggingPKGShort, "test-InfoS", "param-key", "param-InfoS")
    83  	InfofPath(LoggingPKGShort, "test-Infof %v", "extra-Infof")
    84  	InfofVPath(LoggingPKGShort, 1, "test-InfofV %v", "extra-InfofV")
    85  	WarningfPath(LoggingPKGShort, "test-Warningf %v", "extra-Warningf")
    86  	ErrorfPath(LoggingPKGShort, "test-Errorf %v", "extra-Errorf")
    87  	ErrorSPath(LoggingPKGShort, fmt.Errorf("err"), "test-ErrorS", "param-key", "param-ErrorS")
    88  
    89  	go func() {
    90  		goStr := logging("extra %v %v", 1, "test")
    91  		require.Equal(t, "[runtime.goexit] extra 1 test", goStr)
    92  	}()
    93  
    94  	f := func() {
    95  		funcStr := logging("extra %v %v", 1, "test")
    96  		require.Equal(t, "[runtime.goexit] extra 1 test", funcStr)
    97  	}
    98  	go f()
    99  
   100  	time.Sleep(time.Millisecond)
   101  }