mirror of
https://github.com/nim-lang/Nim.git
synced 2026-02-17 08:34:20 +00:00
Add pending operations presence check function, fixes #5155
This commit is contained in:
@@ -254,8 +254,14 @@ when defined(windows) or defined(nimdoc):
|
||||
"Operation performed on a socket which has not been registered with" &
|
||||
" the dispatcher yet.")
|
||||
|
||||
proc hasPendingOperations*(): bool =
|
||||
## Returns `true` if the global dispatcher has pending operations.
|
||||
let p = getGlobalDispatcher()
|
||||
p.handles.len != 0 or p.timers.len != 0 or p.callbacks.len != 0
|
||||
|
||||
proc poll*(timeout = 500) =
|
||||
## Waits for completion events and processes them.
|
||||
## Waits for completion events and processes them. Raises ``ValueError``
|
||||
## if there are no pending operations.
|
||||
let p = getGlobalDispatcher()
|
||||
if p.handles.len == 0 and p.timers.len == 0 and p.callbacks.len == 0:
|
||||
raise newException(ValueError,
|
||||
@@ -1056,8 +1062,15 @@ else:
|
||||
newCBs.add(cb)
|
||||
callbacks = newCBs & callbacks
|
||||
|
||||
proc hasPendingOperations*(): bool =
|
||||
let p = getGlobalDispatcher()
|
||||
p.selector.len != 0 or p.timers.len != 0 or p.callbacks.len != 0
|
||||
|
||||
proc poll*(timeout = 500) =
|
||||
let p = getGlobalDispatcher()
|
||||
if p.selector.len == 0 and p.timers.len == 0 and p.callbacks.len == 0:
|
||||
raise newException(ValueError,
|
||||
"No handles or timers registered in dispatcher.")
|
||||
|
||||
if p.selector.len > 0:
|
||||
for info in p.selector.select(p.adjustedTimeout(timeout)):
|
||||
|
||||
@@ -231,8 +231,14 @@ when defined(windows) or defined(nimdoc):
|
||||
"Operation performed on a socket which has not been registered with" &
|
||||
" the dispatcher yet.")
|
||||
|
||||
proc hasPendingOperations*(): bool =
|
||||
## Returns `true` if the global dispatcher has pending operations.
|
||||
let p = getGlobalDispatcher()
|
||||
p.handles.len != 0 or p.timers.len != 0 or p.callbacks.len != 0
|
||||
|
||||
proc poll*(timeout = 500) =
|
||||
## Waits for completion events and processes them.
|
||||
## Waits for completion events and processes them. Raises ``ValueError``
|
||||
## if there are no pending operations.
|
||||
let p = getGlobalDispatcher()
|
||||
if p.handles.len == 0 and p.timers.len == 0 and p.callbacks.len == 0:
|
||||
raise newException(ValueError,
|
||||
@@ -1182,6 +1188,10 @@ else:
|
||||
raise newException(ValueError, "File descriptor not registered.")
|
||||
p.selector.updateHandle(fd.SocketHandle, newEvents)
|
||||
|
||||
proc hasPendingOperations*(): bool =
|
||||
let p = getGlobalDispatcher()
|
||||
not p.selector.isEmpty() or p.timers.len != 0 or p.callbacks.len != 0
|
||||
|
||||
proc poll*(timeout = 500) =
|
||||
var keys: array[64, ReadyKey[AsyncData]]
|
||||
|
||||
|
||||
21
tests/async/tpendingcheck.nim
Normal file
21
tests/async/tpendingcheck.nim
Normal file
@@ -0,0 +1,21 @@
|
||||
discard """
|
||||
file: "tpendingcheck.nim"
|
||||
exitcode: 0
|
||||
output: ""
|
||||
"""
|
||||
|
||||
import asyncdispatch
|
||||
|
||||
doAssert(not hasPendingOperations())
|
||||
|
||||
proc test() {.async.} =
|
||||
await sleepAsync(100)
|
||||
|
||||
var f = test()
|
||||
while not f.finished:
|
||||
doAssert(hasPendingOperations())
|
||||
poll(10)
|
||||
f.read
|
||||
|
||||
doAssert(not hasPendingOperations())
|
||||
|
||||
Reference in New Issue
Block a user