From 23df9763dbadf865d1623f86bd32ef80a91fb7a8 Mon Sep 17 00:00:00 2001 From: Qingyang Hu Date: Thu, 28 Sep 2023 17:46:35 -0400 Subject: [PATCH] WIP --- mongo/client.go | 29 ++++++++++++----------------- mongo/client_test.go | 18 +++++++++--------- mongo/database_test.go | 2 +- mongo/integration/client_test.go | 8 -------- mongo/mongocryptd.go | 4 ++-- 5 files changed, 24 insertions(+), 37 deletions(-) diff --git a/mongo/client.go b/mongo/client.go index 5b776c7986..56641179d0 100644 --- a/mongo/client.go +++ b/mongo/client.go @@ -81,8 +81,7 @@ type Client struct { encryptedFieldsMap map[string]interface{} } -// Connect creates a new Client and then initializes it using the Connect method. This is equivalent to calling -// NewClient followed by Client.Connect. +// Connect creates a new Client and then initializes it using the Connect method. // // When creating an options.ClientOptions, the order the methods are called matters. Later Set* // methods will overwrite the values from previous Set* method invocations. This includes the @@ -104,18 +103,18 @@ type Client struct { // The Client.Ping method can be used to verify that the deployment is successfully connected and the // Client was correctly configured. func Connect(ctx context.Context, opts ...*options.ClientOptions) (*Client, error) { - c, err := NewClient(opts...) + c, err := newClient(opts...) if err != nil { return nil, err } - err = c.Connect(ctx) + err = c.connect(ctx) if err != nil { return nil, err } return c, nil } -// NewClient creates a new client to connect to a deployment specified by the uri. +// newClient creates a new client to connect to a deployment specified by the uri. // // When creating an options.ClientOptions, the order the methods are called matters. Later Set* // methods will overwrite the values from previous Set* method invocations. This includes the @@ -128,9 +127,7 @@ func Connect(ctx context.Context, opts ...*options.ClientOptions) (*Client, erro // option fields of previous options, there is no partial overwriting. For example, if Username is // set in the Auth field for the first option, and Password is set for the second but with no // Username, after the merge the Username field will be empty. -// -// Deprecated: Use [Connect] instead. -func NewClient(opts ...*options.ClientOptions) (*Client, error) { +func newClient(opts ...*options.ClientOptions) (*Client, error) { clientOpt := options.MergeClientOptions(opts...) id, err := uuid.New() @@ -235,14 +232,12 @@ func NewClient(opts ...*options.ClientOptions) (*Client, error) { return client, nil } -// Connect initializes the Client by starting background monitoring goroutines. +// connect initializes the Client by starting background monitoring goroutines. // If the Client was created using the NewClient function, this method must be called before a Client can be used. // // Connect starts background goroutines to monitor the state of the deployment and does not do any I/O in the main // goroutine. The Client.Ping method can be used to verify that the connection was created successfully. -// -// Deprecated: Use [mongo.Connect] instead. -func (c *Client) Connect(ctx context.Context) error { +func (c *Client) connect(ctx context.Context) error { if connector, ok := c.deployment.(driver.Connector); ok { err := connector.Connect() if err != nil { @@ -257,19 +252,19 @@ func (c *Client) Connect(ctx context.Context) error { } if c.internalClientFLE != nil { - if err := c.internalClientFLE.Connect(ctx); err != nil { + if err := c.internalClientFLE.connect(ctx); err != nil { return err } } if c.keyVaultClientFLE != nil && c.keyVaultClientFLE != c.internalClientFLE && c.keyVaultClientFLE != c { - if err := c.keyVaultClientFLE.Connect(ctx); err != nil { + if err := c.keyVaultClientFLE.connect(ctx); err != nil { return err } } if c.metadataClientFLE != nil && c.metadataClientFLE != c.internalClientFLE && c.metadataClientFLE != c { - if err := c.metadataClientFLE.Connect(ctx); err != nil { + if err := c.metadataClientFLE.connect(ctx); err != nil { return err } } @@ -515,7 +510,7 @@ func (c *Client) getOrCreateInternalClient(clientOpts *options.ClientOptions) (* internalClientOpts.AutoEncryptionOptions = nil internalClientOpts.SetMinPoolSize(0) var err error - c.internalClientFLE, err = NewClient(internalClientOpts) + c.internalClientFLE, err = newClient(internalClientOpts) return c.internalClientFLE, err } @@ -525,7 +520,7 @@ func (c *Client) configureKeyVaultClientFLE(clientOpts *options.ClientOptions) e aeOpts := clientOpts.AutoEncryptionOptions switch { case aeOpts.KeyVaultClientOptions != nil: - c.keyVaultClientFLE, err = NewClient(aeOpts.KeyVaultClientOptions) + c.keyVaultClientFLE, err = newClient(aeOpts.KeyVaultClientOptions) case clientOpts.MaxPoolSize != nil && *clientOpts.MaxPoolSize == 0: c.keyVaultClientFLE = c default: diff --git a/mongo/client_test.go b/mongo/client_test.go index 42ec17d929..5ba4cb4a2e 100644 --- a/mongo/client_test.go +++ b/mongo/client_test.go @@ -35,7 +35,7 @@ func setupClient(opts ...*options.ClientOptions) *Client { integtest.AddTestServerAPIVersion(clientOpts) opts = append(opts, clientOpts) } - client, _ := NewClient(opts...) + client, _ := newClient(opts...) return client } @@ -183,7 +183,7 @@ func TestClient(t *testing.T) { } for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { - _, err := NewClient(tc.opts) + _, err := newClient(tc.opts) assert.Equal(t, tc.err, err, "expected error %v, got %v", tc.err, err) }) } @@ -227,7 +227,7 @@ func TestClient(t *testing.T) { } for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { - _, err := NewClient(tc.opts) + _, err := newClient(tc.opts) assert.Equal(t, tc.err, err, "expected error %v, got %v", tc.err, err) }) } @@ -249,7 +249,7 @@ func TestClient(t *testing.T) { } for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { - client, err := NewClient(tc.opts) + client, err := newClient(tc.opts) if tc.expectErr { assert.NotNil(t, err, "expected error, got nil") return @@ -277,7 +277,7 @@ func TestClient(t *testing.T) { } for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { - client, err := NewClient(tc.opts) + client, err := newClient(tc.opts) if tc.expectErr { assert.NotNil(t, err, "expected error, got nil") return @@ -412,7 +412,7 @@ func TestClient(t *testing.T) { t.Run("success with all options", func(t *testing.T) { serverAPIOptions := getServerAPIOptions() - client, err := NewClient(options.Client().SetServerAPIOptions(serverAPIOptions)) + client, err := newClient(options.Client().SetServerAPIOptions(serverAPIOptions)) assert.Nil(t, err, "unexpected error from NewClient: %v", err) convertedAPIOptions := topology.ConvertToDriverAPIOptions(serverAPIOptions) assert.Equal(t, convertedAPIOptions, client.serverAPI, @@ -420,14 +420,14 @@ func TestClient(t *testing.T) { }) t.Run("failure with unsupported version", func(t *testing.T) { serverAPIOptions := options.ServerAPI("badVersion") - _, err := NewClient(options.Client().SetServerAPIOptions(serverAPIOptions)) + _, err := newClient(options.Client().SetServerAPIOptions(serverAPIOptions)) assert.NotNil(t, err, "expected error from NewClient, got nil") errmsg := `api version "badVersion" not supported; this driver version only supports API version "1"` assert.Equal(t, errmsg, err.Error(), "expected error %v, got %v", errmsg, err.Error()) }) t.Run("cannot modify options after client creation", func(t *testing.T) { serverAPIOptions := getServerAPIOptions() - client, err := NewClient(options.Client().SetServerAPIOptions(serverAPIOptions)) + client, err := newClient(options.Client().SetServerAPIOptions(serverAPIOptions)) assert.Nil(t, err, "unexpected error from NewClient: %v", err) expectedServerAPIOptions := getServerAPIOptions() @@ -476,7 +476,7 @@ func TestClient(t *testing.T) { extraOptions["__cryptSharedLibDisabledForTestOnly"] = true } - _, err := NewClient(options.Client(). + _, err := newClient(options.Client(). SetAutoEncryptionOptions(options.AutoEncryption(). SetKmsProviders(map[string]map[string]interface{}{ "local": {"key": make([]byte, 96)}, diff --git a/mongo/database_test.go b/mongo/database_test.go index c91322ea4d..d537dc0264 100644 --- a/mongo/database_test.go +++ b/mongo/database_test.go @@ -97,7 +97,7 @@ func TestDatabase(t *testing.T) { }) t.Run("TransientTransactionError label", func(t *testing.T) { client := setupClient(options.Client().ApplyURI("mongodb://nonexistent").SetServerSelectionTimeout(3 * time.Second)) - err := client.Connect(bgCtx) + err := client.connect(bgCtx) defer client.Disconnect(bgCtx) assert.Nil(t, err, "expected nil, got %v", err) diff --git a/mongo/integration/client_test.go b/mongo/integration/client_test.go index 038ed25d72..adbb3c211f 100644 --- a/mongo/integration/client_test.go +++ b/mongo/integration/client_test.go @@ -339,14 +339,6 @@ func TestClient(t *testing.T) { assert.Nil(mt, err, "Disconnect error: %v", err) }) }) - mt.RunOpts("watch", noClientOpts, func(mt *mtest.T) { - mt.Run("disconnected", func(mt *mtest.T) { - c, err := mongo.NewClient(options.Client().ApplyURI(mtest.ClusterURI())) - assert.Nil(mt, err, "NewClient error: %v", err) - _, err = c.Watch(context.Background(), mongo.Pipeline{}) - assert.Equal(mt, mongo.ErrClientDisconnected, err, "expected error %v, got %v", mongo.ErrClientDisconnected, err) - }) - }) mt.RunOpts("end sessions", mtest.NewOptions().MinServerVersion("3.6"), func(mt *mtest.T) { _, err := mt.Client.ListDatabases(context.Background(), bson.D{}) assert.Nil(mt, err, "ListDatabases error: %v", err) diff --git a/mongo/mongocryptd.go b/mongo/mongocryptd.go index e77a392419..18678dfc93 100644 --- a/mongo/mongocryptd.go +++ b/mongo/mongocryptd.go @@ -74,7 +74,7 @@ func newMongocryptdClient(opts *options.AutoEncryptionOptions) (*mongocryptdClie } // create client - client, err := NewClient(options.Client().ApplyURI(uri).SetServerSelectionTimeout(defaultServerSelectionTimeout)) + client, err := newClient(options.Client().ApplyURI(uri).SetServerSelectionTimeout(defaultServerSelectionTimeout)) if err != nil { return nil, err } @@ -114,7 +114,7 @@ func (mc *mongocryptdClient) markCommand(ctx context.Context, dbName string, cmd // connect connects the underlying Client instance. This must be called before performing any mark operations. func (mc *mongocryptdClient) connect(ctx context.Context) error { - return mc.client.Connect(ctx) + return mc.client.connect(ctx) } // disconnect disconnects the underlying Client instance. This should be called after all operations have completed.