Skip to content

Commit 8e94db4

Browse files
committed
test: replace context.TODO and context.Background to t.Context in unittests
Signed-off-by: richie <mychenforeverl@gmail.com>
1 parent f612e19 commit 8e94db4

File tree

17 files changed

+123
-131
lines changed

17 files changed

+123
-131
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: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,14 @@ func createFakeK8sClient(initObjs ...runtime.Object) (client.Client, error) {
7171
return fakeClient, nil
7272
}
7373

74-
func createGrpcContext() context.Context {
75-
return grpcContext.NewGrpcContext(context.Background(), grpcContext.GrpcInfo{
74+
func createGrpcContext(t *testing.T) context.Context {
75+
return grpcContext.NewGrpcContext(t.Context(), grpcContext.GrpcInfo{
7676
IPAddress: "127.0.0.1",
7777
})
7878
}
7979

80-
func createGrpcContextWithCancel() (context.Context, context.CancelFunc) {
81-
ctx, cancel := context.WithCancel(context.Background())
80+
func createGrpcContextWithCancel(t *testing.T) (context.Context, context.CancelFunc) {
81+
ctx, cancel := context.WithCancel(t.Context())
8282

8383
return grpcContext.NewGrpcContext(ctx, grpcContext.GrpcInfo{
8484
IPAddress: "127.0.0.1",
@@ -139,7 +139,7 @@ func TestCreateConnection(t *testing.T) {
139139
}{
140140
{
141141
name: "successfully tracks a connection",
142-
ctx: createGrpcContext(),
142+
ctx: createGrpcContext(t),
143143
request: &pb.CreateConnectionRequest{
144144
Resource: &pb.Resource{
145145
Info: &pb.Resource_ContainerInfo{
@@ -171,14 +171,14 @@ func TestCreateConnection(t *testing.T) {
171171
},
172172
{
173173
name: "context is missing data",
174-
ctx: context.Background(),
174+
ctx: t.Context(),
175175
request: &pb.CreateConnectionRequest{},
176176
response: nil,
177177
errString: agentgrpc.ErrStatusInvalidConnection.Error(),
178178
},
179179
{
180180
name: "error getting pod owner",
181-
ctx: createGrpcContext(),
181+
ctx: createGrpcContext(t),
182182
request: &pb.CreateConnectionRequest{
183183
Resource: &pb.Resource{
184184
Info: &pb.Resource_ContainerInfo{
@@ -350,7 +350,7 @@ func TestSubscribe(t *testing.T) {
350350
}
351351
deployment.SetNGINXPlusActions([]*pb.NGINXPlusAction{initialAction})
352352

353-
ctx, cancel := createGrpcContextWithCancel()
353+
ctx, cancel := createGrpcContextWithCancel(t)
354354
defer cancel()
355355

356356
mockServer := newMockSubscribeServer(ctx)
@@ -491,7 +491,7 @@ func TestSubscribe_Reset(t *testing.T) {
491491
deployment.SetFiles(files, []v1.VolumeMount{})
492492
deployment.SetImageVersion("nginx:v1.0.0")
493493

494-
ctx, cancel := createGrpcContextWithCancel()
494+
ctx, cancel := createGrpcContextWithCancel(t)
495495
defer cancel()
496496

497497
mockServer := newMockSubscribeServer(ctx)
@@ -532,7 +532,7 @@ func TestSubscribe_Errors(t *testing.T) {
532532
}{
533533
{
534534
name: "context is missing data",
535-
ctx: context.Background(),
535+
ctx: t.Context(),
536536
errString: agentgrpc.ErrStatusInvalidConnection.Error(),
537537
},
538538
{
@@ -584,7 +584,7 @@ func TestSubscribe_Errors(t *testing.T) {
584584
if test.ctx != nil {
585585
ctx = test.ctx
586586
} else {
587-
ctx, cancel = createGrpcContextWithCancel()
587+
ctx, cancel = createGrpcContextWithCancel(t)
588588
defer cancel()
589589
}
590590

@@ -719,7 +719,7 @@ func TestSetInitialConfig_Errors(t *testing.T) {
719719
test.setup(msgr, deployment)
720720
}
721721

722-
err = cs.setInitialConfig(context.Background(), deployment, conn, msgr)
722+
err = cs.setInitialConfig(t.Context(), deployment, conn, msgr)
723723

724724
g.Expect(err).To(HaveOccurred())
725725
g.Expect(err.Error()).To(ContainSubstring(test.errString))
@@ -946,7 +946,7 @@ func TestUpdateDataPlaneStatus(t *testing.T) {
946946
}{
947947
{
948948
name: "successfully sets the status",
949-
ctx: createGrpcContext(),
949+
ctx: createGrpcContext(t),
950950
request: &pb.UpdateDataPlaneStatusRequest{
951951
Resource: &pb.Resource{
952952
Instances: []*pb.Instance{
@@ -964,7 +964,7 @@ func TestUpdateDataPlaneStatus(t *testing.T) {
964964
},
965965
{
966966
name: "successfully sets the status using plus",
967-
ctx: createGrpcContext(),
967+
ctx: createGrpcContext(t),
968968
request: &pb.UpdateDataPlaneStatusRequest{
969969
Resource: &pb.Resource{
970970
Instances: []*pb.Instance{
@@ -988,14 +988,14 @@ func TestUpdateDataPlaneStatus(t *testing.T) {
988988
},
989989
{
990990
name: "context is missing data",
991-
ctx: context.Background(),
991+
ctx: t.Context(),
992992
request: &pb.UpdateDataPlaneStatusRequest{},
993993
response: nil,
994994
errString: agentgrpc.ErrStatusInvalidConnection.Error(),
995995
},
996996
{
997997
name: "request does not contain ID",
998-
ctx: createGrpcContext(),
998+
ctx: createGrpcContext(t),
999999
request: &pb.UpdateDataPlaneStatusRequest{},
10001000
response: nil,
10011001
errString: "request does not contain nginx instanceID",
@@ -1057,7 +1057,7 @@ func TestUpdateDataPlaneHealth(t *testing.T) {
10571057
nil,
10581058
)
10591059

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

10621062
g.Expect(err).ToNot(HaveOccurred())
10631063
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 context_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{IPAddress: "192.168.1.1"}
1716

18-
newCtx := grpcContext.NewGrpcContext(context.Background(), grpcInfo)
17+
newCtx := grpcContext.NewGrpcContext(t.Context(), grpcInfo)
1918
info, ok := grpcContext.GrpcInfoFromContext(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.GrpcInfoFromContext(context.Background())
27+
info, ok := grpcContext.GrpcInfoFromContext(t.Context())
2928
g.Expect(ok).To(BeFalse())
3029
g.Expect(info).To(Equal(grpcContext.GrpcInfo{}))
3130
}

internal/controller/nginx/agent/grpc/interceptor/interceptor_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,9 +234,9 @@ func TestInterceptor(t *testing.T) {
234234

235235
ctx := t.Context()
236236
if test.md != nil {
237-
peerCtx := context.Background()
237+
peerCtx := t.Context()
238238
if test.peer != nil {
239-
peerCtx = peer.NewContext(context.Background(), test.peer)
239+
peerCtx = peer.NewContext(t.Context(), test.peer)
240240
}
241241
ctx = metadata.NewIncomingContext(peerCtx, test.md)
242242
}

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)