Skip to content

Commit

Permalink
*: replace test usages of context.TODO() with context.Background()
Browse files Browse the repository at this point in the history
This commit replaces all usages of context.TODO() with context.Background()
in tests, and adds a lint rule ensuring context.TODO() does not appear.
Tests should always be initiating a new context if they are calling a
function for the first time - it is now "unknown" and hence not a "TODO"
application.

Release note: None
  • Loading branch information
otan committed May 22, 2020
1 parent 273f90c commit f54afe7
Show file tree
Hide file tree
Showing 221 changed files with 1,448 additions and 1,416 deletions.
2 changes: 1 addition & 1 deletion pkg/base/node_id_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestNodeIDContainer(t *testing.T) {
}

for i := 0; i < 2; i++ {
n.Set(context.TODO(), 5)
n.Set(context.Background(), 5)
if val := n.Get(); val != 5 {
t.Errorf("value should be 5, not %d", val)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/bench/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -884,7 +884,7 @@ CREATE TABLE bench.insert_distinct (
fmt.Fprintf(&buf, "(%d, %d)", zipf.Uint64(), n)
}

if _, err := db.DB.ExecContext(context.TODO(), buf.String()); err != nil {
if _, err := db.DB.ExecContext(context.Background(), buf.String()); err != nil {
return err
}
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/bench/pgbench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func BenchmarkPgbenchExec(b *testing.B) {
defer log.Scope(b).Close(b)
b.Run("Cockroach", func(b *testing.B) {
s, _, _ := serverutils.StartServer(b, base.TestServerArgs{Insecure: true})
defer s.Stopper().Stop(context.TODO())
defer s.Stopper().Stop(context.Background())

pgURL, cleanupFn := sqlutils.PGUrl(
b, s.ServingSQLAddr(), "benchmarkCockroach", url.User(security.RootUser))
Expand Down
8 changes: 4 additions & 4 deletions pkg/blobs/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func BenchmarkStreamingReadFile(b *testing.B) {
rpcContext.TestingAllowNamedRPCToAnonymousServer = true

factory := setUpService(b, rpcContext, localNodeID, remoteNodeID, localExternalDir, remoteExternalDir)
blobClient, err := factory(context.TODO(), remoteNodeID)
blobClient, err := factory(context.Background(), remoteNodeID)
if err != nil {
b.Fatal(err)
}
Expand All @@ -84,7 +84,7 @@ func benchmarkStreamingReadFile(b *testing.B, tc *benchmarkTestCase) {
b.ResetTimer()
b.SetBytes(tc.fileSize)
for i := 0; i < b.N; i++ {
reader, err := tc.blobClient.ReadFile(context.TODO(), tc.fileName)
reader, err := tc.blobClient.ReadFile(context.Background(), tc.fileName)
if err != nil {
b.Fatal(err)
}
Expand Down Expand Up @@ -113,7 +113,7 @@ func BenchmarkStreamingWriteFile(b *testing.B) {
rpcContext.TestingAllowNamedRPCToAnonymousServer = true

factory := setUpService(b, rpcContext, localNodeID, remoteNodeID, localExternalDir, remoteExternalDir)
blobClient, err := factory(context.TODO(), remoteNodeID)
blobClient, err := factory(context.Background(), remoteNodeID)
if err != nil {
b.Fatal(err)
}
Expand All @@ -134,7 +134,7 @@ func benchmarkStreamingWriteFile(b *testing.B, tc *benchmarkTestCase) {
b.ResetTimer()
b.SetBytes(tc.fileSize)
for i := 0; i < b.N; i++ {
err := tc.blobClient.WriteFile(context.TODO(), tc.fileName, bytes.NewReader(content))
err := tc.blobClient.WriteFile(context.Background(), tc.fileName, bytes.NewReader(content))
if err != nil {
b.Fatal(err)
}
Expand Down
12 changes: 6 additions & 6 deletions pkg/blobs/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func createTestResources(t testing.TB) (string, string, *stop.Stopper, func()) {
return localExternalDir, remoteExternalDir, stopper, func() {
cleanupFn()
cleanupFn2()
stopper.Stop(context.TODO())
stopper.Stop(context.Background())
leaktest.AfterTest(t)()
}
}
Expand Down Expand Up @@ -173,7 +173,7 @@ func TestBlobClientReadFile(t *testing.T) {
},
} {
t.Run(tc.name, func(t *testing.T) {
ctx := context.TODO()
ctx := context.Background()
blobClient, err := blobClientFactory(ctx, tc.nodeID)
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -244,7 +244,7 @@ func TestBlobClientWriteFile(t *testing.T) {
},
} {
t.Run(tc.name, func(t *testing.T) {
ctx := context.TODO()
ctx := context.Background()
blobClient, err := blobClientFactory(ctx, tc.nodeID)
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -359,7 +359,7 @@ func TestBlobClientList(t *testing.T) {
},
} {
t.Run(tc.name, func(t *testing.T) {
ctx := context.TODO()
ctx := context.Background()
blobClient, err := blobClientFactory(ctx, tc.nodeID)
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -441,7 +441,7 @@ func TestBlobClientDeleteFrom(t *testing.T) {
},
} {
t.Run(tc.name, func(t *testing.T) {
ctx := context.TODO()
ctx := context.Background()
blobClient, err := blobClientFactory(ctx, tc.nodeID)
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -517,7 +517,7 @@ func TestBlobClientStat(t *testing.T) {
},
} {
t.Run(tc.name, func(t *testing.T) {
ctx := context.TODO()
ctx := context.Background()
blobClient, err := blobClientFactory(ctx, tc.nodeID)
if err != nil {
t.Fatal(err)
Expand Down
6 changes: 3 additions & 3 deletions pkg/blobs/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestBlobServiceList(t *testing.T) {
if err != nil {
t.Fatal(err)
}
ctx := context.TODO()
ctx := context.Background()

t.Run("list-correct-files", func(t *testing.T) {
resp, err := service.List(ctx, &blobspb.GlobRequest{
Expand Down Expand Up @@ -78,7 +78,7 @@ func TestBlobServiceDelete(t *testing.T) {
if err != nil {
t.Fatal(err)
}
ctx := context.TODO()
ctx := context.Background()

t.Run("delete-correct-file", func(t *testing.T) {
_, err := service.Delete(ctx, &blobspb.DeleteRequest{
Expand Down Expand Up @@ -127,7 +127,7 @@ func TestBlobServiceStat(t *testing.T) {
if err != nil {
t.Fatal(err)
}
ctx := context.TODO()
ctx := context.Background()

t.Run("get-correct-file-size", func(t *testing.T) {
resp, err := service.Stat(ctx, &blobspb.StatRequest{
Expand Down
Loading

0 comments on commit f54afe7

Please sign in to comment.