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 8.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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. "io"
  23. "sync/atomic"
  24. "testing"
  25. "time"
  26. "github.com/bluele/gcache"
  27. "github.com/golang/mock/gomock"
  28. "github.com/google/uuid"
  29. "github.com/stretchr/testify/assert"
  30. "seata.apache.org/seata-go/pkg/datasource/sql/exec"
  31. "seata.apache.org/seata-go/pkg/datasource/sql/mock"
  32. "seata.apache.org/seata-go/pkg/datasource/sql/types"
  33. "seata.apache.org/seata-go/pkg/protocol/branch"
  34. "seata.apache.org/seata-go/pkg/tm"
  35. )
  36. type mysqlMockRows struct {
  37. idx int
  38. data [][]interface{}
  39. }
  40. func (m *mysqlMockRows) Columns() []string {
  41. //TODO implement me
  42. panic("implement me")
  43. }
  44. func (m *mysqlMockRows) Close() error {
  45. //TODO implement me
  46. panic("implement me")
  47. }
  48. func (m *mysqlMockRows) Next(dest []driver.Value) error {
  49. if m.idx == len(m.data) {
  50. return io.EOF
  51. }
  52. min := func(a, b int) int {
  53. if a < b {
  54. return a
  55. }
  56. return b
  57. }
  58. cnt := min(len(m.data[0]), len(dest))
  59. for i := 0; i < cnt; i++ {
  60. dest[i] = m.data[m.idx][i]
  61. }
  62. m.idx++
  63. return nil
  64. }
  65. type mockSQLInterceptor struct {
  66. before func(ctx context.Context, execCtx *types.ExecContext)
  67. after func(ctx context.Context, execCtx *types.ExecContext)
  68. }
  69. func (mi *mockSQLInterceptor) Type() types.SQLType {
  70. return types.SQLTypeUnknown
  71. }
  72. // Before
  73. func (mi *mockSQLInterceptor) Before(ctx context.Context, execCtx *types.ExecContext) error {
  74. if mi.before != nil {
  75. mi.before(ctx, execCtx)
  76. }
  77. return nil
  78. }
  79. // After
  80. func (mi *mockSQLInterceptor) After(ctx context.Context, execCtx *types.ExecContext) error {
  81. if mi.after != nil {
  82. mi.after(ctx, execCtx)
  83. }
  84. return nil
  85. }
  86. type mockTxHook struct {
  87. beforeCommit func(tx *Tx)
  88. beforeRollback func(tx *Tx)
  89. }
  90. // BeforeCommit
  91. func (mi *mockTxHook) BeforeCommit(tx *Tx) {
  92. if mi.beforeCommit != nil {
  93. mi.beforeCommit(tx)
  94. }
  95. }
  96. // BeforeRollback
  97. func (mi *mockTxHook) BeforeRollback(tx *Tx) {
  98. if mi.beforeRollback != nil {
  99. mi.beforeRollback(tx)
  100. }
  101. }
  102. func baseMockConn(mockConn *mock.MockTestDriverConn) {
  103. branchStatusCache = gcache.New(1024).LRU().Expiration(time.Minute * 10).Build()
  104. mockConn.EXPECT().ExecContext(gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes().Return(&driver.ResultNoRows, nil)
  105. mockConn.EXPECT().Exec(gomock.Any(), gomock.Any()).AnyTimes().Return(&driver.ResultNoRows, nil)
  106. mockConn.EXPECT().ResetSession(gomock.Any()).AnyTimes().Return(nil)
  107. mockConn.EXPECT().Close().AnyTimes().Return(nil)
  108. mockConn.EXPECT().QueryContext(gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes().DoAndReturn(
  109. func(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error) {
  110. rows := &mysqlMockRows{}
  111. rows.data = [][]interface{}{
  112. {"8.0.29"},
  113. }
  114. return rows, nil
  115. })
  116. }
  117. func initXAConnTestResource(t *testing.T) (*gomock.Controller, *sql.DB, *mockSQLInterceptor, *mockTxHook) {
  118. ctrl := gomock.NewController(t)
  119. mockMgr := initMockResourceManager(branch.BranchTypeXA, ctrl)
  120. _ = mockMgr
  121. //db, err := sql.Open("seata-xa-mysql", "root:seata_go@tcp(127.0.0.1:3306)/seata_go_test?multiStatements=true")
  122. db, err := sql.Open("seata-xa-mysql", "root:12345678@tcp(127.0.0.1:3306)/seata_client?multiStatements=true&interpolateParams=true")
  123. if err != nil {
  124. t.Fatal(err)
  125. }
  126. _ = initMockXaConnector(t, ctrl, db, func(t *testing.T, ctrl *gomock.Controller) driver.Connector {
  127. mockTx := mock.NewMockTestDriverTx(ctrl)
  128. mockTx.EXPECT().Commit().AnyTimes().Return(nil)
  129. mockTx.EXPECT().Rollback().AnyTimes().Return(nil)
  130. mockConn := mock.NewMockTestDriverConn(ctrl)
  131. mockConn.EXPECT().Begin().AnyTimes().Return(mockTx, nil)
  132. mockConn.EXPECT().BeginTx(gomock.Any(), gomock.Any()).AnyTimes().Return(mockTx, nil)
  133. baseMockConn(mockConn)
  134. connector := mock.NewMockTestDriverConnector(ctrl)
  135. connector.EXPECT().Connect(gomock.Any()).AnyTimes().Return(mockConn, nil)
  136. return connector
  137. })
  138. mi := &mockSQLInterceptor{}
  139. ti := &mockTxHook{}
  140. exec.CleanCommonHook()
  141. CleanTxHooks()
  142. exec.RegisterCommonHook(mi)
  143. RegisterTxHook(ti)
  144. return ctrl, db, mi, ti
  145. }
  146. func TestXAConn_ExecContext(t *testing.T) {
  147. ctrl, db, mi, ti := initXAConnTestResource(t)
  148. defer func() {
  149. ctrl.Finish()
  150. db.Close()
  151. CleanTxHooks()
  152. }()
  153. t.Run("have xid", func(t *testing.T) {
  154. ctx := tm.InitSeataContext(context.Background())
  155. tm.SetXID(ctx, uuid.New().String())
  156. before := func(_ context.Context, execCtx *types.ExecContext) {
  157. t.Logf("on exec xid=%s", execCtx.TxCtx.XID)
  158. assert.Equal(t, tm.GetXID(ctx), execCtx.TxCtx.XID)
  159. assert.Equal(t, types.XAMode, execCtx.TxCtx.TransactionMode)
  160. }
  161. mi.before = before
  162. var comitCnt int32
  163. beforeCommit := func(tx *Tx) {
  164. atomic.AddInt32(&comitCnt, 1)
  165. assert.Equal(t, tx.tranCtx.TransactionMode, types.XAMode)
  166. }
  167. ti.beforeCommit = beforeCommit
  168. conn, err := db.Conn(context.Background())
  169. assert.NoError(t, err)
  170. _, err = conn.ExecContext(ctx, "SELECT 1")
  171. assert.NoError(t, err)
  172. _, err = db.ExecContext(ctx, "SELECT 1")
  173. assert.NoError(t, err)
  174. // todo fix
  175. assert.Equal(t, int32(0), atomic.LoadInt32(&comitCnt))
  176. })
  177. t.Run("not xid", func(t *testing.T) {
  178. before := func(_ context.Context, execCtx *types.ExecContext) {
  179. assert.Equal(t, "", execCtx.TxCtx.XID)
  180. assert.Equal(t, types.Local, execCtx.TxCtx.TransactionMode)
  181. }
  182. mi.before = before
  183. var comitCnt int32
  184. beforeCommit := func(tx *Tx) {
  185. atomic.AddInt32(&comitCnt, 1)
  186. }
  187. ti.beforeCommit = beforeCommit
  188. conn, err := db.Conn(context.Background())
  189. assert.NoError(t, err)
  190. _, err = conn.ExecContext(context.Background(), "SELECT 1")
  191. assert.NoError(t, err)
  192. _, err = db.ExecContext(context.Background(), "SELECT 1")
  193. assert.NoError(t, err)
  194. _, err = db.Exec("SELECT 1")
  195. assert.NoError(t, err)
  196. assert.Equal(t, int32(0), atomic.LoadInt32(&comitCnt))
  197. })
  198. }
  199. func TestXAConn_BeginTx(t *testing.T) {
  200. ctrl, db, mi, ti := initXAConnTestResource(t)
  201. defer func() {
  202. CleanTxHooks()
  203. db.Close()
  204. ctrl.Finish()
  205. }()
  206. t.Run("tx-local", func(t *testing.T) {
  207. tx, err := db.Begin()
  208. assert.NoError(t, err)
  209. mi.before = func(_ context.Context, execCtx *types.ExecContext) {
  210. assert.Equal(t, "", execCtx.TxCtx.XID)
  211. assert.Equal(t, types.Local, execCtx.TxCtx.TransactionMode)
  212. }
  213. var comitCnt int32
  214. ti.beforeCommit = func(tx *Tx) {
  215. atomic.AddInt32(&comitCnt, 1)
  216. }
  217. _, err = tx.ExecContext(context.Background(), "SELECT * FROM user")
  218. assert.NoError(t, err)
  219. _, err = tx.ExecContext(tm.InitSeataContext(context.Background()), "SELECT * FROM user")
  220. assert.NoError(t, err)
  221. err = tx.Commit()
  222. assert.NoError(t, err)
  223. assert.Equal(t, int32(1), atomic.LoadInt32(&comitCnt))
  224. })
  225. t.Run("tx-local-context", func(t *testing.T) {
  226. tx, err := db.BeginTx(context.Background(), &sql.TxOptions{})
  227. assert.NoError(t, err)
  228. mi.before = func(_ context.Context, execCtx *types.ExecContext) {
  229. assert.Equal(t, "", execCtx.TxCtx.XID)
  230. assert.Equal(t, types.Local, execCtx.TxCtx.TransactionMode)
  231. }
  232. var comitCnt int32
  233. ti.beforeCommit = func(tx *Tx) {
  234. atomic.AddInt32(&comitCnt, 1)
  235. }
  236. _, err = tx.ExecContext(context.Background(), "SELECT * FROM user")
  237. assert.NoError(t, err)
  238. _, err = tx.ExecContext(tm.InitSeataContext(context.Background()), "SELECT * FROM user")
  239. assert.NoError(t, err)
  240. err = tx.Commit()
  241. assert.NoError(t, err)
  242. assert.Equal(t, int32(1), atomic.LoadInt32(&comitCnt))
  243. })
  244. t.Run("tx-xa-context", func(t *testing.T) {
  245. ctx := tm.InitSeataContext(context.Background())
  246. tm.SetXID(ctx, uuid.NewString())
  247. tx, err := db.BeginTx(ctx, &sql.TxOptions{})
  248. assert.NoError(t, err)
  249. mi.before = func(_ context.Context, execCtx *types.ExecContext) {
  250. assert.Equal(t, tm.GetXID(ctx), execCtx.TxCtx.XID)
  251. assert.Equal(t, types.XAMode, execCtx.TxCtx.TransactionMode)
  252. }
  253. var comitCnt int32
  254. ti.beforeCommit = func(tx *Tx) {
  255. atomic.AddInt32(&comitCnt, 1)
  256. }
  257. _, err = tx.ExecContext(context.Background(), "SELECT * FROM user")
  258. assert.NoError(t, err)
  259. _, err = tx.ExecContext(context.Background(), "SELECT * FROM user")
  260. assert.NoError(t, err)
  261. err = tx.Commit()
  262. assert.NoError(t, err)
  263. assert.Equal(t, int32(1), atomic.LoadInt32(&comitCnt))
  264. })
  265. }