Skip to content

Commit

Permalink
GODRIVER-2604 Add ErrServerSelectionTimeout and `WaitQueueTimeoutEr…
Browse files Browse the repository at this point in the history
…ror` to `IsTimeout`. (#1104)
  • Loading branch information
benjirewis committed Oct 26, 2022
1 parent 0175d29 commit 39194dd
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
6 changes: 6 additions & 0 deletions mongo/errors.go
Expand Up @@ -112,6 +112,12 @@ func IsTimeout(err error) bool {
if err == driver.ErrDeadlineWouldBeExceeded {
return true
}
if err == topology.ErrServerSelectionTimeout {
return true
}
if _, ok := err.(topology.WaitQueueTimeoutError); ok {
return true
}
if ce, ok := err.(CommandError); ok && ce.IsMaxTimeMSExpiredError() {
return true
}
Expand Down
26 changes: 20 additions & 6 deletions mongo/integration/errors_test.go
Expand Up @@ -23,6 +23,8 @@ import (
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/integration/mtest"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/x/mongo/driver"
"go.mongodb.org/mongo-driver/x/mongo/driver/topology"
)

type netErr struct {
Expand Down Expand Up @@ -515,13 +517,25 @@ func TestErrors(t *testing.T) {
err error
result bool
}{
{"context timeout", mongo.CommandError{100, "", []string{"other"}, "blah", context.DeadlineExceeded, nil}, true},
{"ServerError NetworkTimeoutError", mongo.CommandError{100, "", []string{"NetworkTimeoutError"}, "blah", nil, nil}, true},
{"ServerError ExceededTimeLimitError", mongo.CommandError{100, "", []string{"ExceededTimeLimitError"}, "blah", nil, nil}, true},
{"ServerError false", mongo.CommandError{100, "", []string{"other"}, "blah", nil, nil}, false},
{"net error true", mongo.CommandError{100, "", []string{"other"}, "blah", netErr{true}, nil}, true},
{"context timeout", mongo.CommandError{
100, "", []string{"other"}, "blah", context.DeadlineExceeded, nil}, true},
{"deadline would be exceeded", mongo.CommandError{
100, "", []string{"other"}, "blah", driver.ErrDeadlineWouldBeExceeded, nil}, true},
{"server selection timeout", mongo.CommandError{
100, "", []string{"other"}, "blah", topology.ErrServerSelectionTimeout, nil}, true},
{"wait queue timeout", mongo.CommandError{
100, "", []string{"other"}, "blah", topology.WaitQueueTimeoutError{}, nil}, true},
{"ServerError NetworkTimeoutError", mongo.CommandError{
100, "", []string{"NetworkTimeoutError"}, "blah", nil, nil}, true},
{"ServerError ExceededTimeLimitError", mongo.CommandError{
100, "", []string{"ExceededTimeLimitError"}, "blah", nil, nil}, true},
{"ServerError false", mongo.CommandError{
100, "", []string{"other"}, "blah", nil, nil}, false},
{"net error true", mongo.CommandError{
100, "", []string{"other"}, "blah", netErr{true}, nil}, true},
{"net error false", netErr{false}, false},
{"wrapped error", wrappedError{mongo.CommandError{100, "", []string{"other"}, "blah", context.DeadlineExceeded, nil}}, true},
{"wrapped error", wrappedError{mongo.CommandError{
100, "", []string{"other"}, "blah", context.DeadlineExceeded, nil}}, true},
{"other error", errors.New("foo"), false},
}
for _, tc := range testCases {
Expand Down

0 comments on commit 39194dd

Please sign in to comment.