|
35 | 35 | withTxnFailedEvents []*event.CommandFailedEvent
|
36 | 36 | )
|
37 | 37 |
|
| 38 | +type wrappedError struct { |
| 39 | + err error |
| 40 | +} |
| 41 | + |
| 42 | +func (we wrappedError) Error() string { |
| 43 | + return we.err.Error() |
| 44 | +} |
| 45 | + |
| 46 | +func (we wrappedError) Unwrap() error { |
| 47 | + return we.err |
| 48 | +} |
| 49 | + |
38 | 50 | func TestConvenientTransactions(t *testing.T) {
|
39 | 51 | client := setupConvenientTransactions(t)
|
40 | 52 | db := client.Database("TestConvenientTransactions")
|
@@ -381,6 +393,30 @@ func TestConvenientTransactions(t *testing.T) {
|
381 | 393 | // Assert that transaction is canceled within 500ms and not 2 seconds.
|
382 | 394 | assert.Soon(t, callback, 500*time.Millisecond)
|
383 | 395 | })
|
| 396 | + t.Run("wrapped transient transaction error retried", func(t *testing.T) { |
| 397 | + sess, err := client.StartSession() |
| 398 | + assert.Nil(t, err, "StartSession error: %v", err) |
| 399 | + defer sess.EndSession(context.Background()) |
| 400 | + |
| 401 | + // returnError tracks whether or not the callback is being retried |
| 402 | + returnError := true |
| 403 | + res, err := sess.WithTransaction(context.Background(), func(sessCtx SessionContext) (interface{}, error) { |
| 404 | + if returnError { |
| 405 | + returnError = false |
| 406 | + return nil, wrappedError{ |
| 407 | + CommandError{ |
| 408 | + Name: "test Error", |
| 409 | + Labels: []string{driver.TransientTransactionError}, |
| 410 | + }, |
| 411 | + } |
| 412 | + } |
| 413 | + return false, nil |
| 414 | + }) |
| 415 | + assert.Nil(t, err, "WithTransaction error: %v", err) |
| 416 | + resBool, ok := res.(bool) |
| 417 | + assert.True(t, ok, "expected result type %T, got %T", false, res) |
| 418 | + assert.False(t, resBool, "expected result false, got %v", resBool) |
| 419 | + }) |
384 | 420 | }
|
385 | 421 |
|
386 | 422 | func setupConvenientTransactions(t *testing.T, extraClientOpts ...*options.ClientOptions) *Client {
|
|
0 commit comments