Repository: grpc/grpc-go
File: internal/transport/http_util_test.go (Go)

Code before:
	"testing"
	"time"
)

func (s) TestDecodeTimeout(t *testing.T) {
	for _, test := range []struct {
		// input
		s string
		// output
		d   time.Duration
		err error
	}{
		{"0S", 0, nil},
		{"00000000S", 0, nil},
		{"00000001n", time.Nanosecond, nil},
		{"10u", time.Microsecond * 10, nil},
		{"00000010m", time.Millisecond * 10, nil},
		{"1234S", time.Second * 1234, nil},
		{"00000001M", time.Minute, nil},
		{"09999999S", time.Second * 9999999, nil},
		{"99999999S", time.Second * 99999999, nil},
		{"99999999M", time.Minute * 99999999, nil},
		{"2562047H", time.Hour * 2562047, nil},
		{"2562048H", time.Duration(math.MaxInt64), nil},
		{"99999999H", time.Duration(math.MaxInt64), nil},
		{"-1S", 0, fmt.Errorf("strconv.ParseUint: parsing %q: invalid syntax", "-1")},
		{"1234x", 0, fmt.Errorf("transport: timeout unit is not recognized: %q", "1234x")},
		{"1234s", 0, fmt.Errorf("transport: timeout unit is not recognized: %q", "1234s")},
		{"1234", 0, fmt.Errorf("transport: timeout unit is not recognized: %q", "1234")},
		{"1", 0, fmt.Errorf("transport: timeout string is too short: %q", "1")},
		{"", 0, fmt.Errorf("transport: timeout string is too short: %q", "")},
		{"9a1S", 0, fmt.Errorf("strconv.ParseUint: parsing %q: invalid syntax", "9a1")},
		{"000000000S", 0, fmt.Errorf("transport: timeout string is too long: %q", "000000000S")},
	} {
		d, err := decodeTimeout(test.s)
		if d != test.d || fmt.Sprint(err) != fmt.Sprint(test.err) {
			t.Errorf("timeoutDecode(%q) = %d, %v, want %d, %v", test.s, int64(d), err, int64(test.d), test.err)
		}
	}
}

func (s) TestEncodeGrpcMessage(t *testing.T) {
	for _, tt := range []struct {
		input    string
		expected string
	}{
		{"", ""},
		{"Hello", "Hello"},
		{"\u0000", "%00"},
		{"%", "%25"},
		{"系统", "%E7%B3%BB%E7%BB%9F"},

Review comment (suggestion):
This error string is not part of gRPC and can cause the test to break when run with a different version of golang. You can instead use cmp.Equal() with cmpopts.EquateErrors(). You can use `cmpopts.AnyError` as the expected value.

https://google.github.io/styleguide/go/decisions#test-error-semantics

Code after:
	"testing"
	"time"
)

func (s) TestDecodeTimeout(t *testing.T) {
	for _, test := range []struct {
		// input
		s string
		// output
		d       time.Duration
		wantErr bool
	}{

		{"00000001n", time.Nanosecond, false},
		{"10u", time.Microsecond * 10, false},
		{"00000010m", time.Millisecond * 10, false},
		{"1234S", time.Second * 1234, false},
		{"00000001M", time.Minute, false},
		{"09999999S", time.Second * 9999999, false},
		{"99999999S", time.Second * 99999999, false},
		{"99999999M", time.Minute * 99999999, false},
		{"2562047H", time.Hour * 2562047, false},
		{"2562048H", time.Duration(math.MaxInt64), false},
		{"99999999H", time.Duration(math.MaxInt64), false},
		{"-1S", 0, true},
		{"1234x", 0, true},
		{"1234s", 0, true},
		{"1234", 0, true},
		{"1", 0, true},
		{"", 0, true},
		{"9a1S", 0, true},
		{"0S", 0, true}, // PROTOCOL-HTTP2.md requires positive integers
		{"00000000S", 0, true},
		{"000000000S", 0, true},
	} {
		d, err := decodeTimeout(test.s)
		gotErr := err != nil
		if d != test.d || gotErr != test.wantErr {
			t.Errorf("timeoutDecode(%q) = %d, %v, want %d, wantErr=%v",
				test.s, int64(d), err, int64(test.d), test.wantErr)
		}
	}
}

func (s) TestEncodeGrpcMessage(t *testing.T) {
	for _, tt := range []struct {
		input    string
		expected string
	}{
		{"", ""},
		{"Hello", "Hello"},