Skip to content

Commit

Permalink
Merge branch 'master' into GODRIVER-2698
Browse files Browse the repository at this point in the history
  • Loading branch information
prestonvasquez authored Dec 6, 2023
2 parents 6b87758 + 5b711e8 commit ff50428
Show file tree
Hide file tree
Showing 88 changed files with 431 additions and 317 deletions.
151 changes: 86 additions & 65 deletions .evergreen/config.yml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion .evergreen/run-mongodb-aws-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ set -x

# For Go 1.16+, Go builds requires a go.mod file in the current working directory or a parent
# directory. Spawn a new subshell, "cd" to the project directory, then run "go run".
(cd ${PROJECT_DIRECTORY} && go run "./cmd/testaws/main.go")
(cd ${PROJECT_DIRECTORY} && go run "./cmd/testaws/main.go" | tee test.suite)
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ evg-test-load-balancers:
go test $(BUILD_TAGS) ./mongo/integration -run TestChangeStreamSpec -v -timeout $(TEST_TIMEOUT)s >> test.suite
go test $(BUILD_TAGS) ./mongo/integration -run TestInitialDNSSeedlistDiscoverySpec/load_balanced -v -timeout $(TEST_TIMEOUT)s >> test.suite
go test $(BUILD_TAGS) ./mongo/integration -run TestLoadBalancerSupport -v -timeout $(TEST_TIMEOUT)s >> test.suite
go test $(BUILD_TAGS) ./mongo/integration -run TestLoadBalancedConnectionHandshake -v -timeout $(TEST_TIMEOUT)s >> test.suite
go test $(BUILD_TAGS) ./mongo/integration/unified -run TestUnifiedSpec -v -timeout $(TEST_TIMEOUT)s >> test.suite

.PHONY: evg-test-search-index
Expand Down Expand Up @@ -196,7 +197,7 @@ build-kms-test:
### Benchmark specific targets and support. ###
.PHONY: benchmark
benchmark:perf
go test $(BUILD_TAGS) -benchmem -bench=. ./benchmark
go test $(BUILD_TAGS) -benchmem -bench=. ./benchmark | test benchmark.suite

