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
assert: handle TokenTooLong error scenario #1559
Open
arjunmahishi
wants to merge
9
commits into
stretchr:master
Choose a base branch
from
arjunmahishi:scan-max-tokens
base: master
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
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.
Open
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
7f5f3e3
assert: handle TokenTooLong error scenario while formatting assertion…
arjunmahishi f37ae55
assert: add comment that explains the MaxScanTokenSize handling logic
arjunmahishi 48a148a
assert: Fix typos in Test_indentMessageLines
arjunmahishi f24d768
assert: move Test_indentMessageLines into a separate file
arjunmahishi 6b0dfad
assert: make Test_indentMessageLines more deterministic
arjunmahishi 594ac7d
assert: indentMessageLines - build message with bytes instead of string
arjunmahishi f341282
assert: add more test cases to Test_indentMessageLines
arjunmahishi b83d206
assert: pre-compute the indent spaces in indentMessageLines
arjunmahishi 9e31964
assert: reduce memory allocations in indentMessageLines
arjunmahishi File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package assert | ||
|
||
import ( | ||
"bufio" | ||
"bytes" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func Test_indentMessageLines(t *testing.T) { | ||
tt := []struct { | ||
name string | ||
longestLabelLen int | ||
|
||
// the input is constructed based on the below parameters | ||
bytesPerLine int | ||
lineCount int | ||
}{ | ||
{ | ||
name: "single line - over the bufio default limit", | ||
longestLabelLen: 11, | ||
bytesPerLine: bufio.MaxScanTokenSize + 10, | ||
lineCount: 1, | ||
}, | ||
{ | ||
name: "multi line - over the bufio default limit", | ||
longestLabelLen: 11, | ||
bytesPerLine: bufio.MaxScanTokenSize + 10, | ||
lineCount: 3, | ||
}, | ||
{ | ||
name: "single line - just under the bufio default limit", | ||
longestLabelLen: 11, | ||
bytesPerLine: bufio.MaxScanTokenSize - 10, | ||
lineCount: 1, | ||
}, | ||
{ | ||
name: "single line - just under the bufio default limit", | ||
longestLabelLen: 11, | ||
bytesPerLine: bufio.MaxScanTokenSize - 10, | ||
lineCount: 1, | ||
}, | ||
{ | ||
name: "single line - equal to the bufio default limit", | ||
longestLabelLen: 11, | ||
bytesPerLine: bufio.MaxScanTokenSize, | ||
lineCount: 1, | ||
}, | ||
{ | ||
name: "multi line - equal to the bufio default limit", | ||
longestLabelLen: 11, | ||
bytesPerLine: bufio.MaxScanTokenSize, | ||
lineCount: 3, | ||
}, | ||
{ | ||
name: "longest label length is zero", | ||
longestLabelLen: 0, | ||
bytesPerLine: 10, | ||
lineCount: 1, | ||
}, | ||
} | ||
|
||
for _, tc := range tt { | ||
t.Run(tc.name, func(t *testing.T) { | ||
var input bytes.Buffer | ||
for i := 0; i < tc.lineCount; i++ { | ||
input.WriteString(strings.Repeat("#", tc.bytesPerLine)) | ||
input.WriteRune('\n') | ||
} | ||
|
||
output := indentMessageLines( | ||
strings.TrimSpace(input.String()), tc.longestLabelLen, | ||
) | ||
outputLines := strings.Split(output, "\n") | ||
for i, line := range outputLines { | ||
if i > 0 { | ||
// count the leading white spaces. It should be equal to the longest | ||
// label length + 3. The +3 is to account for the 2 '\t' and 1 extra | ||
// space. Read the comment in the function for more context | ||
Equal(t, tc.longestLabelLen+3, strings.Index(line, "#")) | ||
} | ||
|
||
Len(t, strings.TrimSpace(line), tc.bytesPerLine) | ||
} | ||
}) | ||
} | ||
} |
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.
Is this safe? As we know from the assert.Len case this can contain a very large formatted slice. Having more than doubled the memory consumed by the large slice by creating a string containing the formatted version of it. Do we want to double the formatted version again? Plus all the intermediate buffers that were allocated on the heap by msgScanner before the next GC cycle?
We could at least pre-allocate the buffer for msgScanner rather than passing in
nil
. But I'd prefer that we either split or truncate lines that are unreasonably long, or we could just truncate the entire message to less thanbufio.MaxScanTokenSize
?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.
Agreed. I've made this change.
If we decide to truncate, we could probably truncate based on the dimensions of the terminal? This makes more sense from a UX point of view.