internal: move parseMethodName func to internal/grpcutil (#3525)

This commit is contained in:
Zou Nengren 2020-04-18 03:02:55 +08:00 committed by GitHub
parent 6774920a77
commit da1517890d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 51 additions and 64 deletions

View File

@ -25,6 +25,7 @@ import (
"os"
"google.golang.org/grpc/grpclog"
"google.golang.org/grpc/internal/grpcutil"
)
// Logger is the global binary logger. It can be used to get binary logger for
@ -146,7 +147,7 @@ func (l *logger) setBlacklist(method string) error {
// Each methodLogger returned by this method is a new instance. This is to
// generate sequence id within the call.
func (l *logger) getMethodLogger(methodName string) *MethodLogger {
s, m, err := parseMethodName(methodName)
s, m, err := grpcutil.ParseMethod(methodName)
if err != nil {
grpclog.Infof("binarylogging: failed to parse %q: %v", methodName, err)
return nil

View File

@ -1,59 +0,0 @@
/*
*
* Copyright 2018 gRPC authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package binarylog
import "testing"
func (s) TestParseMethodName(t *testing.T) {
testCases := []struct {
methodName string
service, method string
}{
{methodName: "/s/m", service: "s", method: "m"},
{methodName: "/p.s/m", service: "p.s", method: "m"},
{methodName: "/p/s/m", service: "p/s", method: "m"},
}
for _, tc := range testCases {
s, m, err := parseMethodName(tc.methodName)
if err != nil {
t.Errorf("Parsing %q got error %v, want nil", tc.methodName, err)
continue
}
if s != tc.service || m != tc.method {
t.Errorf("Parseing %q got service %q, method %q, want service %q, method %q",
tc.methodName, s, m, tc.service, tc.method,
)
}
}
}
func (s) TestParseMethodNameInvalid(t *testing.T) {
testCases := []string{
"/",
"/sm",
"",
"sm",
}
for _, tc := range testCases {
_, _, err := parseMethodName(tc)
if err == nil {
t.Errorf("Parsing %q got nil error, want non-nil error", tc)
}
}
}

View File

@ -16,18 +16,17 @@
*
*/
package binarylog
package grpcutil
import (
"errors"
"strings"
)
// parseMethodName splits service and method from the input. It expects format
// ParseMethod splits service and method from the input. It expects format
// "/service/method".
//
// TODO: move to internal/grpcutil.
func parseMethodName(methodName string) (service, method string, _ error) {
func ParseMethod(methodName string) (service, method string, _ error) {
if !strings.HasPrefix(methodName, "/") {
return "", "", errors.New("invalid method name: should start with /")
}

View File

@ -0,0 +1,46 @@
/*
*
* Copyright 2018 gRPC authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package grpcutil
import (
"testing"
)
func TestParseMethod(t *testing.T) {
testCases := []struct {
methodName string
wantService string
wantMethod string
wantError bool
}{
{methodName: "/s/m", wantService: "s", wantMethod: "m", wantError: false},
{methodName: "/p.s/m", wantService: "p.s", wantMethod: "m", wantError: false},
{methodName: "/p/s/m", wantService: "p/s", wantMethod: "m", wantError: false},
{methodName: "/", wantError: true},
{methodName: "/sm", wantError: true},
{methodName: "", wantError: true},
{methodName: "sm", wantError: true},
}
for _, tc := range testCases {
s, m, err := ParseMethod(tc.methodName)
if (err != nil) != tc.wantError || s != tc.wantService || m != tc.wantMethod {
t.Errorf("ParseMethod(%s) = (%s, %s, %v), want (%s, %s, %v)", tc.methodName, s, m, err, tc.wantService, tc.wantMethod, tc.wantError)
}
}
}