.PHONY: driver-benchmark
driver-benchmark:perf
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ import (

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
client, err := mongo.Connect(ctx, options.Client().ApplyURI("mongodb://localhost:27017"))
client, err := mongo.Connect(options.Client().ApplyURI("mongodb://localhost:27017"))
```

Make sure to defer a call to `Disconnect` after instantiating your client:
Expand Down Expand Up @@ -166,12 +166,12 @@ Compression can be enabled using the `compressors` parameter on the connection s

```go
opts := options.Client().ApplyURI("mongodb://localhost:27017/?compressors=snappy,zlib,zstd")
client, _ := mongo.Connect(context.TODO(), opts)
client, _ := mongo.Connect(opts)
```

```go
opts := options.Client().SetCompressors([]string{"snappy", "zlib", "zstd"})
client, _ := mongo.Connect(context.TODO(), opts)
client, _ := mongo.Connect(opts)
```

If compressors are set, the Go Driver negotiates with the server to select the first common compressor. For server configuration and defaults, refer to [`networkMessageCompressors`](https://www.mongodb.com/docs/manual/reference/program/mongod/#std-option-mongod.--networkMessageCompressors).
Expand Down
4 changes: 2 additions & 2 deletions benchmark/multi.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func MultiFindMany(ctx context.Context, tm TimerManager, iters int) error {
ctx, cancel := context.WithCancel(ctx)
defer cancel()

db, err := getClientDB(ctx)
db, err := getClientDB()
if err != nil {
return err
}
Expand Down Expand Up @@ -83,7 +83,7 @@ func multiInsertCase(ctx context.Context, tm TimerManager, iters int, data strin
ctx, cancel := context.WithCancel(ctx)
defer cancel()

db, err := getClientDB(ctx)
db, err := getClientDB()
if err != nil {
return err
}
Expand Down
6 changes: 3 additions & 3 deletions benchmark/operation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func BenchmarkClientWrite(b *testing.B) {
}
for _, bm := range benchmarks {
b.Run(bm.name, func(b *testing.B) {
client, err := mongo.Connect(context.Background(), bm.opt)
client, err := mongo.Connect(bm.opt)
if err != nil {
b.Fatalf("error creating client: %v", err)
}
Expand Down Expand Up @@ -71,7 +71,7 @@ func BenchmarkClientBulkWrite(b *testing.B) {
}
for _, bm := range benchmarks {
b.Run(bm.name, func(b *testing.B) {
client, err := mongo.Connect(context.Background(), bm.opt)
client, err := mongo.Connect(bm.opt)
if err != nil {
b.Fatalf("error creating client: %v", err)
}
Expand Down Expand Up @@ -115,7 +115,7 @@ func BenchmarkClientRead(b *testing.B) {
}
for _, bm := range benchmarks {
b.Run(bm.name, func(b *testing.B) {
client, err := mongo.Connect(context.Background(), bm.opt)
client, err := mongo.Connect(bm.opt)
if err != nil {
b.Fatalf("error creating client: %v", err)
}
Expand Down
10 changes: 5 additions & 5 deletions benchmark/single.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ const (
largeData = "large_doc.json"
)

func getClientDB(ctx context.Context) (*mongo.Database, error) {
func getClientDB() (*mongo.Database, error) {
cs, err := integtest.GetConnString()
if err != nil {
return nil, err
}
client, err := mongo.Connect(ctx, options.Client().ApplyURI(cs.String()))
client, err := mongo.Connect(options.Client().ApplyURI(cs.String()))
if err != nil {
return nil, err
}
Expand All @@ -42,7 +42,7 @@ func SingleRunCommand(ctx context.Context, tm TimerManager, iters int) error {
ctx, cancel := context.WithCancel(ctx)
defer cancel()

db, err := getClientDB(ctx)
db, err := getClientDB()
if err != nil {
return err
}
Expand Down Expand Up @@ -75,7 +75,7 @@ func SingleFindOneByID(ctx context.Context, tm TimerManager, iters int) error {
ctx, cancel := context.WithCancel(ctx)
defer cancel()

db, err := getClientDB(ctx)
db, err := getClientDB()
if err != nil {
return err
}
Expand Down Expand Up @@ -123,7 +123,7 @@ func singleInsertCase(ctx context.Context, tm TimerManager, iters int, data stri
ctx, cancel := context.WithCancel(ctx)
defer cancel()

db, err := getClientDB(ctx)
db, err := getClientDB()
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion bson/bsoncodec/default_value_decoders.go
Original file line number Diff line number Diff line change
Expand Up @@ -1787,7 +1787,7 @@ func (DefaultValueDecoders) decodeElemsFromDocumentReader(dc DecodeContext, dr b
elems := make([]reflect.Value, 0)
for {
key, vr, err := dr.ReadElement()
if err == bsonrw.ErrEOD {
if errors.Is(err, bsonrw.ErrEOD) {
break
}
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions bson/bsoncodec/default_value_encoders.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ func (dve DefaultValueEncoders) mapEncodeValue(ec EncodeContext, dw bsonrw.Docum
return err
}

if lookupErr == errInvalidValue {
if errors.Is(lookupErr, errInvalidValue) {
err = vw.WriteNull()
if err != nil {
return err
Expand Down Expand Up @@ -427,7 +427,7 @@ func (dve DefaultValueEncoders) ArrayEncodeValue(ec EncodeContext, vw bsonrw.Val
return err
}

if lookupErr == errInvalidValue {
if errors.Is(lookupErr, errInvalidValue) {
err = vw.WriteNull()
if err != nil {
return err
Expand Down Expand Up @@ -496,7 +496,7 @@ func (dve DefaultValueEncoders) SliceEncodeValue(ec EncodeContext, vw bsonrw.Val
return err
}

if lookupErr == errInvalidValue {
if errors.Is(lookupErr, errInvalidValue) {
err = vw.WriteNull()
if err != nil {
return err
Expand Down
5 changes: 3 additions & 2 deletions bson/bsoncodec/map_codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package bsoncodec

import (
"encoding"
"errors"
"fmt"
"reflect"
"strconv"
Expand Down Expand Up @@ -137,7 +138,7 @@ func (mc *MapCodec) mapEncodeValue(ec EncodeContext, dw bsonrw.DocumentWriter, v
return err
}

if lookupErr == errInvalidValue {
if errors.Is(lookupErr, errInvalidValue) {
err = vw.WriteNull()
if err != nil {
return err
Expand Down Expand Up @@ -200,7 +201,7 @@ func (mc *MapCodec) DecodeValue(dc DecodeContext, vr bsonrw.ValueReader, val ref

for {
key, vr, err := dr.ReadElement()
if err == bsonrw.ErrEOD {
if errors.Is(err, bsonrw.ErrEOD) {
break
}
if err != nil {
Expand Down
9 changes: 3 additions & 6 deletions bson/bsoncodec/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,9 @@ func (r *Registry) RegisterTypeMapEntry(bt bsontype.Type, rt reflect.Type) {
// If no encoder is found, an error of type ErrNoEncoder is returned. LookupEncoder is safe for
// concurrent use by multiple goroutines after all codecs and encoders are registered.
func (r *Registry) LookupEncoder(valueType reflect.Type) (ValueEncoder, error) {
if valueType == nil {
return nil, ErrNoEncoder{Type: valueType}
}
enc, found := r.lookupTypeEncoder(valueType)
if found {
if enc == nil {
Expand All @@ -400,15 +403,10 @@ func (r *Registry) LookupEncoder(valueType reflect.Type) (ValueEncoder, error) {
if found {
return r.typeEncoders.LoadOrStore(valueType, enc), nil
}
if valueType == nil {
r.storeTypeEncoder(valueType, nil)
return nil, ErrNoEncoder{Type: valueType}
}

if v, ok := r.kindEncoders.Load(valueType.Kind()); ok {
return r.storeTypeEncoder(valueType, v), nil
}
r.storeTypeEncoder(valueType, nil)
return nil, ErrNoEncoder{Type: valueType}
}

Expand Down Expand Up @@ -474,7 +472,6 @@ func (r *Registry) LookupDecoder(valueType reflect.Type) (ValueDecoder, error) {
if v, ok := r.kindDecoders.Load(valueType.Kind()); ok {
return r.storeTypeDecoder(valueType, v), nil
}
r.storeTypeDecoder(valueType, nil)
return nil, ErrNoDecoder{Type: valueType}
}

Expand Down
37 changes: 35 additions & 2 deletions bson/bsoncodec/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package bsoncodec

import (
"errors"
"reflect"
"testing"

Expand Down Expand Up @@ -351,7 +352,8 @@ func TestRegistryBuilder(t *testing.T) {
})
t.Run("Decoder", func(t *testing.T) {
wanterr := tc.wanterr
if ene, ok := tc.wanterr.(ErrNoEncoder); ok {
var ene ErrNoEncoder
if errors.As(tc.wanterr, &ene) {
wanterr = ErrNoDecoder(ene)
}

Expand Down Expand Up @@ -775,7 +777,8 @@ func TestRegistry(t *testing.T) {
t.Parallel()

wanterr := tc.wanterr
if ene, ok := tc.wanterr.(ErrNoEncoder); ok {
var ene ErrNoEncoder
if errors.As(tc.wanterr, &ene) {
wanterr = ErrNoDecoder(ene)
}

Expand All @@ -789,6 +792,36 @@ func TestRegistry(t *testing.T) {
})
})
}
t.Run("nil type", func(t *testing.T) {
t.Parallel()

t.Run("Encoder", func(t *testing.T) {
t.Parallel()

wanterr := ErrNoEncoder{Type: reflect.TypeOf(nil)}

gotcodec, goterr := reg.LookupEncoder(nil)
if !cmp.Equal(goterr, wanterr, cmp.Comparer(compareErrors)) {
t.Errorf("errors did not match: got %#v, want %#v", goterr, wanterr)
}
if !cmp.Equal(gotcodec, nil, allowunexported, cmp.Comparer(comparepc)) {
t.Errorf("codecs did not match: got %#v, want nil", gotcodec)
}
})
t.Run("Decoder", func(t *testing.T) {
t.Parallel()

wanterr := ErrNilType

gotcodec, goterr := reg.LookupDecoder(nil)
if !cmp.Equal(goterr, wanterr, cmp.Comparer(compareErrors)) {
t.Errorf("errors did not match: got %#v, want %#v", goterr, wanterr)
}
if !cmp.Equal(gotcodec, nil, allowunexported, cmp.Comparer(comparepc)) {
t.Errorf("codecs did not match: got %v: want nil", gotcodec)
}
})
})
// lookup a type whose pointer implements an interface and expect that the registered hook is
// returned
t.Run("interface implementation with hook (pointer)", func(t *testing.T) {
Expand Down
5 changes: 3 additions & 2 deletions bson/bsonrw/copier.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package bsonrw

import (
"errors"
"fmt"
"io"

Expand Down Expand Up @@ -442,7 +443,7 @@ func (c Copier) copyArray(dst ValueWriter, src ValueReader) error {

for {
vr, err := ar.ReadValue()
if err == ErrEOA {
if errors.Is(err, ErrEOA) {
break
}
if err != nil {
Expand All @@ -466,7 +467,7 @@ func (c Copier) copyArray(dst ValueWriter, src ValueReader) error {
func (c Copier) copyDocumentCore(dw DocumentWriter, dr DocumentReader) error {
for {
key, vr, err := dr.ReadElement()
if err == ErrEOD {
if errors.Is(err, ErrEOD) {
break
}
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion bson/bsonrw/extjson_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ func (ejp *extJSONParser) readValue(t bsontype.Type) (*extJSONValue, error) {
// convert hex to bytes
bytes, err := hex.DecodeString(uuidNoHyphens)
if err != nil {
return nil, fmt.Errorf("$uuid value does not follow RFC 4122 format regarding hex bytes: %v", err)
return nil, fmt.Errorf("$uuid value does not follow RFC 4122 format regarding hex bytes: %w", err)
}

ejp.advanceState()
Expand Down
3 changes: 2 additions & 1 deletion bson/bsonrw/extjson_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package bsonrw

import (
"errors"
"io"
"strings"
"testing"
Expand Down Expand Up @@ -47,7 +48,7 @@ type readKeyValueTestCase struct {

func expectSpecificError(expected error) expectedErrorFunc {
return func(t *testing.T, err error, desc string) {
if err != expected {
if !errors.Is(err, expected) {
t.Helper()
t.Errorf("%s: Expected %v but got: %v", desc, expected, err)
t.FailNow()
Expand Down
5 changes: 3 additions & 2 deletions bson/bsonrw/extjson_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package bsonrw

import (
"errors"
"fmt"
"io"
"sync"
Expand Down Expand Up @@ -613,7 +614,7 @@ func (ejvr *extJSONValueReader) ReadElement() (string, ValueReader, error) {
name, t, err := ejvr.p.readKey()

if err != nil {
if err == ErrEOD {
if errors.Is(err, ErrEOD) {
if ejvr.stack[ejvr.frame].mode == mCodeWithScope {
_, err := ejvr.p.peekType()
if err != nil {
Expand All @@ -640,7 +641,7 @@ func (ejvr *extJSONValueReader) ReadValue() (ValueReader, error) {

t, err := ejvr.p.peekType()
if err != nil {
if err == ErrEOA {
if errors.Is(err, ErrEOA) {
ejvr.pop()
}

Expand Down
Loading

0 comments on commit ff50428

Please sign in to comment.