@@ -2880,6 +2880,138 @@ func (s *testDBSuite4) TestAlterShardRowIDBits(c *C) {
28802880 c .Assert (err .Error (), Equals , "[autoid:1467]Failed to read auto-increment value from storage engine" )
28812881}
28822882
2883+ // port from mysql
2884+ // https://github.com/mysql/mysql-server/blob/124c7ab1d6f914637521fd4463a993aa73403513/mysql-test/t/lock.test
2885+ func (s * testDBSuite2 ) TestLock (c * C ) {
2886+ s .tk = testkit .NewTestKit (c , s .store )
2887+ tk := s .tk
2888+ tk .MustExec ("use test" )
2889+
2890+ /* Testing of table locking */
2891+ tk .MustExec ("DROP TABLE IF EXISTS t1" )
2892+ tk .MustExec ("CREATE TABLE t1 ( `id` int(11) NOT NULL default '0', `id2` int(11) NOT NULL default '0', `id3` int(11) NOT NULL default '0', `dummy1` char(30) default NULL, PRIMARY KEY (`id`,`id2`), KEY `index_id3` (`id3`))" )
2893+ tk .MustExec ("insert into t1 (id,id2) values (1,1),(1,2),(1,3)" )
2894+ tk .MustExec ("LOCK TABLE t1 WRITE" )
2895+ tk .MustExec ("select dummy1,count(distinct id) from t1 group by dummy1" )
2896+ tk .MustExec ("update t1 set id=-1 where id=1" )
2897+ tk .MustExec ("LOCK TABLE t1 READ" )
2898+ _ , err := tk .Exec ("update t1 set id=1 where id=1" )
2899+ c .Assert (terror .ErrorEqual (err , infoschema .ErrTableNotLockedForWrite ), IsTrue )
2900+ tk .MustExec ("unlock tables" )
2901+ tk .MustExec ("update t1 set id=1 where id=-1" )
2902+ tk .MustExec ("drop table t1" )
2903+ }
2904+
2905+ // port from mysql
2906+ // https://github.com/mysql/mysql-server/blob/4f1d7cf5fcb11a3f84cff27e37100d7295e7d5ca/mysql-test/t/tablelock.test
2907+ func (s * testDBSuite2 ) TestTableLock (c * C ) {
2908+ s .tk = testkit .NewTestKit (c , s .store )
2909+ tk := s .tk
2910+ tk .MustExec ("use test" )
2911+ tk .MustExec ("drop table if exists t1,t2" )
2912+
2913+ /* Test of lock tables */
2914+ tk .MustExec ("create table t1 ( n int auto_increment primary key)" )
2915+ tk .MustExec ("lock tables t1 write" )
2916+ tk .MustExec ("insert into t1 values(NULL)" )
2917+ tk .MustExec ("unlock tables" )
2918+ checkTableLock (c , tk .Se , "test" , "t1" , model .TableLockNone )
2919+
2920+ tk .MustExec ("lock tables t1 write" )
2921+ tk .MustExec ("insert into t1 values(NULL)" )
2922+ tk .MustExec ("unlock tables" )
2923+ checkTableLock (c , tk .Se , "test" , "t1" , model .TableLockNone )
2924+
2925+ tk .MustExec ("drop table if exists t1" )
2926+
2927+ /* Test of locking and delete of files */
2928+ tk .MustExec ("drop table if exists t1,t2" )
2929+ tk .MustExec ("CREATE TABLE t1 (a int)" )
2930+ tk .MustExec ("CREATE TABLE t2 (a int)" )
2931+ tk .MustExec ("lock tables t1 write, t2 write" )
2932+ tk .MustExec ("drop table t1,t2" )
2933+
2934+ tk .MustExec ("CREATE TABLE t1 (a int)" )
2935+ tk .MustExec ("CREATE TABLE t2 (a int)" )
2936+ tk .MustExec ("lock tables t1 write, t2 write" )
2937+ tk .MustExec ("drop table t2,t1" )
2938+ }
2939+
2940+ // port from mysql
2941+ // https://github.com/mysql/mysql-server/blob/4f1d7cf5fcb11a3f84cff27e37100d7295e7d5ca/mysql-test/t/lock_tables_lost_commit.test
2942+ func (s * testDBSuite2 ) TestTableLocksLostCommit (c * C ) {
2943+ s .tk = testkit .NewTestKit (c , s .store )
2944+ tk2 := testkit .NewTestKit (c , s .store )
2945+ tk := s .tk
2946+ tk .MustExec ("use test" )
2947+ tk2 .MustExec ("use test" )
2948+
2949+ tk .MustExec ("DROP TABLE IF EXISTS t1" )
2950+ tk .MustExec ("CREATE TABLE t1(a INT)" )
2951+ tk .MustExec ("LOCK TABLES t1 WRITE" )
2952+ tk .MustExec ("INSERT INTO t1 VALUES(10)" )
2953+
2954+ _ , err := tk2 .Exec ("SELECT * FROM t1" )
2955+ c .Assert (terror .ErrorEqual (err , infoschema .ErrTableLocked ), IsTrue )
2956+
2957+ tk .Se .Close ()
2958+
2959+ tk2 .MustExec ("SELECT * FROM t1" )
2960+ tk2 .MustExec ("DROP TABLE t1" )
2961+
2962+ tk .MustExec ("unlock tables" )
2963+ }
2964+
2965+ // test write local lock
2966+ func (s * testDBSuite2 ) TestWriteLocal (c * C ) {
2967+ s .tk = testkit .NewTestKit (c , s .store )
2968+ tk2 := testkit .NewTestKit (c , s .store )
2969+ tk := s .tk
2970+ tk .MustExec ("use test" )
2971+ tk2 .MustExec ("use test" )
2972+ tk .MustExec ("drop table if exists t1" )
2973+ tk .MustExec ("create table t1 ( n int auto_increment primary key)" )
2974+
2975+ // Test: allow read
2976+ tk .MustExec ("lock tables t1 write local" )
2977+ tk .MustExec ("insert into t1 values(NULL)" )
2978+ tk2 .MustQuery ("select count(*) from t1" )
2979+ tk .MustExec ("unlock tables" )
2980+ tk2 .MustExec ("unlock tables" )
2981+
2982+ // Test: forbid write
2983+ tk .MustExec ("lock tables t1 write local" )
2984+ _ , err := tk2 .Exec ("insert into t1 values(NULL)" )
2985+ c .Assert (terror .ErrorEqual (err , infoschema .ErrTableLocked ), IsTrue )
2986+ tk .MustExec ("unlock tables" )
2987+ tk2 .MustExec ("unlock tables" )
2988+
2989+ // Test mutex: lock write local first
2990+ tk .MustExec ("lock tables t1 write local" )
2991+ _ , err = tk2 .Exec ("lock tables t1 write local" )
2992+ c .Assert (terror .ErrorEqual (err , infoschema .ErrTableLocked ), IsTrue )
2993+ _ , err = tk2 .Exec ("lock tables t1 write" )
2994+ c .Assert (terror .ErrorEqual (err , infoschema .ErrTableLocked ), IsTrue )
2995+ _ , err = tk2 .Exec ("lock tables t1 read" )
2996+ c .Assert (terror .ErrorEqual (err , infoschema .ErrTableLocked ), IsTrue )
2997+ tk .MustExec ("unlock tables" )
2998+ tk2 .MustExec ("unlock tables" )
2999+
3000+ // Test mutex: lock write first
3001+ tk .MustExec ("lock tables t1 write" )
3002+ _ , err = tk2 .Exec ("lock tables t1 write local" )
3003+ c .Assert (terror .ErrorEqual (err , infoschema .ErrTableLocked ), IsTrue )
3004+ tk .MustExec ("unlock tables" )
3005+ tk2 .MustExec ("unlock tables" )
3006+
3007+ // Test mutex: lock read first
3008+ tk .MustExec ("lock tables t1 read" )
3009+ _ , err = tk2 .Exec ("lock tables t1 write local" )
3010+ c .Assert (terror .ErrorEqual (err , infoschema .ErrTableLocked ), IsTrue )
3011+ tk .MustExec ("unlock tables" )
3012+ tk2 .MustExec ("unlock tables" )
3013+ }
3014+
28833015func (s * testDBSuite2 ) TestLockTables (c * C ) {
28843016 s .tk = testkit .NewTestKit (c , s .store )
28853017 tk := s .tk
0 commit comments