http3: migrate the error tests away from Ginkgo (#4876)

This commit is contained in:
Marten Seemann 2025-01-15 21:20:43 -08:00 committed by GitHub
parent 150b955d06
commit 259fd92306
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 103 additions and 57 deletions

View file

@ -7,33 +7,31 @@ import (
"path"
"runtime"
"strconv"
"testing"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/stretchr/testify/require"
)
var _ = Describe("error codes", func() {
It("has a string representation for every error code", func() {
// We parse the error code file, extract all constants, and verify that
// each of them has a string version. Go FTW!
_, thisfile, _, ok := runtime.Caller(0)
if !ok {
panic("Failed to get current frame")
}
filename := path.Join(path.Dir(thisfile), "error_codes.go")
fileAst, err := parser.ParseFile(token.NewFileSet(), filename, nil, 0)
Expect(err).NotTo(HaveOccurred())
constSpecs := fileAst.Decls[2].(*ast.GenDecl).Specs
Expect(len(constSpecs)).To(BeNumerically(">", 4)) // at time of writing
for _, c := range constSpecs {
valString := c.(*ast.ValueSpec).Values[0].(*ast.BasicLit).Value
val, err := strconv.ParseInt(valString, 0, 64)
Expect(err).NotTo(HaveOccurred())
Expect(ErrCode(val).String()).ToNot(Equal("unknown error code"))
}
})
func TestErrorCodes(t *testing.T) {
// We parse the error code file, extract all constants, and verify that
// each of them has a string version. Go FTW!
_, thisfile, _, ok := runtime.Caller(0)
require.True(t, ok, "Failed to get current frame")
It("has a string representation for unknown error codes", func() {
Expect(ErrCode(0x1337).String()).To(Equal("unknown error code: 0x1337"))
})
})
filename := path.Join(path.Dir(thisfile), "error_codes.go")
fileAst, err := parser.ParseFile(token.NewFileSet(), filename, nil, 0)
require.NoError(t, err)
constSpecs := fileAst.Decls[2].(*ast.GenDecl).Specs
require.Greater(t, len(constSpecs), 4) // at time of writing
for _, c := range constSpecs {
valString := c.(*ast.ValueSpec).Values[0].(*ast.BasicLit).Value
val, err := strconv.ParseInt(valString, 0, 64)
require.NoError(t, err)
require.NotEqual(t, "unknown error code", ErrCode(val).String())
}
// Test unknown error code
require.Equal(t, "unknown error code: 0x1337", ErrCode(0x1337).String())
}

View file

@ -2,40 +2,88 @@ package http3
import (
"errors"
"testing"
"github.com/quic-go/quic-go"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/stretchr/testify/require"
)
var _ = Describe("HTTP/3 errors", func() {
It("converts", func() {
Expect(maybeReplaceError(nil)).To(BeNil())
Expect(maybeReplaceError(errors.New("foobar"))).To(MatchError("foobar"))
Expect(maybeReplaceError(&quic.StreamError{
ErrorCode: 1337,
Remote: true,
})).To(Equal(&Error{
Remote: true,
ErrorCode: 1337,
}))
Expect(maybeReplaceError(&quic.ApplicationError{
ErrorCode: 42,
Remote: true,
ErrorMessage: "foobar",
})).To(Equal(&Error{
Remote: true,
ErrorCode: 42,
ErrorMessage: "foobar",
}))
})
func TestErrorConversion(t *testing.T) {
regularErr := errors.New("foobar")
It("has a string representation", func() {
Expect((&Error{ErrorCode: 0x10c, Remote: true}).Error()).To(Equal("H3_REQUEST_CANCELLED"))
Expect((&Error{ErrorCode: 0x10c, Remote: true, ErrorMessage: "foobar"}).Error()).To(Equal("H3_REQUEST_CANCELLED: foobar"))
Expect((&Error{ErrorCode: 0x10c, Remote: false}).Error()).To(Equal("H3_REQUEST_CANCELLED (local)"))
Expect((&Error{ErrorCode: 0x10c, Remote: false, ErrorMessage: "foobar"}).Error()).To(Equal("H3_REQUEST_CANCELLED (local): foobar"))
Expect((&Error{ErrorCode: 0x1337, Remote: true}).Error()).To(Equal("H3 error (0x1337)"))
})
})
tests := []struct {
name string
input error
expected error
}{
{name: "nil error", input: nil, expected: nil},
{name: "regular error", input: regularErr, expected: regularErr},
{
name: "stream error",
input: &quic.StreamError{ErrorCode: 1337, Remote: true},
expected: &Error{Remote: true, ErrorCode: 1337},
},
{
name: "application error",
input: &quic.ApplicationError{ErrorCode: 42, Remote: true, ErrorMessage: "foobar"},
expected: &Error{Remote: true, ErrorCode: 42, ErrorMessage: "foobar"},
},
{
name: "transport error",
input: &quic.TransportError{ErrorCode: 42, Remote: true, ErrorMessage: "foobar"},
expected: &quic.TransportError{ErrorCode: 42, Remote: true, ErrorMessage: "foobar"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := maybeReplaceError(tt.input)
if tt.expected == nil {
require.Nil(t, result)
} else {
require.ErrorIs(t, tt.expected, result)
}
})
}
}
func TestErrorString(t *testing.T) {
tests := []struct {
name string
err *Error
expected string
}{
{
name: "remote error",
err: &Error{ErrorCode: 0x10c, Remote: true},
expected: "H3_REQUEST_CANCELLED",
},
{
name: "remote error with message",
err: &Error{ErrorCode: 0x10c, Remote: true, ErrorMessage: "foobar"},
expected: "H3_REQUEST_CANCELLED: foobar",
},
{
name: "local error",
err: &Error{ErrorCode: 0x10c, Remote: false},
expected: "H3_REQUEST_CANCELLED (local)",
},
{
name: "local error with message",
err: &Error{ErrorCode: 0x10c, Remote: false, ErrorMessage: "foobar"},
expected: "H3_REQUEST_CANCELLED (local): foobar",
},
{
name: "unknown error code",
err: &Error{ErrorCode: 0x1337, Remote: true},
expected: "H3 error (0x1337)",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
require.Equal(t, tt.expected, tt.err.Error())
})
}
}