You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

conn_xa_test.go 5.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package sql
  18. import (
  19. "context"
  20. "database/sql"
  21. "database/sql/driver"
  22. "sync/atomic"
  23. "testing"
  24. "github.com/golang/mock/gomock"
  25. "github.com/google/uuid"
  26. "github.com/seata/seata-go/pkg/datasource/sql/exec"
  27. "github.com/seata/seata-go/pkg/datasource/sql/mock"
  28. "github.com/seata/seata-go/pkg/datasource/sql/types"
  29. "github.com/seata/seata-go/pkg/tm"
  30. "github.com/stretchr/testify/assert"
  31. )
  32. type mockSQLInterceptor struct {
  33. before func(ctx context.Context, execCtx *exec.ExecContext)
  34. after func(ctx context.Context, execCtx *exec.ExecContext)
  35. }
  36. func (mi *mockSQLInterceptor) Type() types.SQLType {
  37. return types.SQLTypeUnknown
  38. }
  39. // Before
  40. func (mi *mockSQLInterceptor) Before(ctx context.Context, execCtx *exec.ExecContext) error {
  41. if mi.before != nil {
  42. mi.before(ctx, execCtx)
  43. }
  44. return nil
  45. }
  46. // After
  47. func (mi *mockSQLInterceptor) After(ctx context.Context, execCtx *exec.ExecContext) error {
  48. if mi.after != nil {
  49. mi.after(ctx, execCtx)
  50. }
  51. return nil
  52. }
  53. type mockTxHook struct {
  54. beforeCommit func(tx *Tx)
  55. beforeRollback func(tx *Tx)
  56. }
  57. // BeforeCommit
  58. func (mi *mockTxHook) BeforeCommit(tx *Tx) {
  59. if mi.beforeCommit != nil {
  60. mi.beforeCommit(tx)
  61. }
  62. }
  63. // BeforeRollback
  64. func (mi *mockTxHook) BeforeRollback(tx *Tx) {
  65. if mi.beforeRollback != nil {
  66. mi.beforeRollback(tx)
  67. }
  68. }
  69. func baseMoclConn(mockConn *mock.MockTestDriverConn) {
  70. mockConn.EXPECT().ExecContext(gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes().Return(&driver.ResultNoRows, nil)
  71. mockConn.EXPECT().Exec(gomock.Any(), gomock.Any()).AnyTimes().Return(&driver.ResultNoRows, nil)
  72. mockConn.EXPECT().ResetSession(gomock.Any()).AnyTimes().Return(nil)
  73. mockConn.EXPECT().Close().AnyTimes().Return(nil)
  74. }
  75. func TestXAConn_ExecContext(t *testing.T) {
  76. ctrl := gomock.NewController(t)
  77. defer ctrl.Finish()
  78. mockMgr := initMockResourceManager(t, ctrl)
  79. _ = mockMgr
  80. db, err := sql.Open("seata-xa-mysql", "root:seata_go@tcp(127.0.0.1:3306)/seata_go_test?multiStatements=true")
  81. if err != nil {
  82. t.Fatal(err)
  83. }
  84. defer db.Close()
  85. _ = initMockXaConnector(t, ctrl, db, func(t *testing.T, ctrl *gomock.Controller) *mock.MockTestDriverConnector {
  86. mockTx := mock.NewMockTestDriverTx(ctrl)
  87. mockTx.EXPECT().Commit().AnyTimes().Return(nil)
  88. mockTx.EXPECT().Rollback().AnyTimes().Return(nil)
  89. mockConn := mock.NewMockTestDriverConn(ctrl)
  90. mockConn.EXPECT().Begin().AnyTimes().Return(mockTx, nil)
  91. mockConn.EXPECT().BeginTx(gomock.Any(), gomock.Any()).AnyTimes().Return(mockTx, nil)
  92. baseMoclConn(mockConn)
  93. connector := mock.NewMockTestDriverConnector(ctrl)
  94. connector.EXPECT().Connect(gomock.Any()).AnyTimes().Return(mockConn, nil)
  95. return connector
  96. })
  97. mi := &mockSQLInterceptor{}
  98. ti := &mockTxHook{}
  99. exec.CleanCommonHook()
  100. CleanTxHooks()
  101. exec.RegisCommonHook(mi)
  102. RegisterTxHook(ti)
  103. t.Run("have xid", func(t *testing.T) {
  104. ctx := tm.InitSeataContext(context.Background())
  105. tm.SetXID(ctx, uuid.New().String())
  106. t.Logf("set xid=%s", tm.GetXID(ctx))
  107. before := func(_ context.Context, execCtx *exec.ExecContext) {
  108. t.Logf("on exec xid=%s", execCtx.TxCtx.XaID)
  109. assert.Equal(t, tm.GetXID(ctx), execCtx.TxCtx.XaID)
  110. assert.Equal(t, types.XAMode, execCtx.TxCtx.TransType)
  111. }
  112. mi.before = before
  113. var comitCnt int32
  114. beforeCommit := func(tx *Tx) {
  115. atomic.AddInt32(&comitCnt, 1)
  116. assert.Equal(t, tx.ctx.TransType, types.XAMode)
  117. }
  118. ti.beforeCommit = beforeCommit
  119. conn, err := db.Conn(context.Background())
  120. assert.NoError(t, err)
  121. _, err = conn.ExecContext(ctx, "SELECT 1")
  122. assert.NoError(t, err)
  123. _, err = db.ExecContext(ctx, "SELECT 1")
  124. assert.NoError(t, err)
  125. assert.Equal(t, int32(2), atomic.LoadInt32(&comitCnt))
  126. })
  127. t.Run("not xid", func(t *testing.T) {
  128. before := func(_ context.Context, execCtx *exec.ExecContext) {
  129. assert.Equal(t, "", execCtx.TxCtx.XaID)
  130. assert.Equal(t, types.Local, execCtx.TxCtx.TransType)
  131. }
  132. mi.before = before
  133. var comitCnt int32
  134. beforeCommit := func(tx *Tx) {
  135. atomic.AddInt32(&comitCnt, 1)
  136. }
  137. ti.beforeCommit = beforeCommit
  138. conn, err := db.Conn(context.Background())
  139. assert.NoError(t, err)
  140. _, err = conn.ExecContext(context.Background(), "SELECT 1")
  141. assert.NoError(t, err)
  142. _, err = db.ExecContext(context.Background(), "SELECT 1")
  143. assert.NoError(t, err)
  144. _, err = db.Exec("SELECT 1")
  145. assert.NoError(t, err)
  146. assert.Equal(t, int32(0), atomic.LoadInt32(&comitCnt))
  147. })
  148. }