github.com/ydb-platform/ydb-go-sdk/v3@v3.57.0/internal/table/transaction_test.go (about) 1 package table 2 3 import ( 4 "context" 5 "testing" 6 7 "github.com/ydb-platform/ydb-go-genproto/protos/Ydb" 8 "github.com/ydb-platform/ydb-go-genproto/protos/Ydb_Operations" 9 "github.com/ydb-platform/ydb-go-genproto/protos/Ydb_Table" 10 "google.golang.org/protobuf/proto" 11 12 "github.com/ydb-platform/ydb-go-sdk/v3/internal/xerrors" 13 "github.com/ydb-platform/ydb-go-sdk/v3/table" 14 "github.com/ydb-platform/ydb-go-sdk/v3/testutil" 15 ) 16 17 func TestTxSkipRollbackForCommitted(t *testing.T) { 18 var ( 19 begin = 0 20 commit = 0 21 rollback = 0 22 ) 23 b := StubBuilder{ 24 T: t, 25 cc: testutil.NewBalancer( 26 testutil.WithInvokeHandlers( 27 testutil.InvokeHandlers{ 28 testutil.TableBeginTransaction: func(request interface{}) (proto.Message, error) { 29 _, ok := request.(*Ydb_Table.BeginTransactionRequest) 30 if !ok { 31 t.Fatalf("cannot cast request '%T' to *Ydb_Table.BeginTransactionRequest", request) 32 } 33 begin++ 34 35 return &Ydb_Table.BeginTransactionResult{ 36 TxMeta: &Ydb_Table.TransactionMeta{ 37 Id: "", 38 }, 39 }, nil 40 }, 41 testutil.TableCommitTransaction: func(request interface{}) (proto.Message, error) { 42 _, ok := request.(*Ydb_Table.CommitTransactionRequest) 43 if !ok { 44 t.Fatalf("cannot cast request '%T' to *Ydb_Table.CommitTransactionRequest", request) 45 } 46 commit++ 47 48 return &Ydb_Table.CommitTransactionResult{}, nil 49 }, 50 testutil.TableRollbackTransaction: func(request interface{}) (proto.Message, error) { 51 _, ok := request.(*Ydb_Table.RollbackTransactionRequest) 52 if !ok { 53 t.Fatalf("cannot cast request '%T' to *Ydb_Table.RollbackTransactionRequest", request) 54 } 55 rollback++ 56 57 return &Ydb_Table.RollbackTransactionResponse{ 58 Operation: &Ydb_Operations.Operation{ 59 Ready: true, 60 Status: Ydb.StatusIds_SUCCESS, 61 }, 62 }, nil 63 }, 64 testutil.TableCreateSession: func(interface{}) (proto.Message, error) { 65 return &Ydb_Table.CreateSessionResult{ 66 SessionId: testutil.SessionID(), 67 }, nil 68 }, 69 }, 70 ), 71 ), 72 } 73 s, err := b.createSession(context.Background()) 74 if err != nil { 75 t.Fatal(err) 76 } 77 { 78 x, err := s.BeginTransaction(context.Background(), table.TxSettings()) 79 if err != nil { 80 t.Fatal(err) 81 } 82 if begin != 1 { 83 t.Fatalf("unexpected begin: %d", begin) 84 } 85 _, err = x.CommitTx(context.Background()) 86 if err != nil { 87 t.Fatal(err) 88 } 89 if commit != 1 { 90 t.Fatalf("unexpected commit: %d", begin) 91 } 92 _, _ = x.CommitTx(context.Background()) 93 if commit != 1 { 94 t.Fatalf("unexpected commit: %d", begin) 95 } 96 err = x.Rollback(context.Background()) 97 if err != nil { 98 t.Fatal(err) 99 } 100 if rollback != 0 { 101 t.Fatalf("unexpected rollback: %d", begin) 102 } 103 } 104 { 105 x, err := s.BeginTransaction(context.Background(), table.TxSettings()) 106 if err != nil { 107 t.Fatal(err) 108 } 109 if begin != 2 { 110 t.Fatalf("unexpected begin: %d", begin) 111 } 112 err = x.Rollback(context.Background()) 113 if err != nil { 114 t.Fatal(err) 115 } 116 if rollback != 1 { 117 t.Fatalf("unexpected rollback: %d", begin) 118 } 119 _, err = x.CommitTx(context.Background()) 120 if !xerrors.Is(err, errTxRollbackedEarly) { 121 t.Fatal("must be errTxRollbackedEarly") 122 } 123 if commit != 1 { 124 t.Fatalf("unexpected commit: %d", commit) 125 } 126 } 127 }