Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
bb51db3
GODRIVER-3322 Apply client-level timeout to UploadFromStreamWithID
prestonvasquez Aug 30, 2024
b8a04d9
GODRIVER-3322 Add cluster URI
prestonvasquez Aug 30, 2024
3ad5a92
GODRIVER-3322 Add cluster URI
prestonvasquez Aug 30, 2024
420420e
GODRIVER-3322 Incease timeouts
prestonvasquez Aug 30, 2024
9d9476c
Merge branch 'master' into GODRIVER-3322
prestonvasquez Aug 30, 2024
403ad7b
GODRIVER-3322 Refactor context cancelation to close
prestonvasquez Aug 31, 2024
03b0666
Merge branch 'GODRIVER-3322' of github.com:prestonvasquez/mongo-go-dr…
prestonvasquez Aug 31, 2024
411d635
GODRIVER-3322 Update time from S to MS
prestonvasquez Aug 31, 2024
5e316f9
GODRIVER-3322 Lower from 500 to 10 ms
prestonvasquez Aug 31, 2024
40f5d47
GODRIVER-3322 allow more time for client-specific tests
prestonvasquez Aug 31, 2024
401fb07
GODRIVER-3322 Update t to mt
prestonvasquez Aug 31, 2024
a444deb
GODRIVER-3322 Revert timeouts back to prose
prestonvasquez Aug 31, 2024
b879cb1
GODRIVER-3322 Revert timeouts back to prose
prestonvasquez Aug 31, 2024
24a80e5
GODRIVER-3322 Add abort test
prestonvasquez Sep 3, 2024
fe97083
GODRIVER-3322 Use large timeouts for test 6
prestonvasquez Sep 3, 2024
9b0badb
GODRIVER-3322 Use mt over t
prestonvasquez Sep 3, 2024
b397aa2
GODRIVER-3322 Clean up comments
prestonvasquez Sep 3, 2024
ee199ea
GODRIVER-3322 Use only one mongos
prestonvasquez Sep 4, 2024
632a7f7
GODRIVER-3322 Use prose-specific timeouts
prestonvasquez Sep 4, 2024
7c38b9a
GODRIVER-3322 Raise timeout limits
prestonvasquez Sep 4, 2024
1e1787b
GODRIVER-3322 increase timeout
prestonvasquez Sep 4, 2024
caa5bfd
GODRIVER-3322 increase timeout
prestonvasquez Sep 4, 2024
bf50559
Merge branch 'master' into GODRIVER-3322
prestonvasquez Sep 5, 2024
93c3e45
Merge branch 'master' into GODRIVER-3322
prestonvasquez Sep 5, 2024
bbd0a16
GODRIVER-3322 De-parallelize CSOT tests
prestonvasquez Sep 5, 2024
a3e16d8
Merge branch 'GODRIVER-3322' of github.com:prestonvasquez/mongo-go-dr…
prestonvasquez Sep 5, 2024
cf49d4a
GODRIVER-3322 Increase more timeouts
prestonvasquez Sep 5, 2024
d45177e
Merge branch 'master' into GODRIVER-3322
prestonvasquez Sep 5, 2024
a83b295
GODRIVER-3322 Make GridFS it's own test
prestonvasquez Sep 5, 2024
dd9e3e7
Merge branch 'GODRIVER-3322' of github.com:prestonvasquez/mongo-go-dr…
prestonvasquez Sep 5, 2024
5230e01
GODRIVER-3322 Clean up test comments
prestonvasquez Sep 6, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 121 additions & 0 deletions internal/integration/csot_prose_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package integration

import (
"bytes"
"context"
"strings"
"testing"
Expand Down Expand Up @@ -83,6 +84,126 @@ func TestCSOTProse(t *testing.T) {
assert.Equal(mt, started[1].CommandName,
"insert", "expected a second insert event, got %v", started[1].CommandName)
})

mt.RunOpts("6. gridfs - upload", mtest.NewOptions().MinServerVersion("4.4"), func(mt *mtest.T) {
// Drop and re-create the db.fs.files and db.fs.chunks collections.
err := mt.Client.Database("db").Collection("fs.files").Drop(context.Background())
assert.NoError(t, err, "failed to drop files")

err = mt.Client.Database("db").Collection("fs.chunks").Drop(context.Background())
assert.NoError(t, err, "failed to drop chunks")

// Set a blocking "insert" fail point.
mt.SetFailPoint(mtest.FailPoint{
ConfigureFailPoint: "failCommand",
Mode: mtest.FailPointMode{
Times: 1,
},
Data: mtest.FailPointData{
FailCommands: []string{"insert"},
BlockConnection: true,
BlockTimeMS: 15,
},
})

// Create a new MongoClient with timeoutMS=10.
cliOptions := options.Client().SetTimeout(10).ApplyURI(mtest.ClusterURI())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you mean

.SetTimeout(10 * time.Millisecond)

?

integtest.AddTestServerAPIVersion(cliOptions)

client, err := mongo.Connect(cliOptions)
assert.NoError(t, err, "failed to connect to server")

// Create a GridFS bucket that wraps the db database.
bucket := client.Database("db").GridFSBucket()

// Note that UploadFromStream accounts for the following steps:
// - Call bucket.open_upload_stream()
// - Using uploadStream, upload a single 0x12 byte
// - Call uploadStream.close() to flush the stream and insert chunks
_, err = bucket.UploadFromStream(context.Background(), "filename", bytes.NewReader([]byte{0x12}))
assert.ErrorIs(t, err, context.DeadlineExceeded)
})

