binarylog_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. package binarylog
  19. import (
  20. "testing"
  21. )
  22. // Test that get method logger returns the one with the most exact match.
  23. func TestGetMethodLogger(t *testing.T) {
  24. testCases := []struct {
  25. in string
  26. method string
  27. hdr, msg uint64
  28. }{
  29. // Global.
  30. {
  31. in: "*{h:12;m:23}",
  32. method: "/s/m",
  33. hdr: 12, msg: 23,
  34. },
  35. // service/*.
  36. {
  37. in: "*,s/*{h:12;m:23}",
  38. method: "/s/m",
  39. hdr: 12, msg: 23,
  40. },
  41. // Service/method.
  42. {
  43. in: "*{h;m},s/m{h:12;m:23}",
  44. method: "/s/m",
  45. hdr: 12, msg: 23,
  46. },
  47. {
  48. in: "*{h;m},s/*{h:314;m},s/m{h:12;m:23}",
  49. method: "/s/m",
  50. hdr: 12, msg: 23,
  51. },
  52. {
  53. in: "*{h;m},s/*{h:12;m:23},s/m",
  54. method: "/s/m",
  55. hdr: maxUInt, msg: maxUInt,
  56. },
  57. // service/*.
  58. {
  59. in: "*{h;m},s/*{h:12;m:23},s/m1",
  60. method: "/s/m",
  61. hdr: 12, msg: 23,
  62. },
  63. {
  64. in: "*{h;m},s1/*,s/m{h:12;m:23}",
  65. method: "/s/m",
  66. hdr: 12, msg: 23,
  67. },
  68. // With black list.
  69. {
  70. in: "*{h:12;m:23},-s/m1",
  71. method: "/s/m",
  72. hdr: 12, msg: 23,
  73. },
  74. }
  75. for _, tc := range testCases {
  76. l := NewLoggerFromConfigString(tc.in)
  77. if l == nil {
  78. t.Errorf("in: %q, failed to create logger from config string", tc.in)
  79. continue
  80. }
  81. ml := l.getMethodLogger(tc.method)
  82. if ml == nil {
  83. t.Errorf("in: %q, method logger is nil, want non-nil", tc.in)
  84. continue
  85. }
  86. if ml.headerMaxLen != tc.hdr || ml.messageMaxLen != tc.msg {
  87. t.Errorf("in: %q, want header: %v, message: %v, got header: %v, message: %v", tc.in, tc.hdr, tc.msg, ml.headerMaxLen, ml.messageMaxLen)
  88. }
  89. }
  90. }
  91. // expect method logger to be nil
  92. func TestGetMethodLoggerOff(t *testing.T) {
  93. testCases := []struct {
  94. in string
  95. method string
  96. }{
  97. // method not specified.
  98. {
  99. in: "s1/m",
  100. method: "/s/m",
  101. },
  102. {
  103. in: "s/m1",
  104. method: "/s/m",
  105. },
  106. {
  107. in: "s1/*",
  108. method: "/s/m",
  109. },
  110. {
  111. in: "s1/*,s/m1",
  112. method: "/s/m",
  113. },
  114. // blacklisted.
  115. {
  116. in: "*,-s/m",
  117. method: "/s/m",
  118. },
  119. {
  120. in: "s/*,-s/m",
  121. method: "/s/m",
  122. },
  123. {
  124. in: "-s/m,s/*",
  125. method: "/s/m",
  126. },
  127. }
  128. for _, tc := range testCases {
  129. l := NewLoggerFromConfigString(tc.in)
  130. if l == nil {
  131. t.Errorf("in: %q, failed to create logger from config string", tc.in)
  132. continue
  133. }
  134. ml := l.getMethodLogger(tc.method)
  135. if ml != nil {
  136. t.Errorf("in: %q, method logger is non-nil, want nil", tc.in)
  137. }
  138. }
  139. }