mirror of
https://github.com/logos-storage/nim-datastore.git
synced 2026-01-02 21:53:05 +00:00
fixing up tests
This commit is contained in:
parent
88bf70cb5f
commit
fc8be3f1e6
@ -87,14 +87,23 @@ method put*(
|
||||
data: seq[byte]
|
||||
): Future[?!void] {.async.} =
|
||||
|
||||
echo "put new request thr: ", $getThreadId()
|
||||
var ret = await newThreadResult(void)
|
||||
|
||||
try:
|
||||
put(ret, self.tds, key, data)
|
||||
echo "\n"
|
||||
echo "wait put thr: ", $getThreadId()
|
||||
echo "\n"
|
||||
await sleepAsync(400.milliseconds)
|
||||
await wait(ret)
|
||||
echo "\n"
|
||||
await sleepAsync(400.milliseconds)
|
||||
|
||||
return ret.convert(void)
|
||||
finally:
|
||||
echo "\n"
|
||||
await sleepAsync(400.milliseconds)
|
||||
echo "PUT RELEASE"
|
||||
ret.release()
|
||||
|
||||
|
||||
@ -44,10 +44,13 @@ proc `==`*(a, b: DataBuffer): bool =
|
||||
|
||||
proc new*(tp: typedesc[DataBuffer], size: int = 0): DataBuffer =
|
||||
## allocate new buffer with given size
|
||||
newSharedPtr(DataBufferHolder(
|
||||
result = newSharedPtr(DataBufferHolder(
|
||||
buf: cast[typeof(result[].buf)](allocShared0(size)),
|
||||
size: size,
|
||||
))
|
||||
echo "DataBuffer:new: ", result.unsafeRawPtr().repr,
|
||||
" @ ", result[].buf.pointer.repr & " thr: " & $getThreadId(),
|
||||
" -> ", result.toString().repr
|
||||
|
||||
proc new*[T: byte | char](tp: typedesc[DataBuffer], data: openArray[T]): DataBuffer =
|
||||
## allocate new buffer and copies indata from openArray
|
||||
@ -63,7 +66,7 @@ proc toSeq*[T: byte | char](a: DataBuffer, tp: typedesc[T]): seq[T] =
|
||||
|
||||
proc toString*(data: DataBuffer): string =
|
||||
## convert buffer to string type using copy
|
||||
if data.isNil: return ""
|
||||
if data.isNil: return "nil"
|
||||
result = newString(data.len())
|
||||
if data.len() > 0:
|
||||
copyMem(addr result[0], unsafeAddr data[].buf[0], data.len)
|
||||
|
||||
@ -31,14 +31,14 @@ proc decr*[T](x: var SharedPtr[T]) =
|
||||
if x.container != nil:
|
||||
let res = atomicSubFetch(addr x.container.cnt, 1, ATOMIC_ACQUIRE)
|
||||
if res == 0:
|
||||
echo "SharedPtr: FREE: ", x.container[].repr, " tp: ", $(typeof(T))
|
||||
echo "SharedPtr: FREE: ", x.container.pointer.repr, " cnt: ", x.container.cnt, " tp: ", $(typeof(T))
|
||||
when compiles(`=destroy`(x[])):
|
||||
echo "DECR FREE: ", $(typeof(x[]))
|
||||
echo "SharedPtr:call:child:destructor: ", $(typeof(x[]))
|
||||
`=destroy`(x[])
|
||||
deallocShared(x.container)
|
||||
x.container = nil
|
||||
else:
|
||||
echo "SharedPtr: decr: ", x.container[].repr, " tp: ", $(typeof(T))
|
||||
echo "SharedPtr: decr: ", x.container.pointer.repr, " cnt: ", x.container.cnt, " tp: ", $(typeof(T))
|
||||
|
||||
proc release*[T](x: var SharedPtr[T]) =
|
||||
echo "SharedPtr: release: ", $(typeof(T))
|
||||
@ -47,7 +47,7 @@ proc release*[T](x: var SharedPtr[T]) =
|
||||
|
||||
proc `=destroy`*[T](x: var SharedPtr[T]) =
|
||||
if x.container != nil:
|
||||
echo "SharedPtr: destroy: ", x.container[].repr, " tp: ", $(typeof(T))
|
||||
echo "SharedPtr: destroy: ", x.container.pointer.repr, " cnt: ", x.container.cnt, " tp: ", $(typeof(T)), " thr: ", $getThreadId()
|
||||
decr(x)
|
||||
|
||||
proc `=dup`*[T](src: SharedPtr[T]): SharedPtr[T] =
|
||||
@ -69,7 +69,7 @@ proc newSharedPtr*[T](val: sink Isolated[T]): SharedPtr[T] {.nodestroy.} =
|
||||
result.container = cast[typeof(result.container)](allocShared(sizeof(result.container[])))
|
||||
result.container.cnt = 1
|
||||
result.container.value = extract val
|
||||
echo "SharedPtr: alloc: ", result.container[].repr, " tp: ", $(typeof(T))
|
||||
echo "SharedPtr: alloc: ", result.container.pointer.repr, " cnt: ", result.container.cnt, " tp: ", $(typeof(T))
|
||||
|
||||
template newSharedPtr*[T](val: T): SharedPtr[T] =
|
||||
newSharedPtr(isolate(val))
|
||||
@ -78,11 +78,14 @@ proc newSharedPtr*[T](t: typedesc[T]): SharedPtr[T] =
|
||||
## Returns a shared pointer. It is not initialized,
|
||||
result.container = cast[typeof(result.container)](allocShared0(sizeof(result.container[])))
|
||||
result.container.cnt = 1
|
||||
echo "SharedPtr: alloc: ", result.container[].repr, " tp: ", $(typeof(T))
|
||||
echo "SharedPtr: alloc: ", result.container.pointer.repr, " cnt: ", result.container.cnt, " tp: ", $(typeof(T))
|
||||
|
||||
proc isNil*[T](p: SharedPtr[T]): bool {.inline.} =
|
||||
p.container == nil
|
||||
|
||||
proc unsafeRawPtr*[T](p: SharedPtr[T]): pointer {.inline.} =
|
||||
p.container.pointer
|
||||
|
||||
proc `[]`*[T](p: SharedPtr[T]): var T {.inline.} =
|
||||
checkNotNil(p)
|
||||
p.container.value
|
||||
@ -95,5 +98,5 @@ template `[]=`*[T](p: SharedPtr[T]; val: T) =
|
||||
`[]=`(p, isolate(val))
|
||||
|
||||
proc `$`*[T](p: SharedPtr[T]): string {.inline.} =
|
||||
if p.container == nil: "nil"
|
||||
else: $p.container[]
|
||||
if p.container == nil: "nil\"\""
|
||||
else: p.container.pointer.repr & "\"" & $p.container[] & "\""
|
||||
|
||||
@ -128,7 +128,10 @@ proc putTask*(
|
||||
db: DataBuffer,
|
||||
) =
|
||||
|
||||
# var ret = ret
|
||||
var ret = ret
|
||||
echo "putTask: ", $getThreadId()
|
||||
echo "putTask:kb: ", kb.toString
|
||||
echo "putTask:db: ", db.toString
|
||||
|
||||
without key =? kb.toKey(), err:
|
||||
ret.failure(err)
|
||||
@ -142,7 +145,8 @@ proc putTask*(
|
||||
ret.success()
|
||||
|
||||
discard ret.fireSync()
|
||||
# ret.release()
|
||||
ret.release()
|
||||
echo "putTask: FINISH\n"
|
||||
|
||||
proc put*(
|
||||
ret: TResult[void],
|
||||
@ -150,9 +154,11 @@ proc put*(
|
||||
key: Key,
|
||||
data: seq[byte]
|
||||
) =
|
||||
echo "put request args: ", $getThreadId()
|
||||
let bkey = StringBuffer.new(key.id())
|
||||
let bval = DataBuffer.new(data)
|
||||
|
||||
echo "spawn put request: ", $getThreadId()
|
||||
tds[].tp.spawn putTask(ret, tds, bkey, bval)
|
||||
|
||||
|
||||
|
||||
@ -64,12 +64,13 @@ suite "Share buffer test":
|
||||
|
||||
test "basics":
|
||||
echo "a1: ", $a1
|
||||
check $a1 == "nil"
|
||||
check $a1 == "nil\"\""
|
||||
check a1.isNil
|
||||
check $a2 == "(value: 0, cnt: 2)"
|
||||
# check $a2 == "(value: 0, cnt: 2)"
|
||||
check split($(a2),'"')[1] == "(value: 0, cnt: 2)"
|
||||
check not a2.isNil
|
||||
check a2[] == 0
|
||||
check $a3 == "(value: 0, cnt: 2)"
|
||||
check split($(a3),'"')[1] == "(value: 0, cnt: 2)"
|
||||
check not a3.isNil
|
||||
check a3[] == 0
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user