-
Notifications
You must be signed in to change notification settings - Fork 892
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GODRIVER-2579 Retry connection check-out in a loop.
- Loading branch information
1 parent
43962b8
commit 89db4e6
Showing
16 changed files
with
536 additions
and
185 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// Copyright (C) MongoDB, Inc. 2023-present. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); you may | ||
// not use this file except in compliance with the License. You may obtain | ||
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
//go:build go1.20 | ||
|
||
package errutil | ||
|
||
import "errors" | ||
|
||
// Join calls [errors.Join]. | ||
func Join(errs ...error) error { | ||
return errors.Join(errs...) | ||
} |
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,87 @@ | ||
// Copied from https://cs.opensource.google/go/go/+/refs/tags/go1.21.0:src/errors/join.go;l=15 | ||
|
||
// Copyright 2022 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
//go:build !go1.20 | ||
|
||
package errutil | ||
|
||
import "errors" | ||
|
||
// Join returns an error that wraps the given errors. Any nil error values are | ||
// discarded. Join returns nil if every value in errs is nil. The error formats | ||
// as the concatenation of the strings obtained by calling the Error method of | ||
// each element of errs, with a newline between each string. | ||
// | ||
// A non-nil error returned by Join implements the "Unwrap() error" method. | ||
func Join(errs ...error) error { | ||
n := 0 | ||
for _, err := range errs { | ||
if err != nil { | ||
n++ | ||
} | ||
} | ||
if n == 0 { | ||
return nil | ||
} | ||
e := &go119JoinError{ | ||
errs: make([]error, 0, n), | ||
} | ||
for _, err := range errs { | ||
if err != nil { | ||
e.errs = append(e.errs, err) | ||
} | ||
} | ||
return e | ||
} | ||
|
||
// go119JoinError is a Go 1.13-1.19 compatible joinable error type. Its error | ||
// message is identical to [errors.Join], but it implements "Unwrap() error" | ||
// instead of "Unwrap() []error". | ||
// | ||
// It is heavily based on the joinError from | ||
// https://cs.opensource.google/go/go/+/refs/tags/go1.21.0:src/errors/join.go | ||
type go119JoinError struct { | ||
errs []error | ||
} | ||
|
||
func (e *go119JoinError) Error() string { | ||
var b []byte | ||
for i, err := range e.errs { | ||
if i > 0 { | ||
b = append(b, '\n') | ||
} | ||
b = append(b, err.Error()...) | ||
} | ||
return string(b) | ||
} | ||
|
||
// Unwrap returns another go119JoinError with the same errors as the current | ||
// go119JoinError except the first error in the slice. Continuing to call Unwrap | ||
// on each returned error will increment through every error in the slice. The | ||
// resulting behavior when using [errors.Is] and [errors.As] is similar to an | ||
// error created using [errors.Join] in Go 1.20+. | ||
func (e *go119JoinError) Unwrap() error { | ||
if len(e.errs) == 1 { | ||
return e.errs[0] | ||
} | ||
return &go119JoinError{errs: e.errs[1:]} | ||
} | ||
|
||
// Is calls [errors.Is] with the first error in the slice. | ||
func (e *go119JoinError) Is(target error) bool { | ||
if len(e.errs) == 0 { | ||
return false | ||
} | ||
return errors.Is(e.errs[0], target) | ||
} | ||
|
||
// As calls [errors.As] with the first error in the slice. | ||
func (e *go119JoinError) As(target interface{}) bool { | ||
if len(e.errs) == 0 { | ||
return false | ||
} | ||
return errors.As(e.errs[0], target) | ||
} |
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,165 @@ | ||
// Copyright (C) MongoDB, Inc. 2023-present. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); you may | ||
// not use this file except in compliance with the License. You may obtain | ||
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
//go:build !go1.20 | ||
|
||
package errutil_test | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"testing" | ||
|
||
"go.mongodb.org/mongo-driver/internal/assert" | ||
"go.mongodb.org/mongo-driver/internal/errutil" | ||
) | ||
|
||
func TestJoinReturnsNil(t *testing.T) { | ||
t.Parallel() | ||
|
||
if err := errutil.Join(); err != nil { | ||
t.Errorf("errutil.Join() = %v, want nil", err) | ||
} | ||
if err := errutil.Join(nil); err != nil { | ||
t.Errorf("errutil.Join(nil) = %v, want nil", err) | ||
} | ||
if err := errutil.Join(nil, nil); err != nil { | ||
t.Errorf("errutil.Join(nil, nil) = %v, want nil", err) | ||
} | ||
} | ||
|
||
func TestJoin_Error(t *testing.T) { | ||
t.Parallel() | ||
|
||
err1 := errors.New("err1") | ||
err2 := errors.New("err2") | ||
|
||
tests := []struct { | ||
errs []error | ||
want string | ||
}{ | ||
{ | ||
errs: []error{err1}, | ||
want: "err1", | ||
}, | ||
{ | ||
errs: []error{err1, err2}, | ||
want: "err1\nerr2", | ||
}, | ||
{ | ||
errs: []error{err1, nil, err2}, | ||
want: "err1\nerr2", | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
test := test // Capture range variable. | ||
|
||
t.Run(fmt.Sprintf("Join(%v)", test.errs), func(t *testing.T) { | ||
t.Parallel() | ||
|
||
got := errutil.Join(test.errs...).Error() | ||
assert.Equal(t, test.want, got, "expected and actual error strings are different") | ||
}) | ||
} | ||
} | ||
|
||
func TestJoin_ErrorsIs(t *testing.T) { | ||
t.Parallel() | ||
|
||
err1 := errors.New("err1") | ||
err2 := errors.New("err2") | ||
|
||
tests := []struct { | ||
errs []error | ||
target error | ||
want bool | ||
}{ | ||
{ | ||
errs: []error{err1}, | ||
target: err1, | ||
want: true, | ||
}, | ||
{ | ||
errs: []error{err1}, | ||
target: err2, | ||
want: false, | ||
}, | ||
{ | ||
errs: []error{err1, err2}, | ||
target: err2, | ||
want: true, | ||
}, | ||
{ | ||
errs: []error{err1, nil, context.DeadlineExceeded, err2}, | ||
target: context.DeadlineExceeded, | ||
want: true, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
test := test // Capture range variable. | ||
|
||
t.Run(fmt.Sprintf("Join(%v)", test.errs), func(t *testing.T) { | ||
err := errutil.Join(test.errs...) | ||
got := errors.Is(err, test.target) | ||
assert.Equal(t, test.want, got, "expected and actual errors.Is result are different") | ||
}) | ||
} | ||
} | ||
|
||
type errType1 struct{} | ||
|
||
func (errType1) Error() string { return "" } | ||
|
||
type errType2 struct{} | ||
|
||
func (errType2) Error() string { return "" } | ||
|
||
func TestJoin_ErrorsAs(t *testing.T) { | ||
t.Parallel() | ||
|
||
err1 := errType1{} | ||
err2 := errType2{} | ||
|
||
tests := []struct { | ||
errs []error | ||
target interface{} | ||
want bool | ||
}{ | ||
{ | ||
errs: []error{err1}, | ||
target: &errType1{}, | ||
want: true, | ||
}, | ||
{ | ||
errs: []error{err1}, | ||
target: &errType2{}, | ||
want: false, | ||
}, | ||
{ | ||
errs: []error{err1, err2}, | ||
target: &errType2{}, | ||
want: true, | ||
}, | ||
{ | ||
errs: []error{err1, nil, context.DeadlineExceeded, err2}, | ||
target: &errType2{}, | ||
want: true, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
test := test // Capture range variable. | ||
|
||
t.Run(fmt.Sprintf("Join(%v)", test.errs), func(t *testing.T) { | ||
err := errutil.Join(test.errs...) | ||
got := errors.As(err, test.target) | ||
assert.Equal(t, test.want, got, "expected and actual errors.Is result are different") | ||
}) | ||
} | ||
} |
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
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
Oops, something went wrong.