mirror of
https://github.com/logos-storage/nim-datastore.git
synced 2026-01-08 00:23:10 +00:00
add async semaphore tests
This commit is contained in:
parent
9e27eec291
commit
f6acaa6f32
@ -10,17 +10,17 @@
|
||||
# those terms.
|
||||
|
||||
import random
|
||||
import chronos
|
||||
|
||||
import ../libp2p/utils/semaphore
|
||||
import pkg/chronos
|
||||
import pkg/asynctest
|
||||
|
||||
import ./helpers
|
||||
import pkg/datastore/threads/asyncsemaphore
|
||||
|
||||
randomize()
|
||||
|
||||
suite "AsyncSemaphore":
|
||||
asyncTest "should acquire":
|
||||
let sema = newAsyncSemaphore(3)
|
||||
test "should acquire":
|
||||
let sema = AsyncSemaphore.new(3)
|
||||
|
||||
await sema.acquire()
|
||||
await sema.acquire()
|
||||
@ -28,8 +28,8 @@ suite "AsyncSemaphore":
|
||||
|
||||
check sema.count == 0
|
||||
|
||||
asyncTest "should release":
|
||||
let sema = newAsyncSemaphore(3)
|
||||
test "should release":
|
||||
let sema = AsyncSemaphore.new(3)
|
||||
|
||||
await sema.acquire()
|
||||
await sema.acquire()
|
||||
@ -41,8 +41,8 @@ suite "AsyncSemaphore":
|
||||
sema.release()
|
||||
check sema.count == 3
|
||||
|
||||
asyncTest "should queue acquire":
|
||||
let sema = newAsyncSemaphore(1)
|
||||
test "should queue acquire":
|
||||
let sema = AsyncSemaphore.new(1)
|
||||
|
||||
await sema.acquire()
|
||||
let fut = sema.acquire()
|
||||
@ -55,20 +55,20 @@ suite "AsyncSemaphore":
|
||||
await sleepAsync(10.millis)
|
||||
check fut.finished()
|
||||
|
||||
asyncTest "should keep count == size":
|
||||
let sema = newAsyncSemaphore(1)
|
||||
test "should keep count == size":
|
||||
let sema = AsyncSemaphore.new(1)
|
||||
sema.release()
|
||||
sema.release()
|
||||
sema.release()
|
||||
check sema.count == 1
|
||||
|
||||
asyncTest "should tryAcquire":
|
||||
let sema = newAsyncSemaphore(1)
|
||||
test "should tryAcquire":
|
||||
let sema = AsyncSemaphore.new(1)
|
||||
await sema.acquire()
|
||||
check sema.tryAcquire() == false
|
||||
|
||||
asyncTest "should tryAcquire and acquire":
|
||||
let sema = newAsyncSemaphore(4)
|
||||
test "should tryAcquire and acquire":
|
||||
let sema = AsyncSemaphore.new(4)
|
||||
check sema.tryAcquire() == true
|
||||
check sema.tryAcquire() == true
|
||||
check sema.tryAcquire() == true
|
||||
@ -88,8 +88,8 @@ suite "AsyncSemaphore":
|
||||
check fut.finished == true
|
||||
check sema.count == 4
|
||||
|
||||
asyncTest "should restrict resource access":
|
||||
let sema = newAsyncSemaphore(3)
|
||||
test "should restrict resource access":
|
||||
let sema = AsyncSemaphore.new(3)
|
||||
var resource = 0
|
||||
|
||||
proc task() {.async.} =
|
||||
@ -110,8 +110,8 @@ suite "AsyncSemaphore":
|
||||
|
||||
await allFutures(tasks)
|
||||
|
||||
asyncTest "should cancel sequential semaphore slot":
|
||||
let sema = newAsyncSemaphore(1)
|
||||
test "should cancel sequential semaphore slot":
|
||||
let sema = AsyncSemaphore.new(1)
|
||||
|
||||
await sema.acquire()
|
||||
|
||||
@ -131,8 +131,8 @@ suite "AsyncSemaphore":
|
||||
|
||||
check await sema.acquire().withTimeout(10.millis)
|
||||
|
||||
asyncTest "should handle out of order cancellations":
|
||||
let sema = newAsyncSemaphore(1)
|
||||
test "should handle out of order cancellations":
|
||||
let sema = AsyncSemaphore.new(1)
|
||||
|
||||
await sema.acquire() # 1st acquire
|
||||
let tmp1 = sema.acquire() # 2nd acquire
|
||||
@ -156,8 +156,8 @@ suite "AsyncSemaphore":
|
||||
sema.release() # 4th release
|
||||
check await sema.acquire().withTimeout(10.millis)
|
||||
|
||||
asyncTest "should properly handle timeouts and cancellations":
|
||||
let sema = newAsyncSemaphore(1)
|
||||
test "should properly handle timeouts and cancellations":
|
||||
let sema = AsyncSemaphore.new(1)
|
||||
|
||||
await sema.acquire()
|
||||
check not(await sema.acquire().withTimeout(1.millis)) # should not acquire but cancel
|
||||
@ -165,8 +165,8 @@ suite "AsyncSemaphore":
|
||||
|
||||
check await sema.acquire().withTimeout(10.millis)
|
||||
|
||||
asyncTest "should handle forceAcquire properly":
|
||||
let sema = newAsyncSemaphore(1)
|
||||
test "should handle forceAcquire properly":
|
||||
let sema = AsyncSemaphore.new(1)
|
||||
|
||||
await sema.acquire()
|
||||
check not(await sema.acquire().withTimeout(1.millis)) # should not acquire but cancel
|
||||
|
||||
@ -7,6 +7,7 @@ import
|
||||
./datastore/testmountedds,
|
||||
./datastore/testdatabuffer,
|
||||
./datastore/testthreadproxyds,
|
||||
./datastore/testasyncsemaphore,
|
||||
./datastore/testsemaphore
|
||||
|
||||
{.warning[UnusedImport]: off.}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user