Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GODRIVER-1962 unwrap errors in WithTransaction #634

Merged
merged 4 commits into from
Apr 23, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions mongo/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ func (s *sessionImpl) WithTransaction(ctx context.Context, fn func(sessCtx Sessi
timeout := time.NewTimer(withTransactionTimeout)
defer timeout.Stop()
var err error
RetryLoop:
for {
err = s.StartTransaction(opts...)
if err != nil {
Expand All @@ -193,9 +194,12 @@ func (s *sessionImpl) WithTransaction(ctx context.Context, fn func(sessCtx Sessi
default:
}

if cerr, ok := err.(CommandError); ok {
if cerr.HasErrorLabel(driver.TransientTransactionError) {
continue
currErr := err
for ; currErr != nil; currErr = unwrap(currErr) {
divjotarora marked this conversation as resolved.
Show resolved Hide resolved
divjotarora marked this conversation as resolved.
Show resolved Hide resolved
if cerr, ok := currErr.(CommandError); ok {
divjotarora marked this conversation as resolved.
Show resolved Hide resolved
if cerr.HasErrorLabel(driver.TransientTransactionError) {
continue RetryLoop
}
}
}
return res, err
Expand Down
30 changes: 30 additions & 0 deletions mongo/with_transactions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,18 @@ var (
withTxnFailedEvents []*event.CommandFailedEvent
)

type wrappedError struct {
err error
}

func (we wrappedError) Error() string {
return we.err.Error()
}

func (we wrappedError) Unwrap() error {
return we.err
}

func TestConvenientTransactions(t *testing.T) {
client := setupConvenientTransactions(t)
db := client.Database("TestConvenientTransactions")
Expand Down Expand Up @@ -381,6 +393,24 @@ func TestConvenientTransactions(t *testing.T) {
// Assert that transaction is canceled within 500ms and not 2 seconds.
assert.Soon(t, callback, 500*time.Millisecond)
})
t.Run("wrapped transient transaction error retried", func(t *testing.T) {
sess, err := client.StartSession()
assert.Nil(t, err, "StartSession error: %v", err)
defer sess.EndSession(context.Background())

returnError := true
divjotarora marked this conversation as resolved.
Show resolved Hide resolved
res, err := sess.WithTransaction(context.Background(), func(sessCtx SessionContext) (interface{}, error) {
if returnError {
returnError = false
return nil, wrappedError{CommandError{Name: "test Error", Labels: []string{driver.TransientTransactionError}}}
divjotarora marked this conversation as resolved.
Show resolved Hide resolved
}
return false, nil
})
assert.Nil(t, err, "WithTransaction error: %v", err)
resBool, ok := res.(bool)
assert.True(t, ok, "expected result type %T, got %T", false, res)
assert.False(t, resBool, "expected result false, got %v", resBool)
})
}

func setupConvenientTransactions(t *testing.T, extraClientOpts ...*options.ClientOptions) *Client {
Expand Down