trpc.group/trpc-go/trpc-go@v1.0.3/codec/message_internal_test.go (about)

     1  package codec
     2  
     3  import (
     4  	"regexp"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/require"
     8  )
     9  
    10  func BenchmarkRPCNameIsTRPCForm(b *testing.B) {
    11  	rpcNames := []string{
    12  		"/trpc.app.server.service/method",
    13  		"/sdadfasd/xadfasdf/zxcasd/asdfasd/v2",
    14  		"trpc.app.server.service",
    15  		"/trpc.app.server.service",
    16  		"/trpc.app.",
    17  		"/trpc/asdf/asdf",
    18  		"/trpc.asdfasdf/asdfasdf/sdfasdfa/",
    19  		"/trpc.app/method/",
    20  		"/trpc.app/method/hhhhh",
    21  	}
    22  	b.Run("bench regexp", func(b *testing.B) {
    23  		for i := 0; i < b.N; i++ {
    24  			for j := range rpcNames {
    25  				rpcNameIsTRPCFormRegExp(rpcNames[j])
    26  			}
    27  		}
    28  	})
    29  	b.Run("bench vanilla", func(b *testing.B) {
    30  		for i := 0; i < b.N; i++ {
    31  			for j := range rpcNames {
    32  				rpcNameIsTRPCForm(rpcNames[j])
    33  			}
    34  		}
    35  	})
    36  }
    37  
    38  func TestEnsureEqualSemacticOfTRPCFormChecking(t *testing.T) {
    39  	rpcNames := []string{
    40  		"/trpc.app.server.service/method",
    41  		"/trpc.app.server.service/",
    42  		"/trpc",
    43  		"//",
    44  		"/./",
    45  		"/xx/.",
    46  		"/x./method",
    47  		"/.x/method",
    48  		"/sdadfasd/xadfasdf/zxcasd/asdfasd/v2",
    49  		"trpc.app.server.service",
    50  		"/trpc.app.server.service",
    51  		"/trpc.app.",
    52  		"/trpc/asdf/asdf",
    53  		"/trpc.asdfasdf/asdfasdf/sdfasdfa/",
    54  		"/trpc.app/method/",
    55  		"/trpc.app/method/hhhhh",
    56  	}
    57  	for _, s := range rpcNames {
    58  		v1, v2 := rpcNameIsTRPCFormRegExp(s), rpcNameIsTRPCForm(s)
    59  		require.True(t, v1 == v2, "%s %v %v", s, v1, v2)
    60  	}
    61  }
    62  
    63  var r = regexp.MustCompile(`^/[^/.]+\.[^/]+/[^/.]+$`)
    64  
    65  func rpcNameIsTRPCFormRegExp(s string) bool {
    66  	return r.MatchString(s)
    67  }