1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2026-06-03 18:35:08 +02:00

refactor: replace context.Background() with t.Context()/b.Context() in tests (#7352)

Based on the Go version we currently use, the dependency already
supports 1.24+, which allows using `t.Context()` and `b.Context()` in
unit tests and benchmarks respectively.

- Enable `context-background` and `context-todo` in
[`usetesting`](https://golangci-lint.run/docs/linters/configuration/#usetesting)
- Adjust the code to support linter detection

---------

Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com>
Co-authored-by: Tyler Yahn <codingalias@gmail.com>
Co-authored-by: Damien Mathieu <42@dmathieu.com>
This commit is contained in:
Flc゛
2025-09-23 15:52:45 +08:00
committed by GitHub
parent 2389f4488f
commit 80cb909774
106 changed files with 763 additions and 778 deletions
+2 -2
View File
@@ -41,7 +41,7 @@ func TestLoggerProviderConcurrentSafe(*testing.T) {
<-done
}
func TestLoggerConcurrentSafe(*testing.T) {
func TestLoggerConcurrentSafe(t *testing.T) {
l := &logger{}
done := make(chan struct{})
@@ -50,7 +50,7 @@ func TestLoggerConcurrentSafe(*testing.T) {
go func() {
defer close(done)
ctx := context.Background()
ctx := t.Context()
var r log.Record
var param log.EnabledParameters
+4 -5
View File
@@ -4,7 +4,6 @@
package logtest
import (
"context"
"fmt"
"testing"
"time"
@@ -63,7 +62,7 @@ func TestAssertEqualRecording(t *testing.T) {
Scope{Name: t.Name()}: []Record{
{
Timestamp: y2k,
Context: context.Background(),
Context: t.Context(),
Attributes: []log.KeyValue{log.Int("n", 1), log.String("foo", "bar")},
},
},
@@ -72,7 +71,7 @@ func TestAssertEqualRecording(t *testing.T) {
Scope{Name: t.Name()}: []Record{
{
Timestamp: y2k,
Context: context.Background(),
Context: t.Context(),
Attributes: []log.KeyValue{log.String("foo", "bar"), log.Int("n", 1)},
},
},
@@ -145,12 +144,12 @@ func TestAssertEqualRecord(t *testing.T) {
name: "equal records",
a: Record{
Timestamp: y2k,
Context: context.Background(),
Context: t.Context(),
Attributes: []log.KeyValue{log.Int("n", 1), log.String("foo", "bar")},
},
b: Record{
Timestamp: y2k,
Context: context.Background(),
Context: t.Context(),
Attributes: []log.KeyValue{log.String("foo", "bar"), log.Int("n", 1)},
},
want: true,
+1 -2
View File
@@ -4,7 +4,6 @@
package logtest_test
import (
"context"
"testing"
"time"
@@ -26,7 +25,7 @@ func Example() {
r.SetBody(log.StringValue("Hello there"))
r.AddAttributes(log.String("foo", "bar"))
r.AddAttributes(log.Int("n", 1))
l.Emit(context.Background(), r)
l.Emit(t.Context(), r)
// Verify that the expected and actual log records match.
want := logtest.Recording{
+7 -7
View File
@@ -73,7 +73,7 @@ func TestLoggerEnabled(t *testing.T) {
}{
{
name: "the default option enables every log entry",
ctx: context.Background(),
ctx: t.Context(),
want: true,
},
{
@@ -83,7 +83,7 @@ func TestLoggerEnabled(t *testing.T) {
return false
}),
},
ctx: context.Background(),
ctx: t.Context(),
want: false,
},
} {
@@ -96,7 +96,7 @@ func TestLoggerEnabled(t *testing.T) {
func TestLoggerEnabledFnUnset(t *testing.T) {
r := &logger{}
assert.True(t, r.Enabled(context.Background(), log.EnabledParameters{}))
assert.True(t, r.Enabled(t.Context(), log.EnabledParameters{}))
}
func TestRecorderLoggerEmitAndReset(t *testing.T) {
@@ -104,7 +104,7 @@ func TestRecorderLoggerEmitAndReset(t *testing.T) {
ts := time.Now()
l := rec.Logger(t.Name())
ctx := context.Background()
ctx := t.Context()
r := log.Record{}
r.SetTimestamp(ts)
r.SetSeverity(log.SeverityInfo)
@@ -149,7 +149,7 @@ func TestRecorderLoggerEmitAndReset(t *testing.T) {
assert.Equal(t, want, got)
}
func TestRecorderConcurrentSafe(*testing.T) {
func TestRecorderConcurrentSafe(t *testing.T) {
const goRoutineN = 10
var wg sync.WaitGroup
@@ -162,8 +162,8 @@ func TestRecorderConcurrentSafe(*testing.T) {
defer wg.Done()
nr := r.Logger("test")
nr.Enabled(context.Background(), log.EnabledParameters{})
nr.Emit(context.Background(), log.Record{})
nr.Enabled(t.Context(), log.EnabledParameters{})
nr.Emit(t.Context(), log.Record{})
r.Result()
r.Reset()
+1 -2
View File
@@ -4,7 +4,6 @@
package noop // import "go.opentelemetry.io/otel/log/noop"
import (
"context"
"reflect"
"testing"
@@ -44,7 +43,7 @@ func assertAllExportedMethodNoPanic(rVal reflect.Value, rType reflect.Type) func
numIn--
}
args := make([]reflect.Value, numIn)
ctx := context.Background()
ctx := t.Context()
for i := range args {
aType := mType.Type.In(i)
if aType.Name() == "Context" {