gitee.com/ks-custle/core-gm@v0.0.0-20230922171213-b83bdd97b62c/grpc/internal/binarylog/binarylog_test.go (about)

     1  /*
     2   *
     3   * Copyright 2018 gRPC authors.
     4   *
     5   * Licensed under the Apache License, Version 2.0 (the "License");
     6   * you may not use this file except in compliance with the License.
     7   * You may obtain a copy of the License at
     8   *
     9   *     http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   *
    17   */
    18  
    19  package binarylog
    20  
    21  import (
    22  	"testing"
    23  
    24  	"gitee.com/ks-custle/core-gm/grpc/internal/grpctest"
    25  )
    26  
    27  type s struct {
    28  	grpctest.Tester
    29  }
    30  
    31  func Test(t *testing.T) {
    32  	grpctest.RunSubTests(t, s{})
    33  }
    34  
    35  // Test that get method logger returns the one with the most exact match.
    36  func (s) TestGetMethodLogger(t *testing.T) {
    37  	testCases := []struct {
    38  		in       string
    39  		method   string
    40  		hdr, msg uint64
    41  	}{
    42  		// Global.
    43  		{
    44  			in:     "*{h:12;m:23}",
    45  			method: "/s/m",
    46  			hdr:    12, msg: 23,
    47  		},
    48  		// service/*.
    49  		{
    50  			in:     "*,s/*{h:12;m:23}",
    51  			method: "/s/m",
    52  			hdr:    12, msg: 23,
    53  		},
    54  		// Service/method.
    55  		{
    56  			in:     "*{h;m},s/m{h:12;m:23}",
    57  			method: "/s/m",
    58  			hdr:    12, msg: 23,
    59  		},
    60  		{
    61  			in:     "*{h;m},s/*{h:314;m},s/m{h:12;m:23}",
    62  			method: "/s/m",
    63  			hdr:    12, msg: 23,
    64  		},
    65  		{
    66  			in:     "*{h;m},s/*{h:12;m:23},s/m",
    67  			method: "/s/m",
    68  			hdr:    maxUInt, msg: maxUInt,
    69  		},
    70  
    71  		// service/*.
    72  		{
    73  			in:     "*{h;m},s/*{h:12;m:23},s/m1",
    74  			method: "/s/m",
    75  			hdr:    12, msg: 23,
    76  		},
    77  		{
    78  			in:     "*{h;m},s1/*,s/m{h:12;m:23}",
    79  			method: "/s/m",
    80  			hdr:    12, msg: 23,
    81  		},
    82  
    83  		// With black list.
    84  		{
    85  			in:     "*{h:12;m:23},-s/m1",
    86  			method: "/s/m",
    87  			hdr:    12, msg: 23,
    88  		},
    89  	}
    90  	for _, tc := range testCases {
    91  		l := NewLoggerFromConfigString(tc.in)
    92  		if l == nil {
    93  			t.Errorf("in: %q, failed to create logger from config string", tc.in)
    94  			continue
    95  		}
    96  		ml := l.getMethodLogger(tc.method)
    97  		if ml == nil {
    98  			t.Errorf("in: %q, method logger is nil, want non-nil", tc.in)
    99  			continue
   100  		}
   101  
   102  		if ml.headerMaxLen != tc.hdr || ml.messageMaxLen != tc.msg {
   103  			t.Errorf("in: %q, want header: %v, message: %v, got header: %v, message: %v", tc.in, tc.hdr, tc.msg, ml.headerMaxLen, ml.messageMaxLen)
   104  		}
   105  	}
   106  }
   107  
   108  // expect method logger to be nil
   109  func (s) TestGetMethodLoggerOff(t *testing.T) {
   110  	testCases := []struct {
   111  		in     string
   112  		method string
   113  	}{
   114  		// method not specified.
   115  		{
   116  			in:     "s1/m",
   117  			method: "/s/m",
   118  		},
   119  		{
   120  			in:     "s/m1",
   121  			method: "/s/m",
   122  		},
   123  		{
   124  			in:     "s1/*",
   125  			method: "/s/m",
   126  		},
   127  		{
   128  			in:     "s1/*,s/m1",
   129  			method: "/s/m",
   130  		},
   131  
   132  		// blacklisted.
   133  		{
   134  			in:     "*,-s/m",
   135  			method: "/s/m",
   136  		},
   137  		{
   138  			in:     "s/*,-s/m",
   139  			method: "/s/m",
   140  		},
   141  		{
   142  			in:     "-s/m,s/*",
   143  			method: "/s/m",
   144  		},
   145  	}
   146  	for _, tc := range testCases {
   147  		l := NewLoggerFromConfigString(tc.in)
   148  		if l == nil {
   149  			t.Errorf("in: %q, failed to create logger from config string", tc.in)
   150  			continue
   151  		}
   152  		ml := l.getMethodLogger(tc.method)
   153  		if ml != nil {
   154  			t.Errorf("in: %q, method logger is non-nil, want nil", tc.in)
   155  		}
   156  	}
   157  }