Skip to content

Commit 6013b9e

Browse files
authored
Test: replace context.TODO and context.Background to t.Context in uni… (#4355)
Problem: Unit tests currently use context.TODO() and context.Background(), which can potentially lead to goroutine leaks and resource cleanup issues because these contexts are not automatically canceled when a test completes. Solution: Replaced usage of context.TODO() and context.Background() with t.Context() in unit tests. This ensures that contexts are automatically canceled when tests complete and aligns the test suite with modern Go 1.24+ testing practices. Signed-off-by: richie <mychenforeverl@gmail.com>
1 parent f2ebf93 commit 6013b9e

File tree

16 files changed

+126
-130
lines changed

16 files changed

+126
-130
lines changed

internal/controller/nginx/agent/broadcast/broadcast_test.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package broadcast_test
22

33
import (
4-
"context"
54
"testing"
65

76
. "github.com/onsi/gomega"
@@ -16,7 +15,7 @@ func TestSubscribe(t *testing.T) {
1615
stopCh := make(chan struct{})
1716
defer close(stopCh)
1817

19-
broadcaster := broadcast.NewDeploymentBroadcaster(context.Background(), stopCh)
18+
broadcaster := broadcast.NewDeploymentBroadcaster(t.Context(), stopCh)
2019

2120
subscriber := broadcaster.Subscribe()
2221
g.Expect(subscriber.ID).NotTo(BeEmpty())
@@ -41,7 +40,7 @@ func TestSubscribe_MultipleListeners(t *testing.T) {
4140
stopCh := make(chan struct{})
4241
defer close(stopCh)
4342

44-
broadcaster := broadcast.NewDeploymentBroadcaster(context.Background(), stopCh)
43+
broadcaster := broadcast.NewDeploymentBroadcaster(t.Context(), stopCh)
4544

4645
subscriber1 := broadcaster.Subscribe()
4746
subscriber2 := broadcaster.Subscribe()
@@ -70,7 +69,7 @@ func TestSubscribe_NoListeners(t *testing.T) {
7069
stopCh := make(chan struct{})
7170
defer close(stopCh)
7271

73-
broadcaster := broadcast.NewDeploymentBroadcaster(context.Background(), stopCh)
72+
broadcaster := broadcast.NewDeploymentBroadcaster(t.Context(), stopCh)
7473

7574
message := broadcast.NginxAgentMessage{
7675
ConfigVersion: "v1",
@@ -88,7 +87,7 @@ func TestCancelSubscription(t *testing.T) {
8887
stopCh := make(chan struct{})
8988
defer close(stopCh)
9089

91-
broadcaster := broadcast.NewDeploymentBroadcaster(context.Background(), stopCh)
90+
broadcaster := broadcast.NewDeploymentBroadcaster(t.Context(), stopCh)
9291

9392
subscriber := broadcaster.Subscribe()
9493

internal/controller/nginx/agent/command_test.go

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,16 @@ func createFakeK8sClient(initObjs ...runtime.Object) (client.Client, error) {
7373
return fakeClient, nil
7474
}
7575

76-
func createGrpcContext() context.Context {
77-
return grpcContext.NewGrpcContext(context.Background(), grpcContext.GrpcInfo{
76+
func createGrpcContext(t *testing.T) context.Context {
77+
t.Helper()
78+
return grpcContext.NewGrpcContext(t.Context(), grpcContext.GrpcInfo{
7879
UUID: "1234567",
7980
})
8081
}
8182

82-
func createGrpcContextWithCancel() (context.Context, context.CancelFunc) {
83-
ctx, cancel := context.WithCancel(context.Background())
84-
83+
func createGrpcContextWithCancel(t *testing.T) (context.Context, context.CancelFunc) {
84+
t.Helper()
85+
ctx, cancel := context.WithCancel(t.Context())
8586
return grpcContext.NewGrpcContext(ctx, grpcContext.GrpcInfo{
8687
UUID: "1234567",
8788
}), cancel
@@ -122,7 +123,7 @@ func TestCreateConnection(t *testing.T) {
122123
}{
123124
{
124125
name: "successfully tracks a connection",
125-
ctx: createGrpcContext(),
126+
ctx: createGrpcContext(t),
126127
request: &pb.CreateConnectionRequest{
127128
Resource: &pb.Resource{
128129
Info: &pb.Resource_ContainerInfo{
@@ -181,14 +182,14 @@ func TestCreateConnection(t *testing.T) {
181182
},
182183
{
183184
name: "context is missing data",
184-
ctx: context.Background(),
185+
ctx: t.Context(),
185186
request: &pb.CreateConnectionRequest{},
186187
response: nil,
187188
errString: agentgrpc.ErrStatusInvalidConnection.Error(),
188189
},
189190
{
190191
name: "error getting pod owner",
191-
ctx: createGrpcContext(),
192+
ctx: createGrpcContext(t),
192193
request: &pb.CreateConnectionRequest{
193194
Resource: &pb.Resource{
194195
Info: &pb.Resource_ContainerInfo{
@@ -360,7 +361,7 @@ func TestSubscribe(t *testing.T) {
360361
}
361362
deployment.SetNGINXPlusActions([]*pb.NGINXPlusAction{initialAction})
362363

363-
ctx, cancel := createGrpcContextWithCancel()
364+
ctx, cancel := createGrpcContextWithCancel(t)
364365
defer cancel()
365366

366367
mockServer := newMockSubscribeServer(ctx)
@@ -501,7 +502,7 @@ func TestSubscribe_Reset(t *testing.T) {
501502
deployment.SetFiles(files, []v1.VolumeMount{})
502503
deployment.SetImageVersion("nginx:v1.0.0")
503504

504-
ctx, cancel := createGrpcContextWithCancel()
505+
ctx, cancel := createGrpcContextWithCancel(t)
505506
defer cancel()
506507

507508
mockServer := newMockSubscribeServer(ctx)
@@ -542,7 +543,7 @@ func TestSubscribe_Errors(t *testing.T) {
542543
}{
543544
{
544545
name: "context is missing data",
545-
ctx: context.Background(),
546+
ctx: t.Context(),
546547
errString: agentgrpc.ErrStatusInvalidConnection.Error(),
547548
},
548549
{
@@ -594,7 +595,7 @@ func TestSubscribe_Errors(t *testing.T) {
594595
if test.ctx != nil {
595596
ctx = test.ctx
596597
} else {
597-
ctx, cancel = createGrpcContextWithCancel()
598+
ctx, cancel = createGrpcContextWithCancel(t)
598599
defer cancel()
599600
}
600601

@@ -723,7 +724,7 @@ func TestSetInitialConfig_Errors(t *testing.T) {
723724
test.setup(msgr, deployment)
724725
}
725726

726-
err = cs.setInitialConfig(context.Background(), &grpcContext.GrpcInfo{}, deployment, conn, msgr)
727+
err = cs.setInitialConfig(t.Context(), &grpcContext.GrpcInfo{}, deployment, conn, msgr)
727728

728729
g.Expect(err).To(HaveOccurred())
729730
g.Expect(err.Error()).To(ContainSubstring(test.errString))
@@ -744,7 +745,7 @@ func TestUpdateDataPlaneStatus(t *testing.T) {
744745
}{
745746
{
746747
name: "successfully sets the status",
747-
ctx: createGrpcContext(),
748+
ctx: createGrpcContext(t),
748749
request: &pb.UpdateDataPlaneStatusRequest{
749750
Resource: &pb.Resource{
750751
Instances: []*pb.Instance{
@@ -762,7 +763,7 @@ func TestUpdateDataPlaneStatus(t *testing.T) {
762763
},
763764
{
764765
name: "successfully sets the status using plus",
765-
ctx: createGrpcContext(),
766+
ctx: createGrpcContext(t),
766767
request: &pb.UpdateDataPlaneStatusRequest{
767768
Resource: &pb.Resource{
768769
Instances: []*pb.Instance{
@@ -786,14 +787,14 @@ func TestUpdateDataPlaneStatus(t *testing.T) {
786787
},
787788
{
788789
name: "context is missing data",
789-
ctx: context.Background(),
790+
ctx: t.Context(),
790791
request: &pb.UpdateDataPlaneStatusRequest{},
791792
response: nil,
792793
errString: agentgrpc.ErrStatusInvalidConnection.Error(),
793794
},
794795
{
795796
name: "request does not contain ID",
796-
ctx: createGrpcContext(),
797+
ctx: createGrpcContext(t),
797798
request: &pb.UpdateDataPlaneStatusRequest{},
798799
response: nil,
799800
errString: "request does not contain nginx instanceID",
@@ -855,7 +856,7 @@ func TestUpdateDataPlaneHealth(t *testing.T) {
855856
nil,
856857
)
857858

858-
resp, err := cs.UpdateDataPlaneHealth(context.Background(), &pb.UpdateDataPlaneHealthRequest{})
859+
resp, err := cs.UpdateDataPlaneHealth(t.Context(), &pb.UpdateDataPlaneHealthRequest{})
859860

860861
g.Expect(err).ToNot(HaveOccurred())
861862
g.Expect(resp).To(Equal(&pb.UpdateDataPlaneHealthResponse{}))

internal/controller/nginx/agent/deployment_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package agent
22

33
import (
4-
"context"
54
"errors"
65
"testing"
76

@@ -218,13 +217,13 @@ func TestDeploymentStore(t *testing.T) {
218217

219218
nsName := types.NamespacedName{Namespace: "default", Name: "test-deployment"}
220219

221-
deployment := store.GetOrStore(context.Background(), nsName, nil)
220+
deployment := store.GetOrStore(t.Context(), nsName, nil)
222221
g.Expect(deployment).ToNot(BeNil())
223222

224223
fetchedDeployment := store.Get(nsName)
225224
g.Expect(fetchedDeployment).To(Equal(deployment))
226225

227-
deployment = store.GetOrStore(context.Background(), nsName, nil)
226+
deployment = store.GetOrStore(t.Context(), nsName, nil)
228227
g.Expect(fetchedDeployment).To(Equal(deployment))
229228

230229
store.Remove(nsName)

internal/controller/nginx/agent/grpc/context/context_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package grpcinfo_test
22

33
import (
4-
"context"
54
"testing"
65

76
. "github.com/onsi/gomega"
@@ -15,7 +14,7 @@ func TestGrpcInfoInContext(t *testing.T) {
1514

1615
grpcInfo := grpcContext.GrpcInfo{Token: "test"}
1716

18-
newCtx := grpcContext.NewGrpcContext(context.Background(), grpcInfo)
17+
newCtx := grpcContext.NewGrpcContext(t.Context(), grpcInfo)
1918
info, ok := grpcContext.FromContext(newCtx)
2019
g.Expect(ok).To(BeTrue())
2120
g.Expect(info).To(Equal(grpcInfo))
@@ -25,7 +24,7 @@ func TestGrpcInfoNotInContext(t *testing.T) {
2524
t.Parallel()
2625
g := NewWithT(t)
2726

28-
info, ok := grpcContext.FromContext(context.Background())
27+
info, ok := grpcContext.FromContext(t.Context())
2928
g.Expect(ok).To(BeFalse())
3029
g.Expect(info).To(Equal(grpcContext.GrpcInfo{}))
3130
}

internal/controller/nginx/agent/grpc/messenger/messenger_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func TestSend(t *testing.T) {
6464
t.Parallel()
6565
g := NewWithT(t)
6666

67-
ctx, cancel := context.WithCancel(context.Background())
67+
ctx, cancel := context.WithCancel(t.Context())
6868
defer cancel()
6969

7070
server := createServer()
@@ -90,7 +90,7 @@ func TestMessages(t *testing.T) {
9090
t.Parallel()
9191
g := NewWithT(t)
9292

93-
ctx, cancel := context.WithCancel(context.Background())
93+
ctx, cancel := context.WithCancel(t.Context())
9494
defer cancel()
9595

9696
server := createServer()
@@ -108,7 +108,7 @@ func TestErrors(t *testing.T) {
108108
t.Parallel()
109109
g := NewWithT(t)
110110

111-
ctx, cancel := context.WithCancel(context.Background())
111+
ctx, cancel := context.WithCancel(t.Context())
112112
defer cancel()
113113

114114
server := createErrorServer()

internal/controller/provisioner/handler_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package provisioner
22

33
import (
4-
"context"
54
"testing"
65

76
"github.com/go-logr/logr"
@@ -35,7 +34,7 @@ func TestHandleEventBatch_Upsert(t *testing.T) {
3534
handler, err := newEventHandler(store, provisioner, labelSelector, gcName)
3635
g.Expect(err).ToNot(HaveOccurred())
3736

38-
ctx := context.TODO()
37+
ctx := t.Context()
3938
logger := logr.Discard()
4039

4140
gateway := &gatewayv1.Gateway{
@@ -226,7 +225,7 @@ func TestHandleEventBatch_Delete(t *testing.T) {
226225
handler, err := newEventHandler(store, provisioner, labelSelector, gcName)
227226
g.Expect(err).ToNot(HaveOccurred())
228227

229-
ctx := context.TODO()
228+
ctx := t.Context()
230229
logger := logr.Discard()
231230

232231
// initialize resources

0 commit comments

Comments
 (0)