-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make GetNewClusterTime return a new time rather than an existing time.
This simplifies the change stream’s handling of the writesOff timestamp because the writesOff timestamp is separate from any change event.
- Loading branch information
Showing
8 changed files
with
676 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,35 @@ | ||
package verifier | ||
|
||
import "context" | ||
import ( | ||
"context" | ||
|
||
func (suite *IntegrationTestSuite) TestGetClusterTime() { | ||
"go.mongodb.org/mongo-driver/bson" | ||
"go.mongodb.org/mongo-driver/bson/primitive" | ||
"go.mongodb.org/mongo-driver/mongo" | ||
) | ||
|
||
func (suite *IntegrationTestSuite) TestGetNewClusterTime() { | ||
ctx := context.Background() | ||
logger, _ := getLoggerAndWriter("stdout") | ||
|
||
sess, err := suite.srcMongoClient.StartSession() | ||
suite.Require().NoError(err) | ||
|
||
_, err = suite.srcMongoClient. | ||
Database(suite.DBNameForTest()). | ||
Collection("mycoll"). | ||
InsertOne(mongo.NewSessionContext(ctx, sess), bson.D{}) | ||
suite.Require().NoError(err) | ||
|
||
clusterTimeVal, err := sess.ClusterTime().LookupErr("$clusterTime", "clusterTime") | ||
suite.Require().NoError(err, "should extract cluster time from %+v", sess.ClusterTime()) | ||
|
||
clusterT, clusterI, ok := clusterTimeVal.TimestampOK() | ||
suite.Require().True(ok, "session cluster time (%s: %v) must be a timestamp", clusterTimeVal.Type, clusterTimeVal) | ||
|
||
ts, err := GetNewClusterTime(ctx, logger, suite.srcMongoClient) | ||
suite.Require().NoError(err) | ||
|
||
suite.Assert().NotZero(ts, "timestamp should be nonzero") | ||
suite.Require().NotZero(ts, "timestamp should be nonzero") | ||
suite.Assert().True(ts.After(primitive.Timestamp{T: clusterT, I: clusterI})) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package option | ||
|
||
import ( | ||
"github.com/pkg/errors" | ||
"go.mongodb.org/mongo-driver/bson" | ||
"go.mongodb.org/mongo-driver/bson/bsontype" | ||
"go.mongodb.org/mongo-driver/bson/primitive" | ||
) | ||
|
||
// MarshalBSONValue implements bson.ValueMarshaler. | ||
func (o Option[T]) MarshalBSONValue() (bsontype.Type, []byte, error) { | ||
val, exists := o.Get() | ||
if !exists { | ||
return bson.MarshalValue(primitive.Null{}) | ||
} | ||
|
||
return bson.MarshalValue(val) | ||
} | ||
|
||
// UnmarshalBSONValue implements bson.ValueUnmarshaler. | ||
func (o *Option[T]) UnmarshalBSONValue(bType bsontype.Type, raw []byte) error { | ||
|
||
switch bType { | ||
case bson.TypeNull: | ||
o.val = nil | ||
|
||
default: | ||
valPtr := new(T) | ||
|
||
err := bson.UnmarshalValue(bType, raw, &valPtr) | ||
if err != nil { | ||
return errors.Wrapf(err, "failed to unmarshal %T", *o) | ||
} | ||
|
||
// This may not even be possible, but we should still check. | ||
if isNil(*valPtr) { | ||
return errors.Wrapf(err, "refuse to unmarshal nil %T value", *o) | ||
} | ||
|
||
o.val = valPtr | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// IsZero implements bsoncodec.Zeroer. | ||
func (o Option[T]) IsZero() bool { | ||
return o.IsNone() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package option | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
) | ||
|
||
var _ json.Marshaler = &Option[int]{} | ||
var _ json.Unmarshaler = &Option[int]{} | ||
|
||
// MarshalJSON encodes Option into json. | ||
func (o Option[T]) MarshalJSON() ([]byte, error) { | ||
val, exists := o.Get() | ||
if exists { | ||
return json.Marshal(val) | ||
} | ||
|
||
return json.Marshal(nil) | ||
} | ||
|
||
// UnmarshalJSON decodes Option from json. | ||
func (o *Option[T]) UnmarshalJSON(b []byte) error { | ||
if bytes.Equal(b, []byte("null")) { | ||
o.val = nil | ||
} else { | ||
val := *new(T) | ||
|
||
err := json.Unmarshal(b, &val) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
o.val = &val | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.