github.com/matrixorigin/matrixone@v1.2.0/pkg/util/errutil/errors_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 errutil
    16  
    17  import (
    18  	"context"
    19  	"github.com/stretchr/testify/require"
    20  	"reflect"
    21  	"sync/atomic"
    22  	"testing"
    23  )
    24  
    25  func mockReportError(_ context.Context, err error, depth int) {}
    26  
    27  func TestGetReportErrorFunc(t *testing.T) {
    28  	tests := []struct {
    29  		name string
    30  		f    reportErrorFunc
    31  		want uintptr
    32  	}{
    33  		{
    34  			name: "noop",
    35  			want: reflect.ValueOf(noopReportError).Pointer(),
    36  		},
    37  		{
    38  			name: "mark",
    39  			f:    mockReportError,
    40  			want: reflect.ValueOf(mockReportError).Pointer(),
    41  		},
    42  	}
    43  	for _, tt := range tests {
    44  		t.Run(tt.name, func(t *testing.T) {
    45  			if tt.f != nil {
    46  				SetErrorReporter(tt.f)
    47  			}
    48  			if got := GetReportErrorFunc(); reflect.ValueOf(got).Pointer() != tt.want {
    49  				t.Errorf("GetReportErrorFunc() = %v, want %v", got, tt.want)
    50  			}
    51  		})
    52  	}
    53  }
    54  
    55  func TestReportError(t *testing.T) {
    56  	type args struct {
    57  		ctx context.Context
    58  		err error
    59  	}
    60  	tests := []struct {
    61  		name string
    62  		args args
    63  	}{
    64  		{
    65  			name: "normal",
    66  			args: args{context.Background(), testErr},
    67  		},
    68  		{
    69  			name: "noReport",
    70  			args: args{ContextWithNoReport(context.Background(), true), testErr},
    71  		},
    72  	}
    73  
    74  	var counter atomic.Int32
    75  	var doReportFunc = func(context.Context, error, int) {
    76  		counter.Add(1)
    77  	}
    78  	SetErrorReporter(doReportFunc)
    79  
    80  	for _, tt := range tests {
    81  		t.Run(tt.name, func(t *testing.T) {
    82  			ReportError(tt.args.ctx, tt.args.err)
    83  		})
    84  	}
    85  	require.Equal(t, counter.Load(), int32(len(tests)-1))
    86  }
    87  
    88  func TestWalkDeep(t *testing.T) {
    89  	type args struct {
    90  		err     error
    91  		visitor func(err error) bool
    92  	}
    93  	var depth int32 = 0
    94  	var visitor = func(error) bool {
    95  		atomic.AddInt32(&depth, 1)
    96  		return false
    97  	}
    98  	tests := []struct {
    99  		name      string
   100  		args      args
   101  		wantDepth int32
   102  	}{
   103  		{
   104  			name: "depth_2",
   105  			args: args{
   106  				err:     stackErr,
   107  				visitor: visitor,
   108  			},
   109  			wantDepth: 2,
   110  		},
   111  	}
   112  	for _, tt := range tests {
   113  		t.Run(tt.name, func(t *testing.T) {
   114  			atomic.StoreInt32(&depth, 0)
   115  			_ = WalkDeep(tt.args.err, tt.args.visitor)
   116  			if depth != tt.wantDepth {
   117  				t.Errorf("WalkDeep() depth = %v, want %v", depth, tt.wantDepth)
   118  			}
   119  		})
   120  	}
   121  }
   122  
   123  func TestWrap(t *testing.T) {
   124  	type args struct {
   125  		err     error
   126  		message string
   127  	}
   128  	tests := []struct {
   129  		name    string
   130  		args    args
   131  		wantErr bool
   132  	}{
   133  		{
   134  			name:    "normal",
   135  			args:    args{testErr, "normal message"},
   136  			wantErr: true,
   137  		},
   138  		{
   139  			name:    "nil",
   140  			args:    args{},
   141  			wantErr: false,
   142  		},
   143  	}
   144  	for _, tt := range tests {
   145  		t.Run(tt.name, func(t *testing.T) {
   146  			if err := Wrap(tt.args.err, tt.args.message); (err != nil) != tt.wantErr {
   147  				t.Errorf("Wrap() error = %v, wantErr %v", err, tt.wantErr)
   148  			}
   149  		})
   150  	}
   151  }
   152  
   153  func TestWrapf(t *testing.T) {
   154  	type args struct {
   155  		err    error
   156  		format string
   157  		args   []interface{}
   158  	}
   159  	tests := []struct {
   160  		name    string
   161  		args    args
   162  		wantErr bool
   163  	}{
   164  		{
   165  			name:    "normal",
   166  			args:    args{testErr, "normal message", []any{}},
   167  			wantErr: true,
   168  		},
   169  		{
   170  			name:    "nil",
   171  			args:    args{},
   172  			wantErr: false,
   173  		},
   174  	}
   175  	for _, tt := range tests {
   176  		t.Run(tt.name, func(t *testing.T) {
   177  			if err := Wrapf(tt.args.err, tt.args.format, tt.args.args...); (err != nil) != tt.wantErr {
   178  				t.Errorf("Wrapf() error = %v, wantErr %v", err, tt.wantErr)
   179  			}
   180  		})
   181  	}
   182  }
   183  
   184  func Test_noopReportError(t *testing.T) {
   185  	type args struct {
   186  		in0 context.Context
   187  		in1 error
   188  		in2 int
   189  	}
   190  	tests := []struct {
   191  		name string
   192  		args args
   193  	}{
   194  		{
   195  			name: "normal",
   196  			args: args{context.Background(), testErr, 1},
   197  		},
   198  	}
   199  	for _, tt := range tests {
   200  		t.Run(tt.name, func(t *testing.T) {
   201  			noopReportError(tt.args.in0, tt.args.in1, tt.args.in2)
   202  		})
   203  	}
   204  }