Skip to content

Commit

Permalink
Use convenience writeconcern functions in code.
Browse files Browse the repository at this point in the history
  • Loading branch information
qingyang-hu committed Oct 11, 2023
1 parent eb224b5 commit 19243b7
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 34 deletions.
25 changes: 8 additions & 17 deletions examples/documentation_examples/examples.go
Original file line number Diff line number Diff line change
Expand Up @@ -1971,10 +1971,8 @@ func WithTransactionExample(ctx context.Context) error {
defer func() { _ = client.Disconnect(ctx) }()

// Prereq: Create collections.
wcMajority := &writeconcern.WriteConcern{
W: "majority",
WTimeout: 1 * time.Second,
}
wcMajority := writeconcern.Majority()
wcMajority.WTimeout = 1 * time.Second
wcMajorityCollectionOpts := options.Collection().SetWriteConcern(wcMajority)
fooColl := client.Database("mydb1").Collection("foo", wcMajorityCollectionOpts)
barColl := client.Database("mydb1").Collection("bar", wcMajorityCollectionOpts)
Expand Down Expand Up @@ -2553,13 +2551,11 @@ func CausalConsistencyExamples(client *mongo.Client) error {

// Start Causal Consistency Example 1

rc := readconcern.Majority()
wc := writeconcern.Majority()
wc.WTimeout = 1000
// Use a causally-consistent session to run some operations
opts := options.Session().SetDefaultReadConcern(readconcern.Majority()).SetDefaultWriteConcern(
&writeconcern.WriteConcern{
W: "majority",
WTimeout: 1000,
},
)
opts := options.Session().SetDefaultReadConcern(rc).SetDefaultWriteConcern(wc)
session1, err := client.StartSession(opts)
if err != nil {
return err
Expand Down Expand Up @@ -2591,13 +2587,8 @@ func CausalConsistencyExamples(client *mongo.Client) error {
// Start Causal Consistency Example 2

// Make a new session that is causally consistent with session1 so session2 reads what session1 writes
opts = options.Session().SetDefaultReadPreference(readpref.Secondary()).SetDefaultReadConcern(
readconcern.Majority()).SetDefaultWriteConcern(
&writeconcern.WriteConcern{
W: "majority",
WTimeout: 1000,
},
)
opts = options.Session().SetDefaultReadPreference(readpref.Secondary()).
SetDefaultReadConcern(rc).SetDefaultWriteConcern(wc)
session2, err := client.StartSession(opts)
if err != nil {
return err
Expand Down
12 changes: 4 additions & 8 deletions mongo/integration/client_side_encryption_prose_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,8 @@ func TestClientSideEncryptionProse(t *testing.T) {
alteredKeydoc, _ = bsoncore.AppendDocumentEnd(alteredKeydoc, cidx)

// Insert the copied key document into keyvault.datakeys with majority write concern.
wcMajority := &writeconcern.WriteConcern{
W: "majority",
WTimeout: 1 * time.Second,
}
wcMajority := writeconcern.Majority()
wcMajority.WTimeout = 1 * time.Second
wcMajorityCollectionOpts := options.Collection().SetWriteConcern(wcMajority)
wcmColl := cse.kvClient.Database(kvDatabase).Collection(dkCollection, wcMajorityCollectionOpts)
_, err = wcmColl.InsertOne(context.Background(), alteredKeydoc)
Expand Down Expand Up @@ -1886,10 +1884,8 @@ func TestClientSideEncryptionProse(t *testing.T) {
}),
}

wcMajority := &writeconcern.WriteConcern{
W: "majority",
WTimeout: 1 * time.Second,
}
wcMajority := writeconcern.Majority()
wcMajority.WTimeout = 1 * time.Second
wcMajorityCollectionOpts := options.Collection().SetWriteConcern(wcMajority)
wcmColl := cse.kvClient.Database(kvDatabase).Collection(dkCollection, wcMajorityCollectionOpts)
_, err = wcmColl.Indexes().CreateOne(context.Background(), keyVaultIndex)
Expand Down
2 changes: 1 addition & 1 deletion mongo/integration/collection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1685,7 +1685,7 @@ func TestCollection(t *testing.T) {
assert.Equal(mt, res.UpsertedIDs[3].(string), id3, "expected UpsertedIDs[3] to be %v, got %v", id3, res.UpsertedIDs[3])
})
unackClientOpts := options.Client().
SetWriteConcern(&writeconcern.WriteConcern{W: 0})
SetWriteConcern(writeconcern.Unacknowledged())
unackMtOpts := mtest.NewOptions().
ClientOptions(unackClientOpts).
MinServerVersion("3.6")
Expand Down
4 changes: 2 additions & 2 deletions mongo/integration/index_view_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ func TestIndexView(t *testing.T) {
}
})
unackClientOpts := options.Client().
SetWriteConcern(&writeconcern.WriteConcern{W: 0})
SetWriteConcern(writeconcern.Unacknowledged())
unackMtOpts := mtest.NewOptions().
ClientOptions(unackClientOpts).
MinServerVersion("3.6")
Expand Down Expand Up @@ -344,7 +344,7 @@ func TestIndexView(t *testing.T) {
Name: indexNames[1],
})
})
wc := &writeconcern.WriteConcern{W: 1}
wc := writeconcern.W1()
wcMtOpts := mtest.NewOptions().CollectionOptions(options.Collection().SetWriteConcern(wc))
mt.RunOpts("uses writeconcern", wcMtOpts, func(mt *mtest.T) {
iv := mt.Coll.Indexes()
Expand Down
6 changes: 2 additions & 4 deletions mongo/integration/mtest/mongotest.go
Original file line number Diff line number Diff line change
Expand Up @@ -526,10 +526,8 @@ func (t *T) ClearCollections() {
// could prevent it from being dropped for sharded clusters. We can resolve this by
// re-instantiating the collection with a majority write concern before dropping.
collname := coll.created.Name()
wcm := &writeconcern.WriteConcern{
W: "majority",
WTimeout: 1 * time.Second,
}
wcm := writeconcern.Majority()
wcm.WTimeout = 1 * time.Second
wccoll := t.DB.Collection(collname, options.Collection().SetWriteConcern(wcm))
_ = wccoll.Drop(context.Background())

Expand Down
2 changes: 1 addition & 1 deletion mongo/integration/sessions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func TestSessions(t *testing.T) {
})
})

unackWcOpts := options.Collection().SetWriteConcern(&writeconcern.WriteConcern{W: 0})
unackWcOpts := options.Collection().SetWriteConcern(writeconcern.Unacknowledged())
mt.RunOpts("unacknowledged write", mtest.NewOptions().CollectionOptions(unackWcOpts), func(mt *mtest.T) {
// unacknowledged write during a session should result in an error
sess, err := mt.Client.StartSession()
Expand Down
2 changes: 1 addition & 1 deletion x/mongo/driver/operation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ func TestOperation(t *testing.T) {
noerr(t, err)

wcAck := writeconcern.Majority()
wcUnack := &writeconcern.WriteConcern{W: 0}
wcUnack := writeconcern.Unacknowledged()

descRetryable := description.Server{
WireVersion: &description.VersionRange{Min: 6, Max: 21},
Expand Down

0 comments on commit 19243b7

Please sign in to comment.