...

Source file src/google.golang.org/grpc/internal/binarylog/binarylog_test.go

Documentation: google.golang.org/grpc/internal/binarylog

     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  	"google.golang.org/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).(*TruncatingMethodLogger)
    97  		if ml == nil {
    98  			t.Errorf("in: %q, method logger is nil, want non-nil", tc.in)
    99  			continue
   100  		}
   101  		if ml.headerMaxLen != tc.hdr || ml.messageMaxLen != tc.msg {
   102  			t.Errorf("in: %q, want header: %v, message: %v, got header: %v, message: %v", tc.in, tc.hdr, tc.msg, ml.headerMaxLen, ml.messageMaxLen)
   103  		}
   104  	}
   105  }
   106  
   107  // expect method logger to be nil
   108  func (s) TestGetMethodLoggerOff(t *testing.T) {
   109  	testCases := []struct {
   110  		in     string
   111  		method string
   112  	}{
   113  		// method not specified.
   114  		{
   115  			in:     "s1/m",
   116  			method: "/s/m",
   117  		},
   118  		{
   119  			in:     "s/m1",
   120  			method: "/s/m",
   121  		},
   122  		{
   123  			in:     "s1/*",
   124  			method: "/s/m",
   125  		},
   126  		{
   127  			in:     "s1/*,s/m1",
   128  			method: "/s/m",
   129  		},
   130  
   131  		// blacklisted.
   132  		{
   133  			in:     "*,-s/m",
   134  			method: "/s/m",
   135  		},
   136  		{
   137  			in:     "s/*,-s/m",
   138  			method: "/s/m",
   139  		},
   140  		{
   141  			in:     "-s/m,s/*",
   142  			method: "/s/m",
   143  		},
   144  	}
   145  	for _, tc := range testCases {
   146  		l := NewLoggerFromConfigString(tc.in)
   147  		if l == nil {
   148  			t.Errorf("in: %q, failed to create logger from config string", tc.in)
   149  			continue
   150  		}
   151  		ml := l.GetMethodLogger(tc.method)
   152  		if ml != nil {
   153  			t.Errorf("in: %q, method logger is non-nil, want nil", tc.in)
   154  		}
   155  	}
   156  }
   157  

View as plain text