github.com/matrixorigin/matrixone@v1.2.0/pkg/logutil/dragonboat_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 logutil
    16  
    17  import (
    18  	"fmt"
    19  	"testing"
    20  
    21  	"github.com/lni/dragonboat/v4/logger"
    22  	"github.com/stretchr/testify/require"
    23  )
    24  
    25  func TestDragonboatFactory(t *testing.T) {
    26  	type args struct {
    27  		name string
    28  
    29  		formatter string
    30  		args      []any
    31  	}
    32  	tests := []struct {
    33  		name string
    34  		args args
    35  	}{
    36  		{
    37  			name: "normal",
    38  			args: args{
    39  				name:      "Test",
    40  				formatter: "hello %s",
    41  				args:      []any{"world"},
    42  			},
    43  		},
    44  	}
    45  	for _, tt := range tests {
    46  		t.Run(tt.name, func(t *testing.T) {
    47  
    48  			plog := logger.GetLogger(tt.args.name)
    49  			plog.SetLevel(logger.DEBUG)
    50  			plog.Debugf(tt.args.formatter, tt.args.args...)
    51  			plog.Infof(tt.args.formatter, tt.args.args...)
    52  			plog.Warningf(tt.args.formatter, tt.args.args...)
    53  			plog.Errorf(tt.args.formatter, tt.args.args...)
    54  			plog.SetLevel(logger.INFO)
    55  			plog.SetLevel(logger.WARNING)
    56  			plog.SetLevel(logger.ERROR)
    57  			plog.Errorf("ERROR level has stack: "+tt.args.formatter, tt.args.args...)
    58  			plog.SetLevel(logger.CRITICAL)
    59  			plog.Errorf("CRITICAL level no stack: "+tt.args.formatter, tt.args.args...)
    60  
    61  		})
    62  	}
    63  }
    64  
    65  func TestDragonboat_panic(t *testing.T) {
    66  	type args struct {
    67  		name string
    68  
    69  		formatter string
    70  		args      []any
    71  	}
    72  	tests := []struct {
    73  		name string
    74  		args args
    75  	}{
    76  		{
    77  			name: "normal",
    78  			args: args{
    79  				name:      "Test",
    80  				formatter: "hello %s",
    81  				args:      []any{"world"},
    82  			},
    83  		},
    84  	}
    85  	for _, tt := range tests {
    86  		t.Run(tt.name, func(t *testing.T) {
    87  			defer func() {
    88  				err := recover()
    89  				require.Equal(t, fmt.Sprintf(tt.args.formatter, tt.args.args...), fmt.Sprintf("%s", err))
    90  			}()
    91  
    92  			plog := logger.GetLogger(tt.args.name)
    93  			plog.SetLevel(logger.DEBUG)
    94  			plog.Panicf(tt.args.formatter, tt.args.args...)
    95  
    96  		})
    97  	}
    98  }