mirror of
https://github.com/codex-storage/nim-websock.git
synced 2025-02-09 01:04:19 +00:00
Handle cancellation in close (#143)
* Handle cancellation in close * Using a future instead of sleepAsync
This commit is contained in:
parent
062fd132a5
commit
3696e3f3a5
@ -170,6 +170,37 @@ suite "Test transmission":
|
|||||||
check string.fromBytes(clientRes) == testString
|
check string.fromBytes(clientRes) == testString
|
||||||
await waitForClose(session)
|
await waitForClose(session)
|
||||||
|
|
||||||
|
asyncTest "Close handle cancellation":
|
||||||
|
let testString = "Hello!"
|
||||||
|
let cancelSignal = newFuture[void]()
|
||||||
|
|
||||||
|
proc handle(request: HttpRequest) {.async.} =
|
||||||
|
check request.uri.path == WSPath
|
||||||
|
|
||||||
|
let server = WSServer.new(protos = ["proto"])
|
||||||
|
let ws = await server.handleRequest(request)
|
||||||
|
let servRes = await ws.recvMsg()
|
||||||
|
|
||||||
|
check string.fromBytes(servRes) == testString
|
||||||
|
await ws.waitForClose()
|
||||||
|
|
||||||
|
server = createServer(
|
||||||
|
address = address,
|
||||||
|
handler = handle,
|
||||||
|
flags = {ReuseAddr})
|
||||||
|
|
||||||
|
proc client() {.async, gcsafe.} =
|
||||||
|
let session = await connectClient()
|
||||||
|
await session.send(testString)
|
||||||
|
cancelSignal.complete()
|
||||||
|
expect CancelledError:
|
||||||
|
await session.close()
|
||||||
|
|
||||||
|
let task = client()
|
||||||
|
|
||||||
|
await cancelSignal
|
||||||
|
await task.cancelAndWait()
|
||||||
|
|
||||||
suite "Test ping-pong":
|
suite "Test ping-pong":
|
||||||
setup:
|
setup:
|
||||||
var
|
var
|
||||||
|
@ -518,11 +518,16 @@ proc close*(
|
|||||||
try:
|
try:
|
||||||
while ws.readyState != ReadyState.Closed:
|
while ws.readyState != ReadyState.Closed:
|
||||||
discard await ws.readFrame()
|
discard await ws.readFrame()
|
||||||
|
except CancelledError as exc:
|
||||||
|
raise exc
|
||||||
except CatchableError as exc:
|
except CatchableError as exc:
|
||||||
discard # most likely EOF
|
discard # most likely EOF
|
||||||
try:
|
try:
|
||||||
ws.readyState = ReadyState.Closing
|
ws.readyState = ReadyState.Closing
|
||||||
await gentleCloser(ws, prepareCloseBody(code, reason)).wait(10.seconds)
|
await gentleCloser(ws, prepareCloseBody(code, reason)).wait(10.seconds)
|
||||||
|
except CancelledError as exc:
|
||||||
|
trace "Cancellation when closing!", exc = exc.msg
|
||||||
|
raise exc
|
||||||
except CatchableError as exc:
|
except CatchableError as exc:
|
||||||
trace "Exception closing", exc = exc.msg
|
trace "Exception closing", exc = exc.msg
|
||||||
finally:
|
finally:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user