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.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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) {
  41. if mi.before != nil {
  42. mi.before(ctx, execCtx)
  43. }
  44. }
  45. // After
  46. func (mi *mockSQLInterceptor) After(ctx context.Context, execCtx *exec.ExecContext) {
  47. if mi.after != nil {
  48. mi.after(ctx, execCtx)
  49. }
  50. }
  51. type mockTxHook struct {
  52. beforeCommit func(tx *Tx)
  53. beforeRollback func(tx *Tx)
  54. }
  55. // BeforeCommit
  56. func (mi *mockTxHook) BeforeCommit(tx *Tx) {
  57. if mi.beforeCommit != nil {
  58. mi.beforeCommit(tx)
  59. }
  60. }
  61. // BeforeRollback
  62. func (mi *mockTxHook) BeforeRollback(tx *Tx) {
  63. if mi.beforeRollback != nil {
  64. mi.beforeRollback(tx)
  65. }
  66. }
  67. func baseMoclConn(mockConn *mock.MockTestDriverConn) {
  68. mockConn.EXPECT().ExecContext(gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes().Return(&driver.ResultNoRows, nil)
  69. mockConn.EXPECT().Exec(gomock.Any(), gomock.Any()).AnyTimes().Return(&driver.ResultNoRows, nil)
  70. mockConn.EXPECT().ResetSession(gomock.Any()).AnyTimes().Return(nil)
  71. mockConn.EXPECT().Close().AnyTimes().Return(nil)
  72. }
  73. func TestXAConn_ExecContext(t *testing.T) {
  74. ctrl := gomock.NewController(t)
  75. defer ctrl.Finish()
  76. mockMgr := initMockResourceManager(t, ctrl)
  77. _ = mockMgr
  78. db, err := sql.Open("seata-xa-mysql", "root:seata_go@tcp(127.0.0.1:3306)/seata_go_test?multiStatements=true")
  79. if err != nil {
  80. t.Fatal(err)
  81. }
  82. defer db.Close()
  83. _ = initMockXaConnector(t, ctrl, db, func(t *testing.T, ctrl *gomock.Controller) *mock.MockTestDriverConnector {
  84. mockTx := mock.NewMockTestDriverTx(ctrl)
  85. mockTx.EXPECT().Commit().AnyTimes().Return(nil)
  86. mockTx.EXPECT().Rollback().AnyTimes().Return(nil)
  87. mockConn := mock.NewMockTestDriverConn(ctrl)
  88. mockConn.EXPECT().Begin().AnyTimes().Return(mockTx, nil)
  89. mockConn.EXPECT().BeginTx(gomock.Any(), gomock.Any()).AnyTimes().Return(mockTx, nil)
  90. baseMoclConn(mockConn)
  91. connector := mock.NewMockTestDriverConnector(ctrl)
  92. connector.EXPECT().Connect(gomock.Any()).AnyTimes().Return(mockConn, nil)
  93. return connector
  94. })
  95. mi := &mockSQLInterceptor{}
  96. ti := &mockTxHook{}
  97. exec.CleanCommonHook()
  98. CleanTxHooks()
  99. exec.RegisCommonHook(mi)
  100. RegisterTxHook(ti)
  101. t.Run("have xid", func(t *testing.T) {
  102. ctx := tm.InitSeataContext(context.Background())
  103. tm.SetXID(ctx, uuid.New().String())
  104. t.Logf("set xid=%s", tm.GetXID(ctx))
  105. before := func(_ context.Context, execCtx *exec.ExecContext) {
  106. t.Logf("on exec xid=%s", execCtx.TxCtx.XaID)
  107. assert.Equal(t, tm.GetXID(ctx), execCtx.TxCtx.XaID)
  108. assert.Equal(t, types.XAMode, execCtx.TxCtx.TransType)
  109. }
  110. mi.before = before
  111. var comitCnt int32
  112. beforeCommit := func(tx *Tx) {
  113. atomic.AddInt32(&comitCnt, 1)
  114. assert.Equal(t, tx.ctx.TransType, types.XAMode)
  115. }
  116. ti.beforeCommit = beforeCommit
  117. conn, err := db.Conn(context.Background())
  118. assert.NoError(t, err)
  119. _, err = conn.ExecContext(ctx, "SELECT 1")
  120. assert.NoError(t, err)
  121. _, err = db.ExecContext(ctx, "SELECT 1")
  122. assert.NoError(t, err)
  123. assert.Equal(t, int32(2), atomic.LoadInt32(&comitCnt))
  124. })
  125. t.Run("not xid", func(t *testing.T) {
  126. before := func(_ context.Context, execCtx *exec.ExecContext) {
  127. assert.Equal(t, "", execCtx.TxCtx.XaID)
  128. assert.Equal(t, types.Local, execCtx.TxCtx.TransType)
  129. }
  130. mi.before = before
  131. var comitCnt int32
  132. beforeCommit := func(tx *Tx) {
  133. atomic.AddInt32(&comitCnt, 1)
  134. }
  135. ti.beforeCommit = beforeCommit
  136. conn, err := db.Conn(context.Background())
  137. assert.NoError(t, err)
  138. _, err = conn.ExecContext(context.Background(), "SELECT 1")
  139. assert.NoError(t, err)
  140. _, err = db.ExecContext(context.Background(), "SELECT 1")
  141. assert.NoError(t, err)
  142. _, err = db.Exec("SELECT 1")
  143. assert.NoError(t, err)
  144. assert.Equal(t, int32(0), atomic.LoadInt32(&comitCnt))
  145. })
  146. }