Skip to content

Commit 4438aeb

Browse files
authored
fix(profiler): refine regular expression for parsing backoff duration in E2E tests (#5229)
1 parent ce6e7a0 commit 4438aeb

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

profiler/proftest/proftest.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ var (
4747

4848
// "ms" must be specified before "m" in the regexp, to ensure "ms" is fully
4949
// matched.
50-
backoffTimeRE = regexp.MustCompile(`(\d+(\.\d+)?(ms|h|m|s|us))+`)
50+
backoffTimeRE = regexp.MustCompile(`(?:^|\W)((\d+(\.\d+)?(ms|h|m|s|us))+)(?:$|\W)`)
5151
)
5252

5353
// BaseStartupTmpl is the common part of the startup script that
@@ -454,10 +454,11 @@ func parseLogTime(line string) (time.Time, error) {
454454
// parseBackoffDuration returns the backoff duration associated with a logged
455455
// line, or an error if the line does not contain a valid backoff duration.
456456
func parseBackoffDuration(line string) (time.Duration, error) {
457-
backoffTimeStr := backoffTimeRE.FindString(line)
458-
if backoffTimeStr == "" {
457+
backoffTimeMatch := backoffTimeRE.FindStringSubmatch(line)
458+
if len(backoffTimeMatch) < 2 || backoffTimeMatch[1] == "" {
459459
return 0, fmt.Errorf("log for server-specified backoff %q does not include a backoff time", line)
460460
}
461+
backoffTimeStr := backoffTimeMatch[1]
461462
backoff, err := time.ParseDuration(backoffTimeStr)
462463
if err != nil {
463464
return 0, fmt.Errorf("failed to parse backoff duration %q", backoffTimeStr)

profiler/proftest/proftest_test.go

+23-2
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,24 @@ func TestParseBackoffDuration(t *testing.T) {
120120
wantErr bool
121121
}{
122122
{
123-
desc: "a valid backoff duration is parsed correctly",
124-
line: "Fri May 15 22:05:01 UTC 2020: benchmark 0: failed to create profile, will retry: rpc error: code = Aborted desc = generic::aborted: action throttled, backoff for 32m0s",
123+
desc: "a valid backoff duration is parsed correctly when at the end of the message",
124+
line: "Fri May 15 22:05:01 UTC 2020: benchmark 0: action throttled, backoff for 32m0s",
125+
wantBackoffDur: 32 * time.Minute,
126+
},
127+
{
128+
desc: "a valid backoff duration is parsed correctly when at the end of a message ending with a period",
129+
line: "Fri May 15 22:05:01 UTC 2020: benchmark 0: failed to create profile, will retry: rpc error: code = Aborted desc = generic::aborted: action throttled, backoff for 32m0s.",
130+
wantBackoffDur: 32 * time.Minute,
131+
},
132+
{
133+
desc: "a valid backoff duration in a structured log statement is parsed correctly",
134+
line: `Mon Dec 20 20:21:40 UTC 2021: benchmark 39: {"timestamp":"2021-12-20T20:21:39.973Z","severity":"DEBUG","logging.googleapis.com/insertId":"..........K231soqV4EIcG8w42yR2.3","message":"Must wait 35m to create profile: Error: generic::aborted: action throttled, backoff for 35m0s","logName":"projects/{{projectId}}/logs/%40google-cloud%2Fprofiler","resource":{"type":"gae_app","labels":{"module_id":"profiler-backoff-test-node12-2021-12-20-12-18-47-077307-0800","zone":"us-east4-b"}}}`,
135+
wantBackoffDur: 35 * time.Minute,
136+
},
137+
138+
{
139+
desc: "a valid backoff duration is parsed correctly when in the middle of the message",
140+
line: "Fri May 15 22:05:01 UTC 2020: benchmark 0: failed to create profile, will retry: rpc error: code = Aborted desc = generic::aborted: action throttled, backoff for 32m0s ... more information",
125141
wantBackoffDur: 32 * time.Minute,
126142
},
127143
{
@@ -139,6 +155,11 @@ func TestParseBackoffDuration(t *testing.T) {
139155
line: "Fri May 15 22:05:01 UTC 2020: benchmark 0: failed to create profile, will retry: rpc error: code = Aborted desc = generic::aborted: action throttled, backoff for 1h1m1s1ms1us",
140156
wantBackoffDur: time.Hour + time.Minute + time.Second + time.Millisecond + time.Microsecond,
141157
},
158+
{
159+
desc: "a backoff duration at the start of the message is parsed correctly",
160+
line: "1h1m1s1ms",
161+
wantBackoffDur: time.Hour + time.Minute + time.Second + time.Millisecond,
162+
},
142163
} {
143164
t.Run(tc.desc, func(t *testing.T) {
144165
backoffDur, err := parseBackoffDuration(tc.line)

0 commit comments

Comments
 (0)