(sequelize, Task, from, to)
| 445 | // Lock the row with id of `from`, and then try to update the row |
| 446 | // with id of `to` |
| 447 | const update = async (sequelize, Task, from, to) => { |
| 448 | await sequelize |
| 449 | .transaction(async transaction => { |
| 450 | try { |
| 451 | try { |
| 452 | await Task.findAll({ |
| 453 | where: { id: { [Sequelize.Op.eq]: from } }, |
| 454 | lock: transaction.LOCK.UPDATE, |
| 455 | transaction |
| 456 | }); |
| 457 | |
| 458 | await delay(10); |
| 459 | |
| 460 | await Task.update( |
| 461 | { id: to }, |
| 462 | { |
| 463 | where: { id: { [Sequelize.Op.ne]: to } }, |
| 464 | lock: transaction.LOCK.UPDATE, |
| 465 | transaction |
| 466 | } |
| 467 | ); |
| 468 | } catch (e) { |
| 469 | console.log(e.message); |
| 470 | } |
| 471 | |
| 472 | await Task.create({ id: 2 }, { transaction }); |
| 473 | } catch (e) { |
| 474 | console.log(e.message); |
| 475 | } |
| 476 | |
| 477 | throw new Error('Rollback!'); |
| 478 | }) |
| 479 | .catch(() => {}); |
| 480 | }; |
| 481 | |
| 482 | it('should treat deadlocked transaction as rollback', async function() { |
| 483 | const Task = await getAndInitializeTaskModel(this.sequelize); |
no test coverage detected