@@ -9,7 +9,12 @@ import (
9
9
"time"
10
10
11
11
"github.com/mongodb/mongo-go-driver/core/connstring"
12
+ "github.com/mongodb/mongo-go-driver/core/readconcern"
13
+ "github.com/mongodb/mongo-go-driver/core/readpref"
14
+ "github.com/mongodb/mongo-go-driver/core/tag"
15
+ "github.com/mongodb/mongo-go-driver/core/writeconcern"
12
16
"github.com/mongodb/mongo-go-driver/internal/testutil"
17
+ "github.com/mongodb/mongo-go-driver/mongo/clientopt"
13
18
"github.com/stretchr/testify/require"
14
19
)
15
20
@@ -21,7 +26,7 @@ func TestClientOptions_simple(t *testing.T) {
21
26
}
22
27
23
28
cs := testutil .ConnString (t )
24
- client , err := NewClientWithOptions (cs .String (), ClientOpt .AppName ("foo" ))
29
+ client , err := NewClientWithOptions (cs .String (), clientopt .AppName ("foo" ))
25
30
require .NoError (t , err )
26
31
27
32
require .Equal (t , "foo" , client .connString .AppName )
@@ -37,7 +42,7 @@ func TestClientOptions_deferToConnString(t *testing.T) {
37
42
cs := testutil .ConnString (t )
38
43
uri := testutil .AddOptionsToURI (cs .String (), "appname=bar" )
39
44
40
- client , err := NewClientWithOptions (uri , ClientOpt .AppName ("foo" ))
45
+ client , err := NewClientWithOptions (uri , clientopt .AppName ("foo" ))
41
46
require .NoError (t , err )
42
47
43
48
require .Equal (t , "bar" , client .connString .AppName )
@@ -47,7 +52,7 @@ func TestClientOptions_doesNotAlterConnectionString(t *testing.T) {
47
52
t .Parallel ()
48
53
49
54
cs := connstring.ConnString {}
50
- client , err := newClient (cs , ClientOpt .AppName ("foobar" ))
55
+ client , err := newClient (cs , clientopt .AppName ("foobar" ))
51
56
require .NoError (t , err )
52
57
if cs .AppName != "" {
53
58
t .Errorf ("Creating a new Client should not alter the original connection string, but it did. got %s; want <empty>" , cs .AppName )
@@ -59,53 +64,60 @@ func TestClientOptions_doesNotAlterConnectionString(t *testing.T) {
59
64
60
65
func TestClientOptions_chainAll (t * testing.T ) {
61
66
t .Parallel ()
62
-
63
- opts := ClientOpt .
67
+ readPrefMode , err := readpref .ModeFromString ("secondary" )
68
+ require .NoError (t , err )
69
+ rp , err := readpref .New (
70
+ readPrefMode ,
71
+ readpref .WithTagSets (tag .NewTagSetsFromMaps ([]map [string ]string {{"nyc" : "1" }})... ),
72
+ readpref .WithMaxStaleness (2 * time .Second ),
73
+ )
74
+ require .NoError (t , err )
75
+ opts := clientopt .BundleClient ().
64
76
AppName ("foo" ).
65
- AuthMechanism ("MONGODB-X509" ).
66
- AuthMechanismProperties (map [string ]string {"foo" : "bar" }).
67
- AuthSource ("$external" ).
77
+ Auth (clientopt.Credential {
78
+ AuthMechanism : "MONGODB-X509" ,
79
+ AuthMechanismProperties : map [string ]string {"foo" : "bar" },
80
+ AuthSource : "$external" ,
81
+ Password : "supersecurepassword" ,
82
+ Username : "admin" ,
83
+ }).
68
84
ConnectTimeout (500 * time .Millisecond ).
69
85
HeartbeatInterval (15 * time .Second ).
70
86
Hosts ([]string {
71
87
"mongodb://localhost:27018" ,
72
88
"mongodb://localhost:27019" }).
73
- Journal (true ).
74
89
LocalThreshold (time .Second ).
75
90
MaxConnIdleTime (30 * time .Second ).
76
91
MaxConnsPerHost (150 ).
77
92
MaxIdleConnsPerHost (20 ).
78
- Password ("supersecurepassword" ).
79
- ReadConcernLevel ("majority" ).
80
- ReadPreference ("secondary" ).
81
- ReadPreferenceTagSets ([]map [string ]string {
82
- {"nyc" : "1" }}).
83
- MaxStaleness (2 * time .Second ).
93
+ ReadConcern (readconcern .New (readconcern .Level ("majority" ))).
94
+ ReadPreference (rp ).
84
95
ReplicaSet ("foo" ).
85
96
ServerSelectionTimeout (time .Second ).
86
97
Single (false ).
87
98
SocketTimeout (2 * time .Second ).
88
- SSL (true ).
89
- SSLClientCertificateKeyFile ("client.pem" ).
90
- SSLClientCertificateKeyPassword (func () string { return "password" }).
91
- SSLInsecure (false ).
92
- SSLCaFile ("ca.pem" ).
93
- WString ("majority" ).
94
- WNumber (3 ).
95
- Username ("admin" ).
96
- WTimeout (2 * time .Second )
97
-
98
- client := new (Client )
99
- for opts .opt != nil {
100
- err := opts .opt (client )
101
- require .NoError (t , err )
102
- opts = opts .next
103
- }
99
+ SSL (& clientopt.SSLOpt {
100
+ Enabled : true ,
101
+ ClientCertificateKeyFile : "client.pem" ,
102
+ ClientCertificateKeyPassword : func () string { return "password" },
103
+ Insecure : false ,
104
+ CaFile : "ca.pem" ,
105
+ }).
106
+ WriteConcern (writeconcern .New (
107
+ writeconcern .J (true ),
108
+ writeconcern .WTagSet ("majority" ),
109
+ writeconcern .W (3 ),
110
+ writeconcern .WTimeout (2 * time .Second ),
111
+ ))
112
+
113
+ client , err := opts .Unbundle (connstring.ConnString {})
114
+ require .NoError (t , err )
115
+ require .NotNil (t , client )
104
116
}
105
117
106
118
func TestClientOptions_CustomDialer (t * testing.T ) {
107
119
td := & testDialer {d : & net.Dialer {}}
108
- opts := ClientOpt .Dialer (td )
120
+ opts := clientopt .Dialer (td )
109
121
client , err := newClient (testutil .ConnString (t ), opts )
110
122
require .NoError (t , err )
111
123
err = client .Connect (context .Background ())
0 commit comments