mirror of
https://github.com/logos-storage/nim-datastore.git
synced 2026-01-03 22:23:10 +00:00
80 lines
1.7 KiB
Nim
80 lines
1.7 KiB
Nim
|
|
import std/options
|
||
|
|
import std/locks
|
||
|
|
import std/os
|
||
|
|
import pkg/stew/byteutils
|
||
|
|
import pkg/unittest2
|
||
|
|
import pkg/questionable
|
||
|
|
import pkg/questionable/results
|
||
|
|
|
||
|
|
include pkg/datastore/threads/sharedptr
|
||
|
|
include pkg/datastore/threads/databuffer
|
||
|
|
|
||
|
|
|
||
|
|
type TestObj = object
|
||
|
|
val: ref int
|
||
|
|
|
||
|
|
proc `=destroy`(obj: var TestObj) =
|
||
|
|
echo "test obj destroy"
|
||
|
|
obj.val[] = 0
|
||
|
|
|
||
|
|
proc destroyTest(intref: ref int) =
|
||
|
|
let a: SharedPtr[TestObj] = newSharedPtr(unsafeIsolate TestObj(val: intref))
|
||
|
|
echo "a[]: ", a[]
|
||
|
|
check a[].val[] == 10
|
||
|
|
|
||
|
|
proc runDestroyTest() =
|
||
|
|
echo "\nintref setup:\n"
|
||
|
|
let intref: ref int = new(ref int)
|
||
|
|
intref[] = 10
|
||
|
|
destroyTest(intref)
|
||
|
|
check intref[] == 0
|
||
|
|
|
||
|
|
proc runDestroyOnReleaseTest() =
|
||
|
|
echo "\nintref setup:\n"
|
||
|
|
let intref: ref int = new(ref int)
|
||
|
|
intref[] = 20
|
||
|
|
var a: SharedPtr[TestObj] = newSharedPtr(unsafeIsolate TestObj(val: intref))
|
||
|
|
try:
|
||
|
|
echo "a[]: ", a[]
|
||
|
|
check a[].val[] == 20
|
||
|
|
finally:
|
||
|
|
a.release()
|
||
|
|
check intref[] == 0
|
||
|
|
|
||
|
|
|
||
|
|
suite "Share buffer test":
|
||
|
|
|
||
|
|
setup:
|
||
|
|
var a1 {.used.}: SharedPtr[int]
|
||
|
|
let a2 {.used.} = newSharedPtr(0)
|
||
|
|
let a3 {.used.} = a2
|
||
|
|
|
||
|
|
test "basics":
|
||
|
|
echo "a1: ", $a1
|
||
|
|
check $a1 == "nil"
|
||
|
|
check a1.isNil
|
||
|
|
check $a2 == "(val: 0)"
|
||
|
|
check not a2.isNil
|
||
|
|
check a2[] == 0
|
||
|
|
check $a3 == "(val: 0)"
|
||
|
|
check not a3.isNil
|
||
|
|
check a3[] == 0
|
||
|
|
|
||
|
|
test "test destroy procs":
|
||
|
|
runDestroyTest()
|
||
|
|
|
||
|
|
test "test destroy release":
|
||
|
|
runDestroyOnReleaseTest()
|
||
|
|
|
||
|
|
test "test destroy release no proc":
|
||
|
|
echo "\nintref setup:\n"
|
||
|
|
let intref: ref int = new(ref int)
|
||
|
|
intref[] = 30
|
||
|
|
var a: SharedPtr[TestObj] = newSharedPtr(unsafeIsolate TestObj(val: intref))
|
||
|
|
try:
|
||
|
|
echo "a[]: ", a[]
|
||
|
|
check a[].val[] == 30
|
||
|
|
finally:
|
||
|
|
a.release()
|
||
|
|
check intref[] == 0
|