-
Notifications
You must be signed in to change notification settings - Fork 5
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
Relax expectation for unspecified errors, fix expectation for cardinality violations #833
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ac2d69b
relaxed error code checks for some grpc errors that aren't fully spec…
jhump a014445
add cardinality violation tests for server-stream
jhump 28a95c8
fix test expectations
jhump ca66bf0
Merge branch 'main' into jh/relax-expectation-for-unspecified-errors
jhump f947757
add comment to proto for new field; add section to docs
jhump 1a35712
improve computation of expected codes string
jhump e287a2a
add TODO
jhump 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 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
This file contains 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
This file contains 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 |
---|---|---|
|
@@ -158,7 +158,7 @@ func (r *testResults) assert( | |
expected := definition.ExpectedResponse | ||
var errs multiErrors | ||
|
||
errs = append(errs, checkError(expected.Error, actual.Error)...) | ||
errs = append(errs, checkError(expected.Error, actual.Error, definition.OtherAllowedErrorCodes)...) | ||
errs = append(errs, checkPayloads(expected.Payloads, actual.Payloads)...) | ||
|
||
if len(expected.Payloads) == 0 && | ||
|
@@ -459,7 +459,7 @@ func checkPayloads(expected, actual []*conformancev1.ConformancePayload) multiEr | |
return errs | ||
} | ||
|
||
func checkError(expected, actual *conformancev1.Error) multiErrors { | ||
func checkError(expected, actual *conformancev1.Error, otherCodes []conformancev1.Code) multiErrors { | ||
switch { | ||
case expected == nil && actual == nil: | ||
// nothing to do | ||
|
@@ -471,13 +471,14 @@ func checkError(expected, actual *conformancev1.Error) multiErrors { | |
} | ||
|
||
var errs multiErrors | ||
if expected.Code != actual.Code { | ||
errs = append(errs, fmt.Errorf("actual error code %d (%s) does not match expected code %d (%s)", | ||
actual.Code, connect.Code(actual.Code).String(), expected.Code, connect.Code(expected.Code).String())) | ||
if expected.Code != actual.Code && !inSlice(actual.Code, otherCodes) { | ||
expectedCodes := expectedCodeString(expected.Code, otherCodes) | ||
errs = append(errs, fmt.Errorf("actual error {code: %d (%s), message: %q} does not match expected code %s", | ||
actual.Code, connect.Code(actual.Code).String(), actual.GetMessage(), expectedCodes)) | ||
} | ||
if expected.Message != nil && expected.GetMessage() != actual.GetMessage() { | ||
errs = append(errs, fmt.Errorf("actual error message %q does not match expected message %q", | ||
actual.GetMessage(), expected.GetMessage())) | ||
errs = append(errs, fmt.Errorf("actual error {code: %d (%s), message: %q} does not match expected message %q", | ||
actual.Code, connect.Code(actual.Code).String(), actual.GetMessage(), expected.GetMessage())) | ||
} | ||
if len(expected.Details) != len(actual.Details) { | ||
// TODO: Should this be more lenient? Are we okay with a Connect implementation adding extra | ||
|
@@ -530,3 +531,28 @@ func indent(s string) string { | |
} | ||
return strings.Join(lines, "\n") | ||
} | ||
|
||
func inSlice[T comparable](elem T, slice []T) bool { | ||
// TODO: delete this function when this repo is using Go 1.21 | ||
// and update call sites to instead use slices.Contains | ||
for _, item := range slice { | ||
if item == elem { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
func expectedCodeString(expectedCode conformancev1.Code, otherAllowedCodes []conformancev1.Code) string { | ||
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. Nice |
||
allowedCodes := make([]string, len(otherAllowedCodes)+1) | ||
for i, code := range append([]conformancev1.Code{expectedCode}, otherAllowedCodes...) { | ||
allowedCodes[i] = fmt.Sprintf("%d (%s)", code, connect.Code(code).String()) | ||
if i == len(allowedCodes)-1 && i != 0 { | ||
allowedCodes[i] = "or " + allowedCodes[i] | ||
} | ||
} | ||
if len(allowedCodes) < 3 { | ||
return strings.Join(allowedCodes, " ") | ||
} | ||
return strings.Join(allowedCodes, ", ") | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.
Oops, something went wrong.
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.
Would it make sense to use a
map[string]struct{}
or otherSet
type here to make searching easier?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.
The list of codes is always small, so linear scan seemed fine (likely faster than having to create a map). As far as just simplifying the code (since perf is not a big concern here), I think constructing the map from the repeated proto field would be the same amount of code as the linear search through the slice, so didn't seem like a clear win. Also, the linear search is part of the standard library in Go 1.21, which introduces a new
slices
package, so we could upgrade in the near future and then remove some of this code.I can add a comment to the
inSlice
function about removing it once we update to Go 1.21.