Skip to content

Commit 261fc87

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

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{
@@ -197,14 +197,14 @@ func TestCreateConnection(t *testing.T) {
197197
},
198198
{
199199
name: "context is missing data",
200-
ctx: context.Background(),
200+
ctx: t.Context(),
201201
request: &pb.CreateConnectionRequest{},
202202
response: nil,
203203
errString: agentgrpc.ErrStatusInvalidConnection.Error(),
204204
},
205205
{
206206
name: "error getting pod owner",
207-
ctx: createGrpcContext(),
207+
ctx: createGrpcContext(t),
208208
request: &pb.CreateConnectionRequest{
209209
Resource: &pb.Resource{
210210
Info: &pb.Resource_ContainerInfo{
@@ -376,7 +376,7 @@ func TestSubscribe(t *testing.T) {
376376
}
377377
deployment.SetNGINXPlusActions([]*pb.NGINXPlusAction{initialAction})
378378

379-
ctx, cancel := createGrpcContextWithCancel()
379+
ctx, cancel := createGrpcContextWithCancel(t)
380380
defer cancel()
381381

382382
mockServer := newMockSubscribeServer(ctx)
@@ -517,7 +517,7 @@ func TestSubscribe_Reset(t *testing.T) {
517517
deployment.SetFiles(files, []v1.VolumeMount{})
518518
deployment.SetImageVersion("nginx:v1.0.0")
519519

520-
ctx, cancel := createGrpcContextWithCancel()
520+
ctx, cancel := createGrpcContextWithCancel(t)
521521
defer cancel()
522522

523523
mockServer := newMockSubscribeServer(ctx)
@@ -558,7 +558,7 @@ func TestSubscribe_Errors(t *testing.T) {
558558
}{
559559
{
560560
name: "context is missing data",
561-
ctx: context.Background(),
561+
ctx: t.Context(),
562562
errString: agentgrpc.ErrStatusInvalidConnection.Error(),
563563
},
564564
{
@@ -610,7 +610,7 @@ func TestSubscribe_Errors(t *testing.T) {
610610
if test.ctx != nil {
611611
ctx = test.ctx
612612
} else {
613-
ctx, cancel = createGrpcContextWithCancel()
613+
ctx, cancel = createGrpcContextWithCancel(t)
614614
defer cancel()
615615
}
616616

@@ -745,7 +745,7 @@ func TestSetInitialConfig_Errors(t *testing.T) {
745745
test.setup(msgr, deployment)
746746
}
747747

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

750750
g.Expect(err).To(HaveOccurred())
751751
g.Expect(err.Error()).To(ContainSubstring(test.errString))
@@ -972,7 +972,7 @@ func TestUpdateDataPlaneStatus(t *testing.T) {
972972
}{
973973
{
974974
name: "successfully sets the status",
975-
ctx: createGrpcContext(),
975+
ctx: createGrpcContext(t),
976976
request: &pb.UpdateDataPlaneStatusRequest{
977977
Resource: &pb.Resource{
978978
Instances: []*pb.Instance{
@@ -990,7 +990,7 @@ func TestUpdateDataPlaneStatus(t *testing.T) {
990990
},
991991
{
992992
name: "successfully sets the status using plus",
993-
ctx: createGrpcContext(),
993+
ctx: createGrpcContext(t),
994994
request: &pb.UpdateDataPlaneStatusRequest{
995995
Resource: &pb.Resource{
996996
Instances: []*pb.Instance{
@@ -1014,14 +1014,14 @@ func TestUpdateDataPlaneStatus(t *testing.T) {
10141014
},
10151015
{
10161016
name: "context is missing data",
1017-
ctx: context.Background(),
1017+
ctx: t.Context(),
10181018
request: &pb.UpdateDataPlaneStatusRequest{},
10191019
response: nil,
10201020
errString: agentgrpc.ErrStatusInvalidConnection.Error(),
10211021
},
10221022
{
10231023
name: "request does not contain ID",
1024-
ctx: createGrpcContext(),
1024+
ctx: createGrpcContext(t),
10251025
request: &pb.UpdateDataPlaneStatusRequest{},
10261026
response: nil,
10271027
errString: "request does not contain nginx instanceID",
@@ -1083,7 +1083,7 @@ func TestUpdateDataPlaneHealth(t *testing.T) {
10831083
nil,
10841084
)
10851085

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

10881088
g.Expect(err).ToNot(HaveOccurred())
10891089
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)