-
Notifications
You must be signed in to change notification settings - Fork 4.5k
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
isaiahvita
wants to merge
1
commit into
grpc:master
Choose a base branch
from
isaiahvita:isaiahvita-encoding-tests
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+98
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
// 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
} | ||
}() | ||
}) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.