@@ -101,6 +101,58 @@ describe('test/client.test.js', () => {
101101 } ) ;
102102 } ) ;
103103
104+ describe ( 'locks([...]), lockOne(name, lockType, alias), unlock()' , ( ) => {
105+ it ( 'validate arguments' , async ( ) => {
106+ await assert . rejects ( async ( ) => {
107+ await db . locks ( [
108+ { tableName : 'xxxx' } ,
109+ ] ) ;
110+ } , new Error ( 'No lock_type provided while trying to lock table `xxxx`' ) ) ;
111+
112+ await assert . rejects ( async ( ) => {
113+ await db . locks ( [
114+ { lockType : 'READ' } ,
115+ ] ) ;
116+ } , new Error ( 'No table_name provided while trying to lock table' ) ) ;
117+ } ) ;
118+
119+ it ( 'should lock a table' , async ( ) => {
120+ // assert.equal(sql.replace(/\s+/g, ' '), 'LOCK TABLES `posts` READ;');
121+ await assert . rejects ( async ( ) => {
122+ await db . locks ( [
123+ { tableName : table , lockType : 'READ' } ,
124+ { tableName : table , lockType : 'READ' } ,
125+ ] ) ;
126+ } , err => err . sql . includes ( 'LOCK TABLES `' + table + '` READ, `' + table + '` READ;' ) ) ;
127+ } ) ;
128+
129+ it ( 'should lock multiple tables' , async ( ) => {
130+ // assert.equal(sql.replaceAll(/\s+/g, ' '), 'LOCK TABLES `posts` READ, `posts2` WRITE, `posts3` AS `t` WRITE;');
131+ await assert . rejects ( async ( ) => {
132+ await db . locks ( [
133+ { tableName : table , lockType : 'READ' } ,
134+ { tableName : table , lockType : 'WRITE' } ,
135+ { tableName : table , lockType : 'WRITE' , tableAlias : 't' } ,
136+ ] ) ;
137+ } , err => err . sql . includes ( 'LOCK TABLES `' + table + '` READ, `' + table + '` WRITE, `' + table + '` AS `t` WRITE;' ) ) ;
138+ await assert . rejects ( async ( ) => {
139+ await db . locks ( [
140+ { tableName : 'xxxx' } ,
141+ ] ) ;
142+ } , new Error ( 'No lock_type provided while trying to lock table `xxxx`' ) ) ;
143+ } ) ;
144+ it ( 'should unlock tables' , async ( ) => {
145+ await db . lockOne ( 'ali-sdk-test-user' , 'READ' , 't' ) ;
146+ // error thrown: when table locked with alias, you can only query with the alias.
147+ await assert . rejects ( async ( ) => {
148+ await db . query ( 'select * from `ali-sdk-test-user` limit 1;' ) ;
149+ } ) ;
150+ await db . unlock ( ) ;
151+ // recovered after unlock.
152+ await db . query ( 'select * from `ali-sdk-test-user` limit 1;' ) ;
153+ } ) ;
154+ } ) ;
155+
104156 describe ( 'transactions' , ( ) => {
105157 it ( 'should beginTransaction error' , async ( ) => {
106158 const failDB = new RDSClient ( {
@@ -217,6 +269,24 @@ describe('test/client.test.js', () => {
217269 assert . equal ( rows [ 1 ] . name , prefix + 'beginTransaction2' ) ;
218270 } ) ;
219271
272+ it ( 'should lock & unlock table during transaction' , async ( ) => {
273+ const conn = await db . getConnection ( ) ;
274+ try {
275+ await conn . beginTransaction ( ) ;
276+ await conn . lockOne ( 'ali-sdk-test-user' , 'READ' , 't' ) ;
277+ // error thrown: when table locked with alias, you can only query with the alias.
278+ await assert . rejects ( async ( ) => {
279+ await conn . query ( 'select * from `ali-sdk-test-user` limit 1;' ) ;
280+ } ) ;
281+ await conn . unlock ( ) ;
282+ // recovered after unlock.
283+ await conn . query ( 'select * from `ali-sdk-test-user` limit 1;' ) ;
284+ } catch ( err ) {
285+ conn . release ( ) ;
286+ throw err ;
287+ }
288+ } ) ;
289+
220290 it ( 'should rollback when query fail' , async ( ) => {
221291 const conn = await db . getConnection ( ) ;
222292 try {
0 commit comments