Repository: googleforgames/open-saves
Pull request #377 (2022-08-24 01:41:33)

fix(blob): pass base context in NewBlobGCP


<!--  Thanks for sending a pull request!  Here are some tips for you:

1. If this is your first time, please read our contributor guidelines: https://github.com/googleforgames/open-saves/blob/main/docs/contributing.md and developer guide https://github.com/googleforgames/open-saves/blob/main/docs/development.md
2. Please label this pull request according to what type of issue you are addressing.
3. Ensure you have added or ran the appropriate tests for your PR.
-->

**What type of PR is this?**
/kind fix

**What this PR does / Why we need it**:
NewBlobGCP used a new background context but it should instead
use a context passed from a caller.

**Which issue(s) this PR fixes**:
<!--
*Automatically closes linked issue when PR is merged.
Usage: `Closes #<issue number>`, or `Closes (paste link of issue)`.
-->
Closes #

**Special notes for your reviewer**:



Patch:
diff --git a/internal/app/collector/collector.go b/internal/app/collector/collector.go
index b81ee07..aaa6c1f 100644
--- a/internal/app/collector/collector.go
+++ b/internal/app/collector/collector.go
@@ -56,7 +56,7 @@ func newCollector(ctx context.Context, cfg *Config) (*Collector, error) {
 	switch cfg.Cloud {
 	case "gcp":
 		log.Infoln("Starting Open Saves garbage collector on GCP")
-		gcs, err := blob.NewBlobGCP(cfg.Bucket)
+		gcs, err := blob.NewBlobGCP(ctx, cfg.Bucket)
 		if err != nil {
 			return nil, err
 		}
diff --git a/internal/app/server/open_saves.go b/internal/app/server/open_saves.go
index cefc447..14be1f3 100644
--- a/internal/app/server/open_saves.go
+++ b/internal/app/server/open_saves.go
@@ -67,7 +67,7 @@ func newOpenSavesServer(ctx context.Context, cfg *config.ServiceConfig) (*openSa
 	switch cfg.ServerConfig.Cloud {
 	case "gcp":
 		log.Infoln("Instantiating Open Saves server on GCP")
-		gcs, err := blob.NewBlobGCP(cfg.ServerConfig.Bucket)
+		gcs, err := blob.NewBlobGCP(ctx, cfg.ServerConfig.Bucket)
 		if err != nil {
 			return nil, err
 		}
diff --git a/internal/pkg/blob/gcs.go b/internal/pkg/blob/gcs.go
index e205a7a..e1c3438 100644
--- a/internal/pkg/blob/gcs.go
+++ b/internal/pkg/blob/gcs.go
@@ -32,8 +32,7 @@ type BlobGCP struct {
 var _ BlobStore = new(BlobGCP)
 
 // NewBlobGCP returns a new BlobGCP instance.
-func NewBlobGCP(bucketURL string) (*BlobGCP, error) {
-	ctx := context.Background()
+func NewBlobGCP(ctx context.Context, bucketURL string) (*BlobGCP, error) {
 	// blob.OpenBucket creates a *blob.Bucket from url.
 	bucket, err := blob.OpenBucket(ctx, bucketURL)
 	if err != nil {


Test patch:
diff --git a/internal/app/server/open_saves_test.go b/internal/app/server/open_saves_test.go
index 540ecff..081fdac 100644
--- a/internal/app/server/open_saves_test.go
+++ b/internal/app/server/open_saves_test.go
@@ -191,7 +191,7 @@ func cleanupBlobs(ctx context.Context, t *testing.T, storeKey, recordkey string)
 		t.Errorf("datastore.NewClient failed during cleanup: %v", err)
 		return
 	}
-	blobClient, err := blob.NewBlobGCP(testBucket)
+	blobClient, err := blob.NewBlobGCP(ctx, testBucket)
 	if err != nil {
 		t.Errorf("NewBlobGCP returned error: %v", err)
 		blobClient = nil
diff --git a/internal/pkg/blob/gcs_test.go b/internal/pkg/blob/gcs_test.go
index f9f802d..93ed609 100644
--- a/internal/pkg/blob/gcs_test.go
+++ b/internal/pkg/blob/gcs_test.go
@@ -29,10 +29,10 @@ import (
 // It provides the same interface and serves as a mock.
 const testBucket = "mem://"
 
-func getBucket(t *testing.T) *BlobGCP {
+func getBucket(ctx context.Context, t *testing.T) *BlobGCP {
 	t.Helper()
 
-	gcs, err := NewBlobGCP(testBucket)
+	gcs, err := NewBlobGCP(ctx, testBucket)
 	if err != nil {
 		t.Fatalf("Initializing bucket error: %v", err)
 	}
@@ -45,7 +45,7 @@ func TestGCS_OpenBucket(t *testing.T) {
 	t.Parallel()
 
 	// Any bucket name works as it doesn't actually send requests.
-	gcs, err := NewBlobGCP("gs://bucket")
+	gcs, err := NewBlobGCP(context.Background(), "gs://bucket")
 	assert.NotNil(t, gcs)
 	assert.NoError(t, err)
 	assert.NoError(t, gcs.Close())
@@ -55,7 +55,7 @@ func TestGCS_PutGetDelete(t *testing.T) {
 	t.Parallel()
 
 	ctx := context.Background()
-	gcs := getBucket(t)
+	gcs := getBucket(ctx, t)
 	const (
 		filePath   = "get.txt"
 		testString = "hello world"
@@ -91,7 +91,7 @@ func TestGCS_SimpleStreamTests(t *testing.T) {
 	t.Parallel()
 
 	ctx := context.Background()
-	gcs := getBucket(t)
+	gcs := getBucket(ctx, t)
 	const filePath = "simple-stream-tests.txt"
 	testBlob := []byte("Lorem ipsum dolor sit amet, consectetur adipiscing elit")