Skip to content

grpctest: minor improvements to the test logger implementation #8370

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
wants to merge 22 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions internal/grpctest/grpctest.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ type Tester struct{}

// Setup updates the tlogger.
func (Tester) Setup(t *testing.T) {
tLogr.Update(t)
tLogr.update(t)
// TODO: There is one final leak around closing connections without completely
// draining the recvBuffer that has yet to be resolved. All other leaks have been
// completely addressed, and this can be turned back on as soon as this issue is
Expand All @@ -75,7 +75,7 @@ func (Tester) Teardown(t *testing.T) {
if atomic.LoadUint32(&lcFailed) == 1 {
t.Log("Goroutine leak check disabled for future tests")
}
tLogr.EndTest(t)
tLogr.endTest(t)
}

// Interface defines Tester's methods for use in this package.
Expand Down
39 changes: 22 additions & 17 deletions internal/grpctest/tlogger.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,21 @@
}

func init() {
tLogr = &tLogger{errors: map[*regexp.Regexp]int{}}
vLevel := os.Getenv("GRPC_GO_LOG_VERBOSITY_LEVEL")
if vl, err := strconv.Atoi(vLevel); err == nil {
tLogr.v = vl
vLevel := 0 // Default verbosity level

if vLevelEnv, found := os.LookupEnv("GRPC_GO_LOG_VERBOSITY_LEVEL"); found {
// If found, attempt to convert. If conversion is successful, update vLevel.
// If conversion fails, log a warning, but vLevel remains its default of 0.
if val, err := strconv.Atoi(vLevelEnv); err == nil {
vLevel = val
} else {
// Log the error if the environment variable is not a valid integer.
fmt.Printf("Warning: GRPC_GO_LOG_VERBOSITY_LEVEL environment variable '%s' is not a valid integer. "+
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@arjan-bal is it fine if we print this message?

"Using default verbosity level 0. Error: %v\n", vLevelEnv, err)
}

Check warning on line 87 in internal/grpctest/tlogger.go

View check run for this annotation

Codecov / codecov/patch

internal/grpctest/tlogger.go#L79-L87

Added lines #L79 - L87 were not covered by tests
}
}

func getLogger() *tLogger {
return tLogr
// Initialize tLogr with the determined verbosity level.
tLogr = &tLogger{errors: make(map[*regexp.Regexp]int), v: vLevel}
}

// getCallingPrefix returns the <file:line> at the given depth from the stack.
Expand All @@ -99,7 +105,7 @@
defer tl.mu.Unlock()
prefix, err := getCallingPrefix(callingFrame + depth)
if err != nil {
tl.t.Error(err)

Check warning on line 108 in internal/grpctest/tlogger.go

View check run for this annotation

Codecov / codecov/patch

internal/grpctest/tlogger.go#L108

Added line #L108 was not covered by tests
return
}
args = append([]any{ltype.String() + " " + prefix}, args...)
Expand All @@ -112,7 +118,7 @@
if tl.expected(fmt.Sprintln(args...)) {
tl.t.Log(args...)
} else {
tl.t.Error(args...)

Check warning on line 121 in internal/grpctest/tlogger.go

View check run for this annotation

Codecov / codecov/patch

internal/grpctest/tlogger.go#L121

Added line #L121 was not covered by tests
}
case fatalLog:
panic(fmt.Sprint(args...))
Expand All @@ -127,7 +133,7 @@
if tl.expected(fmt.Sprintf(format, args...)) {
tl.t.Logf(format, args...)
} else {
tl.t.Errorf(format, args...)

Check warning on line 136 in internal/grpctest/tlogger.go

View check run for this annotation

Codecov / codecov/patch

internal/grpctest/tlogger.go#L136

Added line #L136 was not covered by tests
}
case fatalLog:
panic(fmt.Sprintf(format, args...))
Expand All @@ -137,9 +143,9 @@
}
}

// Update updates the testing.T that the testing logger logs to. Should be done
// update updates the testing.T that the testing logger logs to. Should be done
// before every test. It also initializes the tLogger if it has not already.
func (tl *tLogger) Update(t *testing.T) {
func (tl *tLogger) update(t *testing.T) {
tl.mu.Lock()
defer tl.mu.Unlock()
if !tl.initialized {
Expand All @@ -162,19 +168,18 @@

// ExpectErrorN declares an error to be expected n times.
func ExpectErrorN(expr string, n int) {
tl := getLogger()
tl.mu.Lock()
defer tl.mu.Unlock()
tLogr.mu.Lock()
defer tLogr.mu.Unlock()
re, err := regexp.Compile(expr)
if err != nil {
tl.t.Error(err)
tLogr.t.Error(err)

Check warning on line 175 in internal/grpctest/tlogger.go

View check run for this annotation

Codecov / codecov/patch

internal/grpctest/tlogger.go#L175

Added line #L175 was not covered by tests
return
}
tl.errors[re] += n
tLogr.errors[re] += n
}

// EndTest checks if expected errors were not encountered.
func (tl *tLogger) EndTest(t *testing.T) {
// endTest checks if expected errors were not encountered.
func (tl *tLogger) endTest(t *testing.T) {
tl.mu.Lock()
defer tl.mu.Unlock()
for re, count := range tl.errors {
Expand Down Expand Up @@ -247,16 +252,16 @@
tl.log(errorLog, depth, "", args...)
}

func (tl *tLogger) Fatal(args ...any) {
tl.log(fatalLog, 0, "", args...)

Check warning on line 256 in internal/grpctest/tlogger.go

View check run for this annotation

Codecov / codecov/patch

internal/grpctest/tlogger.go#L255-L256

Added lines #L255 - L256 were not covered by tests
}

func (tl *tLogger) Fatalln(args ...any) {
tl.log(fatalLog, 0, "", args...)

Check warning on line 260 in internal/grpctest/tlogger.go

View check run for this annotation

Codecov / codecov/patch

internal/grpctest/tlogger.go#L259-L260

Added lines #L259 - L260 were not covered by tests
}

func (tl *tLogger) Fatalf(format string, args ...any) {
tl.log(fatalLog, 0, format, args...)

Check warning on line 264 in internal/grpctest/tlogger.go

View check run for this annotation

Codecov / codecov/patch

internal/grpctest/tlogger.go#L263-L264

Added lines #L263 - L264 were not covered by tests
}

func (tl *tLogger) FatalDepth(depth int, args ...any) {
Expand Down