Skip to content

Commit

Permalink
ensure to setup the event loop
Browse files Browse the repository at this point in the history
  • Loading branch information
nitely committed Feb 2, 2025
1 parent 050bdcc commit 351092d
Show file tree
Hide file tree
Showing 6 changed files with 6 additions and 17 deletions.
1 change: 1 addition & 0 deletions src/hyperx/clientserver.nim
Original file line number Diff line number Diff line change
Expand Up @@ -734,6 +734,7 @@ proc failSilently(f: Future[void]) {.async.} =
debugErr getCurrentException()

template with*(client: ClientContext, body: untyped): untyped =
discard getGlobalDispatcher() # setup event loop
doAssert not client.isConnected
var recvFut, dispFut, winupFut: Future[void] = nil
try:
Expand Down
18 changes: 1 addition & 17 deletions src/hyperx/limiter.nim
Original file line number Diff line number Diff line change
Expand Up @@ -83,28 +83,12 @@ proc join*(lt: LimiterAsync) {.async.} =
await lt.wait()

when isMainModule:
discard getGlobalDispatcher()
proc sleepCycle: Future[void] =
let fut = newFuture[void]()
proc wakeup = fut.complete()
callSoon wakeup
return fut
block:
proc test {.async.} =
let lt = newLimiter(1)
var puts = newSeq[int]()
proc putOne(i: int) {.async.} =
puts.add i
dec lt
for i in 1 .. 6:
inc lt
asyncCheck putOne(i)
if lt.isFull:
await lt.wait()
doAssert puts.len == i
doAssert lt.used <= 1
doAssert puts == @[1,2,3,4,5,6]
waitFor test()
doAssert not hasPendingOperations()
block:
proc test {.async.} =
let lt = newLimiter(1)
Expand Down
1 change: 1 addition & 0 deletions src/hyperx/queue.nim
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ proc close*[T](q: QueueAsync[T]) {.raises: [].} =
failSoon q.popWaiter

when isMainModule:
discard getGlobalDispatcher()
proc sleepCycle: Future[void] =
let fut = newFuture[void]()
proc wakeup = fut.complete()
Expand Down
1 change: 1 addition & 0 deletions src/hyperx/server.nim
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ proc recvClient*(server: ServerContext): Future[ClientContext] {.async.} =
return newClient(ctServer, sock, server.hostname)

template with*(server: ServerContext, body: untyped): untyped =
discard getGlobalDispatcher() # setup event loop
try:
server.isConnected = true
server.listen()
Expand Down
1 change: 1 addition & 0 deletions src/hyperx/signal.nim
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ proc close*(sig: SignalAsync) {.raises: [].} =
failSoon sig.waiters.popLast()

when isMainModule:
discard getGlobalDispatcher()
block:
proc test() {.async.} =
var sig = newSignal()
Expand Down
1 change: 1 addition & 0 deletions src/hyperx/value.nim
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ proc close*[T](vala: ValueAsync[T]) {.raises: [].} =
failSoon vala.getWaiter

when isMainModule:
discard getGlobalDispatcher()
func newIntRef(n: int): ref int =
new result
result[] = n
Expand Down

0 comments on commit 351092d

Please sign in to comment.