From fc8be3f1e64529deb25debcd81eee1135cfcf37c Mon Sep 17 00:00:00 2001 From: Jaremy Creechley Date: Wed, 13 Sep 2023 13:19:38 -0700 Subject: [PATCH] fixing up tests --- datastore/threadproxyds.nim | 9 +++++++++ datastore/threads/databuffer.nim | 7 +++++-- datastore/threads/sharedptr.nim | 19 +++++++++++-------- datastore/threads/threadbackend.nim | 10 ++++++++-- tests/datastore/testsharedptr.nim | 7 ++++--- 5 files changed, 37 insertions(+), 15 deletions(-) diff --git a/datastore/threadproxyds.nim b/datastore/threadproxyds.nim index 091e572..1ceb521 100644 --- a/datastore/threadproxyds.nim +++ b/datastore/threadproxyds.nim @@ -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() diff --git a/datastore/threads/databuffer.nim b/datastore/threads/databuffer.nim index b268e57..599f2f1 100644 --- a/datastore/threads/databuffer.nim +++ b/datastore/threads/databuffer.nim @@ -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) diff --git a/datastore/threads/sharedptr.nim b/datastore/threads/sharedptr.nim index 60ef070..0cb8e53 100644 --- a/datastore/threads/sharedptr.nim +++ b/datastore/threads/sharedptr.nim @@ -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[] & "\"" diff --git a/datastore/threads/threadbackend.nim b/datastore/threads/threadbackend.nim index aba67fc..a548e58 100644 --- a/datastore/threads/threadbackend.nim +++ b/datastore/threads/threadbackend.nim @@ -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) diff --git a/tests/datastore/testsharedptr.nim b/tests/datastore/testsharedptr.nim index e1a932b..6ea5226 100644 --- a/tests/datastore/testsharedptr.nim +++ b/tests/datastore/testsharedptr.nim @@ -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