github.com/matrixorigin/matrixone@v1.2.0/pkg/util/errutil/stack_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  	"reflect"
    20  	"testing"
    21  
    22  	"github.com/matrixorigin/matrixone/pkg/util/stack"
    23  	"github.com/stretchr/testify/require"
    24  )
    25  
    26  func TestGetStackTracer(t *testing.T) {
    27  	type args struct {
    28  		cause error
    29  	}
    30  	tests := []struct {
    31  		name           string
    32  		args           args
    33  		hasStackTracer bool
    34  		want           stack.StackTrace
    35  	}{
    36  		{
    37  			name: "normal",
    38  			args: args{
    39  				cause: stackErr,
    40  			},
    41  			hasStackTracer: true,
    42  			want:           stackErr.(*withStack).Stack.StackTrace(),
    43  		},
    44  		{
    45  			name: "withContext",
    46  			args: args{
    47  				cause: WithContext(context.Background(), stackErr),
    48  			},
    49  			hasStackTracer: true,
    50  			want:           stackErr.(*withStack).Stack.StackTrace(),
    51  		},
    52  	}
    53  	for _, tt := range tests {
    54  		t.Run(tt.name, func(t *testing.T) {
    55  			got := GetStackTracer(tt.args.cause)
    56  			if !tt.hasStackTracer {
    57  				require.Empty(t, got, "GetStackTracer not empty")
    58  				return
    59  			}
    60  			if !reflect.DeepEqual(got.StackTrace(), tt.want) {
    61  				t.Errorf("GetStackTracer() = %v, want %v", got.StackTrace(), tt.want)
    62  			}
    63  		})
    64  	}
    65  }
    66  
    67  func TestHasStack(t *testing.T) {
    68  	type args struct {
    69  		err error
    70  	}
    71  	tests := []struct {
    72  		name string
    73  		args args
    74  		want bool
    75  	}{
    76  		{
    77  			name: "normal",
    78  			args: args{
    79  				err: stackErr,
    80  			},
    81  			want: true,
    82  		},
    83  		{
    84  			name: "withContext",
    85  			args: args{
    86  				err: WithContext(context.Background(), stackErr),
    87  			},
    88  			want: true,
    89  		},
    90  		{
    91  			name: "raw error",
    92  			args: args{
    93  				err: testErr,
    94  			},
    95  			want: false,
    96  		},
    97  	}
    98  	for _, tt := range tests {
    99  		t.Run(tt.name, func(t *testing.T) {
   100  			if got := HasStack(tt.args.err); got != tt.want {
   101  				t.Errorf("HasStack() = %v, want %v", got, tt.want)
   102  			}
   103  		})
   104  	}
   105  }
   106  
   107  func Test_withStack_Cause(t *testing.T) {
   108  	type fields struct {
   109  		cause error
   110  		Stack *stack.Stack
   111  	}
   112  	tests := []struct {
   113  		name    string
   114  		fields  fields
   115  		wantErr bool
   116  	}{
   117  		{
   118  			name: "normal",
   119  			fields: fields{
   120  				cause: stackErr,
   121  				Stack: stackErr.(*withStack).Stack,
   122  			},
   123  			wantErr: true,
   124  		},
   125  		{
   126  			name: "withContext",
   127  			fields: fields{
   128  				cause: testErr,
   129  				Stack: stackErr.(*withStack).Stack,
   130  			},
   131  			wantErr: true,
   132  		},
   133  		{
   134  			name: "empty",
   135  			fields: fields{
   136  				cause: nil,
   137  				Stack: nil,
   138  			},
   139  			wantErr: false,
   140  		},
   141  	}
   142  	for _, tt := range tests {
   143  		t.Run(tt.name, func(t *testing.T) {
   144  			w := &withStack{
   145  				cause: tt.fields.cause,
   146  				Stack: tt.fields.Stack,
   147  			}
   148  			if err := w.Cause(); (err != nil) != tt.wantErr {
   149  				t.Errorf("Cause() error = %v, wantErr %v", err, tt.wantErr)
   150  			}
   151  		})
   152  	}
   153  }
   154  
   155  func Test_WithStack_HasStack(t *testing.T) {
   156  	type fields struct {
   157  		cause error
   158  		Stack *stack.Stack
   159  	}
   160  	tests := []struct {
   161  		name   string
   162  		fields fields
   163  		want   bool
   164  	}{
   165  		{
   166  			name: "normal",
   167  			fields: fields{
   168  				cause: stackErr,
   169  				Stack: stackErr.(*withStack).Stack,
   170  			},
   171  			want: true,
   172  		},
   173  		{
   174  			name: "withContext",
   175  			fields: fields{
   176  				cause: WithContext(context.Background(), stackErr),
   177  				Stack: stackErr.(*withStack).Stack,
   178  			},
   179  			want: true,
   180  		},
   181  	}
   182  	for _, tt := range tests {
   183  		t.Run(tt.name, func(t *testing.T) {
   184  			w := &withStack{
   185  				cause: tt.fields.cause,
   186  				Stack: tt.fields.Stack,
   187  			}
   188  			if got := w.HasStack(); got != tt.want {
   189  				t.Errorf("HasStack() = %v, want %v", got, tt.want)
   190  			}
   191  		})
   192  	}
   193  }