const test61 = "6.1 gridfs - upload and download with non-expiring client-level timeout"
mt.RunOpts(test61, mtest.NewOptions().MinServerVersion("4.4"), func(mt *mtest.T) {
// Drop and re-create the db.fs.files and db.fs.chunks collections.
err := mt.Client.Database("db").Collection("fs.files").Drop(context.Background())
assert.NoError(t, err, "failed to drop files")

err = mt.Client.Database("db").Collection("fs.chunks").Drop(context.Background())
assert.NoError(t, err, "failed to drop chunks")

// Create a new MongoClient with timeoutMS=10.
cliOptions := options.Client().SetTimeout(500 * time.Millisecond).ApplyURI(mtest.ClusterURI())
integtest.AddTestServerAPIVersion(cliOptions)

client, err := mongo.Connect(cliOptions)
assert.NoError(t, err, "failed to connect to server")

// Create a GridFS bucket that wraps the db database.
bucket := client.Database("db").GridFSBucket()

// Upload file and ensure it uploaded correctly.
fileID, err := bucket.UploadFromStream(context.Background(), "filename", bytes.NewReader([]byte{0x12}))
assert.NoError(t, err, "failed to upload stream")

buf := bytes.Buffer{}

_, err = bucket.DownloadToStream(context.Background(), fileID, &buf)
assert.NoError(t, err, "failed to download stream")
assert.Equal(t, buf.Len(), 1)
assert.Equal(t, buf.Bytes(), []byte{0x12})
})

const test62 = "6.2 gridfs - upload with operation-level timeout"
mt.RunOpts(test62, mtest.NewOptions().MinServerVersion("4.4"), func(mt *mtest.T) {
// Drop and re-create the db.fs.files and db.fs.chunks collections.
err := mt.Client.Database("db").Collection("fs.files").Drop(context.Background())
assert.NoError(t, err, "failed to drop files")

err = mt.Client.Database("db").Collection("fs.chunks").Drop(context.Background())
assert.NoError(t, err, "failed to drop chunks")

// Set a blocking "insert" fail point.
mt.SetFailPoint(mtest.FailPoint{
ConfigureFailPoint: "failCommand",
Mode: mtest.FailPointMode{
Times: 1,
},
Data: mtest.FailPointData{
FailCommands: []string{"insert"},
BlockConnection: true,
BlockTimeMS: 15,
},
})

// Create a new MongoClient with timeoutMS=10.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will be better to update the comment to describe the code more accurately.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator

@qingyang-hu qingyang-hu Sep 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Never mind, it looks good now.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I may have missed something, but is the "timeoutMS=10" still accurate after bf50559? Where is the timeout set?

cliOptions := options.Client().SetTimeout(10 * time.Second).ApplyURI(mtest.ClusterURI())
integtest.AddTestServerAPIVersion(cliOptions)

client, err := mongo.Connect(cliOptions)
assert.NoError(t, err, "failed to connect to server")

// Create a GridFS bucket that wraps the db database.
bucket := client.Database("db").GridFSBucket()

// If the operation-level context is not respected, then the client-level
// timeout will exceed deadline.
ctx, cancel := context.WithTimeout(context.Background(), 500*time.Millisecond)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use:

.SetTimeout(10 * time.Millisecond)

?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The goal of this test is to ensure that an operation-level context is actually used. Otherwise, this test would fail.

defer cancel()

// Upload file and ensure it uploaded correctly.
fileID, err := bucket.UploadFromStream(ctx, "filename", bytes.NewReader([]byte{0x12}))
assert.NoError(t, err, "failed to upload stream")

buf := bytes.Buffer{}

_, err = bucket.DownloadToStream(ctx, fileID, &buf)
assert.NoError(t, err, "failed to download stream")
assert.Equal(t, buf.Len(), 1)
assert.Equal(t, buf.Bytes(), []byte{0x12})
})

mt.Run("8. server selection", func(mt *mtest.T) {
cliOpts := options.Client().ApplyURI("mongodb://invalid/?serverSelectionTimeoutMS=100")
mtOpts := mtest.NewOptions().ClientOptions(cliOpts).CreateCollection(false)
Expand Down
3 changes: 3 additions & 0 deletions mongo/gridfs_bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,9 @@ func (b *GridFSBucket) UploadFromStreamWithID(
source io.Reader,
opts ...options.Lister[options.GridFSUploadOptions],
) error {
ctx, cancel := csot.WithTimeout(ctx, b.db.client.timeout)
defer cancel()

us, err := b.OpenUploadStreamWithID(ctx, fileID, filename, opts...)
if err != nil {
return err
Expand Down