Skip to content

Add unit tests for registering a codec #8244

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 98 additions & 0 deletions encoding/encoding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,3 +363,101 @@ func (s) TestForceCodecName(t *testing.T) {
t.Fatalf("ss.Client.EmptyCall(_, _) = _, %v; want _, nil", err)
}
}

type noopCodec struct {
encoding.CodecV2
ID int
CodecName string
}

func (m *noopCodec) Name() string {
return m.CodecName
}

// TestRegisterCodecV2 tests the existing behavioral assumptions made by
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// TestRegisterCodecV2 tests the existing behavioral assumptions made by
// TestRegisterCodecV2 tests verify the following behaviors of RegisterCodeV2:
- invalid codecs (nil, empty name) should panic
- if new codecs has same name as existing, existing codec is overwritten
- if codec with same name does not exist, it is registered

// RegisterCodeV2. The behavioral assumptions are:
// 1. invalid codecs (nil, empty name) should panic
// 2. same name codecs are valid, they just overwrite previous.
func TestRegisterCodecV2(t *testing.T) {
tests := []struct {
name string
codec1 encoding.CodecV2
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

might be good to name codec1 and codec2 as existingCodec and newCodec or add a comment against them

codec2 encoding.CodecV2
expectPanic bool
}{
{
name: "nil codes, panic",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo: nil codecs, panics

expectPanic: true,
},
{
name: "empty codecs, panic",
codec1: &noopCodec{
CodecName: "",
},
codec2: &noopCodec{
CodecName: "",
},
expectPanic: true,
},
{
name: "one of the codec has valid name",
codec1: &noopCodec{
CodecName: "first",
},
expectPanic: false,
},
{
name: "new codec overrides original codec if name is same",
codec1: &noopCodec{
ID: 1,
CodecName: "first",
},
codec2: &noopCodec{
ID: 2,
CodecName: "first",
},
expectPanic: false,
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {

func() {
defer func() {
r := recover()
// If a panic occurred
if r != nil {
if !test.expectPanic {
t.Errorf("[%v] Unexpected panic: %v\n", test.name, r)
}
} else {
// If no panic occurred
if test.expectPanic {
t.Errorf("[%v] Expected panic but function did not panic\n", test.name)
}
}
}()

// test behavior on initial registration
encoding.RegisterCodecV2(test.codec1)
actualCodec := encoding.GetCodecV2(test.codec1.Name())
if !cmp.Equal(test.codec1, actualCodec) {
t.Errorf("[%v] Expected returned codec to be equal.\n", test.name)
}
if test.codec2 == nil {
// only test behavior on subsequent registrations
// if the test case prescribes it by setting codec2
t.Skip()
}

// test behavior on subsequent registrations
encoding.RegisterCodecV2(test.codec2)
actualCodec = encoding.GetCodecV2(test.codec1.Name())
if !cmp.Equal(actualCodec, test.codec2) {
t.Errorf("[%v] Expected returned codec to be equal.\n", test.name)
}
}()
})
}
}