-
Notifications
You must be signed in to change notification settings - Fork 1
Reduce allocations in Parse and Format #6
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -252,6 +252,9 @@ func TestParse(t *testing.T) { | |
| "v1.2.3+metadata", | ||
| "v1.2.3+metadata-with-hyphen", | ||
| "v1.2.3+metadata.with.dots", | ||
| "v24.1.0-alpha:1", | ||
| "v24.1.0-rc_1-12-gabcdef12", | ||
| "v24.1.0-beta/1-cloudonly.2", | ||
| } | ||
| for _, str := range testData { | ||
| _, err := Parse(str) | ||
|
|
@@ -368,6 +371,100 @@ func TestParse(t *testing.T) { | |
| }) | ||
| } | ||
|
|
||
| func TestFormatPlaceholders(t *testing.T) { | ||
| for _, tc := range []struct { | ||
| rawVersion string | ||
| wantPhase string | ||
| }{ | ||
| {rawVersion: "v24.1.0-alpha.2", wantPhase: "alpha"}, | ||
| {rawVersion: "v24.1.0-beta.2", wantPhase: "beta"}, | ||
| {rawVersion: "v24.1.0-rc.2", wantPhase: "rc"}, | ||
| {rawVersion: "v24.1.0-cloudonly.2", wantPhase: "cloudonly"}, | ||
| {rawVersion: "v24.1.0", wantPhase: ""}, | ||
| {rawVersion: "v24.1.0-build-tag", wantPhase: ""}, | ||
| } { | ||
| t.Run(tc.rawVersion, func(t *testing.T) { | ||
| v := MustParse(tc.rawVersion) | ||
| require.Equal(t, tc.wantPhase, v.Format("%P")) | ||
| }) | ||
| } | ||
|
|
||
| require.PanicsWithValue(t, "unknown placeholders in format string: %Q", func() { | ||
| _ = MustParse("v24.1.0").Format("v%X.%Y.%Z-%Q") | ||
| }) | ||
| } | ||
|
|
||
| func TestParseSpecificPatternWins(t *testing.T) { | ||
| for _, tc := range []struct { | ||
| raw string | ||
| phase releasePhase | ||
| phaseOrdinal int | ||
| phaseSubOrdinal int | ||
| customOrdinal int | ||
| adhocLabel string | ||
| }{ | ||
| { | ||
| raw: "v23.2.0-cloudonly2", | ||
| phase: cloudonly, | ||
| phaseOrdinal: 2, | ||
|
Contributor
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. This is not a valid version, there must be a (I think? What did the old code version do with this version string?)
Contributor
Author
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. I think this matches the existing pattern? That accepts cloudonly with optional digits and no dot — it predates this PR. Do you know if these versions exist in the wild? If not, we could tighten the pattern to require a dot.
Contributor
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. Huh, you're right, and we have pre-existing test cases. My mistake! (I think as cloudonlys were being introduced there were some odd versions with inconsistent naming types; the set of tests in this package is based on a comprehensive review of the patterns of actual versions that existed in the CC control plane DB when I wrote this package, plus consultation with releng folks.) |
||
| }, | ||
| { | ||
| raw: "v23.2.0-cloudonly", | ||
dcrosta marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| phase: cloudonly, | ||
| phaseOrdinal: 0, | ||
| }, | ||
| { | ||
| raw: "v23.2.0-cloudonly.1", | ||
| phase: cloudonly, | ||
| phaseOrdinal: 1, | ||
| }, | ||
| { | ||
| raw: "v24.1.0-rc.1-12-gabcdef56", | ||
| phase: rc, | ||
| phaseOrdinal: 1, | ||
| customOrdinal: 12, | ||
benbardin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }, | ||
| { | ||
| raw: "v24.1.0-beta.1-cloudonly.2", | ||
| phase: beta, | ||
| phaseOrdinal: 1, | ||
| phaseSubOrdinal: 2, | ||
| }, | ||
| } { | ||
| t.Run(tc.raw, func(t *testing.T) { | ||
| v := MustParse(tc.raw) | ||
|
|
||
| require.Equal(t, tc.phase, v.phase) | ||
| require.Equal(t, tc.phaseOrdinal, v.phaseOrdinal) | ||
| require.Equal(t, tc.phaseSubOrdinal, v.phaseSubOrdinal) | ||
| require.Equal(t, tc.customOrdinal, v.customOrdinal) | ||
| require.Equal(t, tc.adhocLabel, v.adhocLabel) | ||
| require.False(t, v.IsAdhocBuild()) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func BenchmarkParse(b *testing.B) { | ||
| testData := []string{ | ||
| "v19.1.11", | ||
| "v21.1.0-1-g9cbe7c5281", | ||
| "v22.2.10-1-g7b8322d67c-fips", | ||
| "v23.1.0-alpha.1-1643-gdf8e73734e-fips", | ||
| "v23.2.0-rc.2-cloudonly-rc2", | ||
| "v24.3.0-alpha.1-cloudonly.1", | ||
| "v23.1.0-swenson-mr-4", | ||
| "sha256:6bbf843734d11db9cc5eb8ea77f6974032e17ad216c91ccecfaf52a4890eaa11:latest-v22.2-build", | ||
| } | ||
|
|
||
| b.ReportAllocs() | ||
| for i := 0; i < b.N; i++ { | ||
| _, err := Parse(testData[i%len(testData)]) | ||
| if err != nil { | ||
| b.Fatal(err) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func TestVersionCompare(t *testing.T) { | ||
| const aEqualsB = "equal" | ||
| const aLessThanB = "less than" | ||
|
|
||
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.
Here, I think? Is where cloudonly with no dot was allowed