/** * Copyright (c) 2022 EdgerOS Team. * All rights reserved. * * Detailed license information can be found in the LICENSE file. * * Author : xueqiang * Date : 2024-08-07 10:46:45 * LastEditors : xueqiang * LastEditTime : 2024-08-14 11:14:12 */ import { type DBClient, LockFailedError } from '../' import { createTestClientAndKeys, tearDownTestClient } from './util' import { test } from '@edgeros/tapes' import assert from 'assert' const assertCantLock = async (client: DBClient) => // EtcdLockFailedError await client.lock('resource').acquire() const assertAbleToLock = async (client: DBClient) => { const lock = client.lock('resource') lock.options({}) await lock.acquire() await lock.release() } test('lock() - locks exclusively around a resource', async (t) => { const client = await createTestClientAndKeys() const lock1 = client.lock('resource') // eslint-disable-next-line @typescript-eslint/ban-ts-comment -- i // @ts-ignore const lease = lock1.lease // eslint-disable-next-line @typescript-eslint/ban-ts-comment -- i // @ts-ignore lock1.lease = null try { await lock1.release() } catch (error) { t.pass('no acquired error.') } // eslint-disable-next-line @typescript-eslint/ban-ts-comment -- i // @ts-ignore lock1.lease = lease await lock1.acquire() try { await assertCantLock(client) } catch (err) { assert.ok(err instanceof LockFailedError) } await lock1.release() await assertAbleToLock(client) await tearDownTestClient(client) t.pass('lock() - locks exclusively around a resource - ') t.end() }) test('lock() - provides locking around functions', async (t) => { const client = await createTestClientAndKeys() // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment -- i const cantlock = assertCantLock.bind(null, client) try { // eslint-disable-next-line @typescript-eslint/no-unsafe-argument -- i await client.lock('resource').do(cantlock) } catch (err) { assert.ok(err instanceof LockFailedError) } await assertAbleToLock(client) await tearDownTestClient(client) t.pass('lock() - provides locking around functions - ') t.end() }) test('lock() allows setting lock TTL before acquiring', async (t) => { const client = await createTestClientAndKeys() const lock = await client.lock('resource').ttl(10).acquire() await lock.release() await tearDownTestClient(client) t.pass('lock() - allows setting lock TTL before acquiring - ') t.end() }) test('lock() disallows setting TTL while lock is acquired', async (t) => { const client = await createTestClientAndKeys() const lock = await client.lock('resource').acquire() try { lock.ttl(10) } catch (err) { // eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-member-access -- i assert.ok(/Cannot set a lock TTL after acquiring the lock/.test(err.message)) } await lock.release() await tearDownTestClient(client) t.pass('lock() - disallows setting TTL while lock is acquired - ') t.end() }) test('lock() gets the lock lease ID', async (t) => { const client = await createTestClientAndKeys() const lock = client.lock('resource') console.warn('leaseId', await lock.leaseId()) assert.equal(await lock.leaseId(), null) await lock.acquire() const leaseId = await lock.leaseId() assert.ok(typeof leaseId === 'string') console.warn('-----leaseId', leaseId) console.warn('+++++leaseId', (await client.get('resource').exec()).kvs[0].lease) assert.equal((await client.get('resource').exec()).kvs[0].lease, leaseId) await lock.release() await tearDownTestClient(client) t.pass('lock() - gets the lock lease ID - ') t.end() })