...

Source file src/go.mongodb.org/mongo-driver/mongo/gridfs/gridfs_test.go

Documentation: go.mongodb.org/mongo-driver/mongo/gridfs

     1  // Copyright (C) MongoDB, Inc. 2017-present.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License"); you may
     4  // not use this file except in compliance with the License. You may obtain
     5  // a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
     6  
     7  package gridfs
     8  
     9  import (
    10  	"context"
    11  	"testing"
    12  
    13  	"go.mongodb.org/mongo-driver/event"
    14  	"go.mongodb.org/mongo-driver/internal/assert"
    15  	"go.mongodb.org/mongo-driver/internal/integtest"
    16  	"go.mongodb.org/mongo-driver/mongo"
    17  	"go.mongodb.org/mongo-driver/mongo/options"
    18  	"go.mongodb.org/mongo-driver/mongo/readpref"
    19  	"go.mongodb.org/mongo-driver/mongo/writeconcern"
    20  )
    21  
    22  var (
    23  	connsCheckedOut int
    24  )
    25  
    26  func TestGridFS(t *testing.T) {
    27  	if testing.Short() {
    28  		t.Skip("skipping integration test in short mode")
    29  	}
    30  
    31  	cs := integtest.ConnString(t)
    32  	poolMonitor := &event.PoolMonitor{
    33  		Event: func(evt *event.PoolEvent) {
    34  			switch evt.Type {
    35  			case event.GetSucceeded:
    36  				connsCheckedOut++
    37  			case event.ConnectionReturned:
    38  				connsCheckedOut--
    39  			}
    40  		},
    41  	}
    42  	clientOpts := options.Client().
    43  		ApplyURI(cs.Original).
    44  		SetReadPreference(readpref.Primary()).
    45  		SetWriteConcern(writeconcern.New(writeconcern.WMajority())).
    46  		SetPoolMonitor(poolMonitor).
    47  		// Connect to a single host. For sharded clusters, this will pin to a single mongos, which avoids
    48  		// non-deterministic versioning errors in the server. This has no effect for replica sets because the driver
    49  		// will discover the other hosts during SDAM checks.
    50  		SetHosts(cs.Hosts[:1])
    51  
    52  	client, err := mongo.Connect(context.Background(), clientOpts)
    53  	assert.Nil(t, err, "Connect error: %v", err)
    54  	db := client.Database("gridfs")
    55  	defer func() {
    56  		sessions := client.NumberSessionsInProgress()
    57  		conns := connsCheckedOut
    58  
    59  		_ = db.Drop(context.Background())
    60  		_ = client.Disconnect(context.Background())
    61  		assert.Equal(t, 0, sessions, "%v sessions checked out", sessions)
    62  		assert.Equal(t, 0, conns, "%v connections checked out", conns)
    63  	}()
    64  
    65  	// Unit tests showing the chunk size is set correctly on the bucket and upload stream objects.
    66  	t.Run("ChunkSize", func(t *testing.T) {
    67  		chunkSizeTests := []struct {
    68  			testName   string
    69  			bucketOpts *options.BucketOptions
    70  			uploadOpts *options.UploadOptions
    71  		}{
    72  			{"Default values", nil, nil},
    73  			{"Options provided without chunk size", options.GridFSBucket(), options.GridFSUpload()},
    74  			{"Bucket chunk size set", options.GridFSBucket().SetChunkSizeBytes(27), nil},
    75  			{"Upload stream chunk size set", nil, options.GridFSUpload().SetChunkSizeBytes(27)},
    76  			{"Bucket and upload set to different values", options.GridFSBucket().SetChunkSizeBytes(27), options.GridFSUpload().SetChunkSizeBytes(31)},
    77  		}
    78  
    79  		for _, tt := range chunkSizeTests {
    80  			t.Run(tt.testName, func(t *testing.T) {
    81  				bucket, err := NewBucket(db, tt.bucketOpts)
    82  				assert.Nil(t, err, "NewBucket error: %v", err)
    83  
    84  				us, err := bucket.OpenUploadStream("filename", tt.uploadOpts)
    85  				assert.Nil(t, err, "OpenUploadStream error: %v", err)
    86  
    87  				expectedBucketChunkSize := DefaultChunkSize
    88  				if tt.bucketOpts != nil && tt.bucketOpts.ChunkSizeBytes != nil {
    89  					expectedBucketChunkSize = *tt.bucketOpts.ChunkSizeBytes
    90  				}
    91  				assert.Equal(t, expectedBucketChunkSize, bucket.chunkSize,
    92  					"expected chunk size %v, got %v", expectedBucketChunkSize, bucket.chunkSize)
    93  
    94  				expectedUploadChunkSize := expectedBucketChunkSize
    95  				if tt.uploadOpts != nil && tt.uploadOpts.ChunkSizeBytes != nil {
    96  					expectedUploadChunkSize = *tt.uploadOpts.ChunkSizeBytes
    97  				}
    98  				assert.Equal(t, expectedUploadChunkSize, us.chunkSize,
    99  					"expected chunk size %v, got %v", expectedUploadChunkSize, us.chunkSize)
   100  			})
   101  		}
   102  	})
   103  }
   104  

View as plain text