This commit is contained in:
@@ -7,12 +7,12 @@ describe('asyncLock', () => {
|
||||
})
|
||||
|
||||
it('executes callback immediately when no lock exists', async () => {
|
||||
let executed = false
|
||||
const executed = { value: false }
|
||||
await acquire('test', async () => {
|
||||
executed = true
|
||||
executed.value = true
|
||||
return 42
|
||||
})
|
||||
expect(executed).toBe(true)
|
||||
expect(executed.value).toBe(true)
|
||||
})
|
||||
|
||||
it('returns callback result', async () => {
|
||||
@@ -24,43 +24,35 @@ describe('asyncLock', () => {
|
||||
|
||||
it('serializes concurrent operations on same key', async () => {
|
||||
const results = []
|
||||
const promises = []
|
||||
|
||||
for (let i = 0; i < 5; i++) {
|
||||
promises.push(
|
||||
acquire('same-key', async () => {
|
||||
results.push(`start-${i}`)
|
||||
await new Promise(resolve => setTimeout(resolve, 10))
|
||||
results.push(`end-${i}`)
|
||||
return i
|
||||
}),
|
||||
)
|
||||
}
|
||||
const promises = Array.from({ length: 5 }, (_, i) =>
|
||||
acquire('same-key', async () => {
|
||||
results.push(`start-${i}`)
|
||||
await new Promise(resolve => setTimeout(resolve, 10))
|
||||
results.push(`end-${i}`)
|
||||
return i
|
||||
}),
|
||||
)
|
||||
|
||||
await Promise.all(promises)
|
||||
|
||||
// Operations should be serialized: start-end pairs should not interleave
|
||||
expect(results.length).toBe(10)
|
||||
for (let i = 0; i < 5; i++) {
|
||||
Array.from({ length: 5 }, (_, i) => {
|
||||
expect(results[i * 2]).toBe(`start-${i}`)
|
||||
expect(results[i * 2 + 1]).toBe(`end-${i}`)
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
it('allows parallel operations on different keys', async () => {
|
||||
const results = []
|
||||
const promises = []
|
||||
|
||||
for (let i = 0; i < 5; i++) {
|
||||
promises.push(
|
||||
acquire(`key-${i}`, async () => {
|
||||
results.push(`start-${i}`)
|
||||
await new Promise(resolve => setTimeout(resolve, 10))
|
||||
results.push(`end-${i}`)
|
||||
return i
|
||||
}),
|
||||
)
|
||||
}
|
||||
const promises = Array.from({ length: 5 }, (_, i) =>
|
||||
acquire(`key-${i}`, async () => {
|
||||
results.push(`start-${i}`)
|
||||
await new Promise(resolve => setTimeout(resolve, 10))
|
||||
results.push(`end-${i}`)
|
||||
return i
|
||||
}),
|
||||
)
|
||||
|
||||
await Promise.all(promises)
|
||||
|
||||
@@ -74,10 +66,10 @@ describe('asyncLock', () => {
|
||||
})
|
||||
|
||||
it('handles errors and releases lock', async () => {
|
||||
let callCount = 0
|
||||
const callCount = { value: 0 }
|
||||
try {
|
||||
await acquire('error-key', async () => {
|
||||
callCount++
|
||||
callCount.value++
|
||||
throw new Error('Test error')
|
||||
})
|
||||
}
|
||||
@@ -87,27 +79,22 @@ describe('asyncLock', () => {
|
||||
|
||||
// Lock should be released, next operation should execute
|
||||
await acquire('error-key', async () => {
|
||||
callCount++
|
||||
callCount.value++
|
||||
return 'success'
|
||||
})
|
||||
|
||||
expect(callCount).toBe(2)
|
||||
expect(callCount.value).toBe(2)
|
||||
})
|
||||
|
||||
it('maintains lock ordering', async () => {
|
||||
const order = []
|
||||
const promises = []
|
||||
|
||||
for (let i = 0; i < 3; i++) {
|
||||
const idx = i
|
||||
promises.push(
|
||||
acquire('ordered', async () => {
|
||||
order.push(`before-${idx}`)
|
||||
await new Promise(resolve => setTimeout(resolve, 5))
|
||||
order.push(`after-${idx}`)
|
||||
}),
|
||||
)
|
||||
}
|
||||
const promises = Array.from({ length: 3 }, (_, i) =>
|
||||
acquire('ordered', async () => {
|
||||
order.push(`before-${i}`)
|
||||
await new Promise(resolve => setTimeout(resolve, 5))
|
||||
order.push(`after-${i}`)
|
||||
}),
|
||||
)
|
||||
|
||||
await Promise.all(promises)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user