From 19faca3f352d6b18e6a6468036d308b34f594d05 Mon Sep 17 00:00:00 2001 From: Bob den Os Date: Thu, 26 Jun 2025 14:11:55 +0200 Subject: [PATCH] Setup forUpdate wait>0 test case for HANA behavior --- hana/lib/HANAService.js | 2 +- hana/test/lock.test.js | 51 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 hana/test/lock.test.js diff --git a/hana/lib/HANAService.js b/hana/lib/HANAService.js index 58f18a503..f1af23d5f 100644 --- a/hana/lib/HANAService.js +++ b/hana/lib/HANAService.js @@ -213,7 +213,7 @@ class HANAService extends SQLService { // Clear current tenant connection pool this.disconnect(this.context.tenant) } - throw err + return this.rollback(err) } } diff --git a/hana/test/lock.test.js b/hana/test/lock.test.js new file mode 100644 index 000000000..aa840b55e --- /dev/null +++ b/hana/test/lock.test.js @@ -0,0 +1,51 @@ +const { tx } = require('@sap/cds') +const cds = require('../../test/cds.js') + +describe('locking', () => { + const { expect } = cds.test(__dirname + '/../../test/bookshop') + + describe('forUpdate', async () => { + + test('wait=0', async () => { + const { Books } = cds.entities + let tx1, tx2 + try { + tx1 = await cds.tx() + tx2 = await cds.tx() + const query = cds.ql.SELECT.from(Books).forUpdate({ wait: 0 }) + + await tx1.run(query) + await expect(tx2.run(query)).rejected + } finally { + await tx1?.rollback() + await tx2?.rollback() + } + }) + + test('wait>0', async () => { + const { Books } = cds.entities + let tx1, tx2 + try { + tx1 = await cds.tx() + tx2 = await cds.tx() + const query = cds.ql.SELECT.from(Books).forUpdate({ wait: 1 }) + + await tx2.run(INSERT({ ID: 999 }).into(Books)) + + await tx1.run(query) + + await expect(tx2.run(query)).rejected + + await tx2.commit() + await tx1.commit() + + const res = await cds.ql.SELECT.from(Books).where({ ID: 999 }) + expect(res).length(0) + } finally { + await tx1?.rollback() + await tx2?.rollback() + } + }) + + }) +}) \ No newline at end of file