tidb_test.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. package tidb
  2. import (
  3. "context"
  4. "database/sql"
  5. "os"
  6. "testing"
  7. "time"
  8. "go-common/library/net/netutil/breaker"
  9. xtime "go-common/library/time"
  10. )
  11. func TestMySQL(t *testing.T) {
  12. bc := &breaker.Config{
  13. Window: xtime.Duration(10 * time.Second),
  14. Sleep: xtime.Duration(10 * time.Second),
  15. Bucket: 10,
  16. Ratio: 0.5,
  17. Request: 100,
  18. }
  19. dsn := os.Getenv("TEST_MYSQL_DSN")
  20. if dsn == "" {
  21. t.Skipf("TEST_MYSQL_DSN is empty, sql test skipped")
  22. }
  23. dsn = dsn + "?timeout=5s&readTimeout=5s&writeTimeout=5s&parseTime=true&loc=Local&charset=utf8"
  24. c := &Config{
  25. DSN: dsn,
  26. Active: 10,
  27. Idle: 5,
  28. IdleTimeout: xtime.Duration(time.Minute),
  29. QueryTimeout: xtime.Duration(time.Minute),
  30. ExecTimeout: xtime.Duration(time.Minute),
  31. TranTimeout: xtime.Duration(time.Minute),
  32. Breaker: bc,
  33. }
  34. db := NewTiDB(c)
  35. defer db.Close()
  36. testPing(t, db)
  37. testTable(t, db)
  38. testExec(t, db)
  39. testQuery(t, db)
  40. testQueryRow(t, db)
  41. testPrepare(t, db)
  42. testPrepared(t, db)
  43. testTransaction(t, db)
  44. }
  45. func testTransaction(t *testing.T, db *DB) {
  46. var (
  47. tx *Tx
  48. err error
  49. execSQL = "INSERT INTO test(name) VALUES(?)"
  50. selSQL = "SELECT name FROM test WHERE name=?"
  51. txstmt *Stmt
  52. )
  53. if tx, err = db.Begin(context.TODO()); err != nil {
  54. t.Errorf("MySQL: db transaction Begin err(%v)", err)
  55. tx.Rollback()
  56. return
  57. }
  58. t.Log("MySQL: db transaction begin")
  59. if txstmt, err = tx.Prepare(execSQL); err != nil {
  60. t.Errorf("MySQL: tx.Prepare err(%v)", err)
  61. }
  62. if stmt := tx.Stmt(txstmt); stmt == nil {
  63. t.Errorf("MySQL:tx.Stmt err(%v)", err)
  64. }
  65. // exec
  66. if _, err = tx.Exec(execSQL, "tx1"); err != nil {
  67. t.Errorf("MySQL: tx.Exec err(%v)", err)
  68. tx.Rollback()
  69. return
  70. }
  71. t.Logf("MySQL:tx.Exec tx1")
  72. if _, err = tx.Exec(execSQL, "tx1"); err != nil {
  73. t.Errorf("MySQL: tx.Exec err(%v)", err)
  74. tx.Rollback()
  75. return
  76. }
  77. t.Logf("MySQL:tx.Exec tx1")
  78. // query
  79. rows, err := tx.Query(selSQL, "tx2")
  80. if err != nil {
  81. t.Errorf("MySQL:tx.Query err(%v)", err)
  82. tx.Rollback()
  83. return
  84. }
  85. rows.Close()
  86. t.Log("MySQL: tx.Query tx2")
  87. // queryrow
  88. var name string
  89. row := tx.QueryRow(selSQL, "noexist")
  90. if err = row.Scan(&name); err != sql.ErrNoRows {
  91. t.Errorf("MySQL: queryRow name: noexist")
  92. }
  93. if err = tx.Commit(); err != nil {
  94. t.Errorf("MySQL:tx.Commit err(%v)", err)
  95. }
  96. if err = tx.Commit(); err != nil {
  97. t.Logf("MySQL:tx.Commit err(%v)", err)
  98. }
  99. if err = tx.Rollback(); err != nil {
  100. t.Logf("MySQL:tx Rollback err(%v)", err)
  101. }
  102. }
  103. func testPing(t *testing.T, db *DB) {
  104. if err := db.Ping(context.TODO()); err != nil {
  105. t.Errorf("MySQL: ping error(%v)", err)
  106. t.FailNow()
  107. } else {
  108. t.Log("MySQL: ping ok")
  109. }
  110. }
  111. func testTable(t *testing.T, db *DB) {
  112. table := "CREATE TABLE IF NOT EXISTS `test` (`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '自增ID', `name` varchar(16) NOT NULL DEFAULT '' COMMENT '名称', PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8"
  113. if _, err := db.Exec(context.TODO(), table); err != nil {
  114. t.Errorf("MySQL: create table error(%v)", err)
  115. } else {
  116. t.Log("MySQL: create table ok")
  117. }
  118. }
  119. func testExec(t *testing.T, db *DB) {
  120. sql := "INSERT INTO test(name) VALUES(?)"
  121. if _, err := db.Exec(context.TODO(), sql, "test"); err != nil {
  122. t.Errorf("MySQL: insert error(%v)", err)
  123. } else {
  124. t.Log("MySQL: insert ok")
  125. }
  126. }
  127. func testQuery(t *testing.T, db *DB) {
  128. sql := "SELECT name FROM test WHERE name=?"
  129. rows, err := db.Query(context.TODO(), sql, "test")
  130. if err != nil {
  131. t.Errorf("MySQL: query error(%v)", err)
  132. }
  133. defer rows.Close()
  134. for rows.Next() {
  135. name := ""
  136. if err := rows.Scan(&name); err != nil {
  137. t.Errorf("MySQL: query scan error(%v)", err)
  138. } else {
  139. t.Logf("MySQL: query name: %s", name)
  140. }
  141. }
  142. }
  143. func testQueryRow(t *testing.T, db *DB) {
  144. sql := "SELECT name FROM test WHERE name=?"
  145. name := ""
  146. row := db.QueryRow(context.TODO(), sql, "test")
  147. if err := row.Scan(&name); err != nil {
  148. t.Errorf("MySQL: queryRow error(%v)", err)
  149. } else {
  150. t.Logf("MySQL: queryRow name: %s", name)
  151. }
  152. }
  153. func testPrepared(t *testing.T, db *DB) {
  154. sql := "SELECT name FROM test WHERE name=?"
  155. name := ""
  156. stmt := db.Prepared(sql)
  157. row := stmt.QueryRow(context.TODO(), "test")
  158. if err := row.Scan(&name); err != nil {
  159. t.Errorf("MySQL: prepared query error(%v)", err)
  160. } else {
  161. t.Logf("MySQL: prepared query name: %s", name)
  162. }
  163. if err := stmt.Close(); err != nil {
  164. t.Errorf("MySQL:stmt.Close err(%v)", err)
  165. }
  166. }
  167. func testPrepare(t *testing.T, db *DB) {
  168. var (
  169. selsql = "SELECT name FROM test WHERE name=?"
  170. execsql = "INSERT INTO test(name) VALUES(?)"
  171. name = ""
  172. )
  173. selstmt, err := db.Prepare(selsql)
  174. if err != nil {
  175. t.Errorf("MySQL:Prepare err(%v)", err)
  176. return
  177. }
  178. row := selstmt.QueryRow(context.TODO(), "noexit")
  179. if err = row.Scan(&name); err == sql.ErrNoRows {
  180. t.Logf("MySQL: prepare query error(%v)", err)
  181. } else {
  182. t.Errorf("MySQL: prepared query name: noexist")
  183. }
  184. rows, err := selstmt.Query(context.TODO(), "test")
  185. if err != nil {
  186. t.Errorf("MySQL:stmt.Query err(%v)", err)
  187. }
  188. rows.Close()
  189. execstmt, err := db.Prepare(execsql)
  190. if err != nil {
  191. t.Errorf("MySQL:Prepare err(%v)", err)
  192. return
  193. }
  194. if _, err := execstmt.Exec(context.TODO(), "test"); err != nil {
  195. t.Errorf("MySQL: stmt.Exec(%v)", err)
  196. }
  197. }
  198. func BenchmarkMySQL(b *testing.B) {
  199. c := &Config{
  200. DSN: "test:test@tcp(172.16.0.148:3306)/test?timeout=5s&readTimeout=5s&writeTimeout=5s&parseTime=true&loc=Local&charset=utf8",
  201. Active: 10,
  202. Idle: 5,
  203. IdleTimeout: xtime.Duration(time.Minute),
  204. }
  205. db := NewTiDB(c)
  206. defer db.Close()
  207. b.RunParallel(func(pb *testing.PB) {
  208. for pb.Next() {
  209. sql := "SELECT name FROM test WHERE name=?"
  210. rows, err := db.Query(context.TODO(), sql, "test")
  211. if err == nil {
  212. for rows.Next() {
  213. var name string
  214. if err = rows.Scan(&name); err != nil {
  215. break
  216. }
  217. }
  218. rows.Close()
  219. }
  220. }
  221. })
  222. }