Skip to content

Commit

Permalink
Reduce compiler warnings when using new chronos
Browse files Browse the repository at this point in the history
  • Loading branch information
jangko committed Jan 22, 2024
1 parent f8ed9b4 commit 0a6a4cd
Show file tree
Hide file tree
Showing 14 changed files with 97 additions and 63 deletions.
2 changes: 1 addition & 1 deletion tests/extensions/base64ext.nim
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ proc base64Factory*(padding: bool): ExtFactory =

proc factory(isServer: bool,
args: seq[ExtParam]): Result[Ext, string] {.
gcsafe, raises: [Defect].} =
gcsafe, raises: [].} =

# you can capture configuration variables via closure
# if you want
Expand Down
2 changes: 1 addition & 1 deletion tests/extensions/hexext.nim
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ proc hexFactory*(): ExtFactory =

proc factory(isServer: bool,
args: seq[ExtParam]): Result[Ext, string] {.
gcsafe, raises: [Defect].} =
gcsafe, raises: [].} =

# you can capture configuration variables via closure
# if you want
Expand Down
10 changes: 6 additions & 4 deletions tests/helpers.nim
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@ import pkg/[
import ../websock/websock
import ./keys

{.push gcsafe, raises: [].}

const WSPath* = when defined secure: "/wss" else: "/ws"

proc rndStr*(size: int): string =
proc rndStr*(size: int): string {.gcsafe, raises: [].} =
for _ in 0..<size:
add(result, char(rand(int('A') .. int('z'))))

proc rndBin*(size: int): seq[byte] =
proc rndBin*(size: int): seq[byte] {.gcsafe, raises: [].} =
for _ in 0..<size:
add(result, byte(rand(0 .. 255)))

Expand All @@ -45,7 +47,7 @@ proc createServer*(
tlsFlags: set[TLSFlags] = {},
tlsMinVersion = TLSVersion.TLS12,
tlsMaxVersion = TLSVersion.TLS12): HttpServer
{.raises: [Defect, HttpError].} =
{.raises: [].} =
try:
let server = when defined secure:
TlsHttpServer.create(
Expand All @@ -62,7 +64,7 @@ proc createServer*(
flags = flags)

when defined accepts:
proc accepts() {.async, raises: [Defect].} =
proc accepts() {.async, raises: [].} =
try:
let req = await server.accept()
await req.handler()
Expand Down
5 changes: 2 additions & 3 deletions tests/testframes.nim
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
## those terms.

import
pkg/stew/byteutils,
pkg/chronos/unittest2/asynctests

include ../websock/frame
Expand All @@ -17,7 +16,7 @@ include ../websock/frame

suite "Test data frames":
setup:
var maskKey: array[4, char]
var maskKey {.used.} : array[4, char]

asyncTest "# 7bit length text":
check (await Frame(
Expand Down Expand Up @@ -254,7 +253,7 @@ suite "Test data frames":

suite "Test control frames":
setup:
var maskKey: array[4, char]
var maskKey {.used.} : array[4, char]

asyncTest "Close":
check (await Frame(
Expand Down
41 changes: 22 additions & 19 deletions tests/testhooks.nim
Original file line number Diff line number Diff line change
Expand Up @@ -25,30 +25,30 @@ type
request: HttpRequest

proc clientAppendGoodToken(ctx: Hook, headers: var HttpTable):
Result[void, string] {.gcsafe, raises: [Defect].} =
Result[void, string] {.gcsafe, raises: [].} =
headers.add("auth-token", "good-token")
return ok()

proc clientAppendBadToken(ctx: Hook, headers: var HttpTable):
Result[void, string] {.gcsafe, raises: [Defect].} =
Result[void, string] {.gcsafe, raises: [].} =
headers.add("auth-token", "bad-token")
return ok()

proc clientVerify(ctx: Hook, headers: HttpTable):
Future[Result[void, string]] {.async, gcsafe, raises: [Defect].} =
Future[Result[void, string]] {.gcsafe, async: (raises: []).} =
var p = TokenHook(ctx)
p.token = headers.getString("auth-status")
return ok()

proc serverVerify(ctx: Hook, headers: HttpTable):
Future[Result[void, string]] {.async, gcsafe, raises: [Defect].} =
Future[Result[void, string]] {.gcsafe, async: (raises: []).} =
var p = TokenHook(ctx)
if headers.getString("auth-token") == "good-token":
p.status = 101
return ok()

proc serverAppend(ctx: Hook, headers: var HttpTable):
Result[void, string] {.gcsafe, raises: [Defect].} =
Result[void, string] {.gcsafe, raises: [].} =
var p = TokenHook(ctx)
if p.status == 101:
headers.add("auth-status", "accept")
Expand Down Expand Up @@ -76,14 +76,17 @@ proc serverHook(): Hook =
)

proc serverVerifyWithCode(ctx: Hook, headers: HttpTable):
Future[Result[void, string]] {.async, gcsafe, raises: [Defect].} =
var p = TokenHook(ctx)
if headers.getString("auth-token") == "good-token":
p.status = 101
return ok()
else:
await p.request.stream.writer.sendError(Http401)
return err("authentication error")
Future[Result[void, string]] {.gcsafe, async: (raises: []).} =
try:
var p = TokenHook(ctx)
if headers.getString("auth-token") == "good-token":
p.status = 101
return ok()
else:
await p.request.stream.writer.sendError(Http401)
return err("authentication error")
except CatchableError as exc:
return err(exc.msg)

proc serverHookWithCode(request: HttpRequest): Hook =
TokenHook(
Expand All @@ -96,8 +99,8 @@ suite "Test Hooks":
setup:
var
server: HttpServer
goodCP = goodClientHook()
badCP = badClientHook()
goodCP {.used.} = goodClientHook()
badCP {.used.} = badClientHook()

teardown:
if server != nil:
Expand All @@ -109,7 +112,7 @@ suite "Test Hooks":
check request.uri.path == WSPath
let
server = WSServer.new()
ws = await server.handleRequest(
discard await server.handleRequest(
request,
hooks = @[serverHook()]
)
Expand All @@ -133,7 +136,7 @@ suite "Test Hooks":
check request.uri.path == WSPath
let
server = WSServer.new()
ws = await server.handleRequest(
discard await server.handleRequest(
request,
hooks = @[serverHook()]
)
Expand All @@ -157,7 +160,7 @@ suite "Test Hooks":
check request.uri.path == WSPath
let
server = WSServer.new()
ws = await server.handleRequest(
discard await server.handleRequest(
request,
hooks = @[serverHookWithCode(request)]
)
Expand All @@ -181,7 +184,7 @@ suite "Test Hooks":
check request.uri.path == WSPath
let
server = WSServer.new()
ws = await server.handleRequest(
discard await server.handleRequest(
request,
hooks = @[serverHookWithCode(request)]
)
Expand Down
4 changes: 3 additions & 1 deletion tests/testutf8.nim
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ suite "UTF-8 validator in action":
check request.uri.path == "/ws"

proc onClose(status: StatusCodes, reason: string):
CloseResult {.gcsafe, raises: [Defect].} =
CloseResult {.gcsafe, raises: [].} =
try:
check status == StatusFulfilled
check reason == closeReason
Expand Down Expand Up @@ -182,6 +182,7 @@ suite "UTF-8 validator in action":

expect WSInvalidUTF8:
let data = await session.recvMsg()
discard data

asyncTest "invalid UTF-8 sequence close code":
let closeReason = "i want to close\xc0\xaf"
Expand All @@ -207,3 +208,4 @@ suite "UTF-8 validator in action":

expect WSInvalidUTF8:
let data = await session.recvMsg()
discard data
5 changes: 4 additions & 1 deletion websock/extensions/compression/deflate.nim
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ proc createParams(args: seq[ExtParam],

ok(resp)

{.warning[HoleEnumConv]:off.}
proc getWindowBits(opts: DeflateOpts, isServer: bool): ZWindowBits =
if isServer:
if opts.serverMaxWindowBits == 0:
Expand All @@ -152,6 +153,8 @@ proc getWindowBits(opts: DeflateOpts, isServer: bool): ZWindowBits =
else:
ZWindowBits(-opts.clientMaxWindowBits)

{.warning[HoleEnumConv]:on.}

proc getContextTakeover(opts: DeflateOpts, isServer: bool): bool =
if isServer:
opts.serverNoContextTakeOver
Expand Down Expand Up @@ -369,7 +372,7 @@ proc deflateFactory*(

proc factory(isServer: bool,
args: seq[ExtParam]): Result[Ext, string] {.
gcsafe, raises: [Defect].} =
gcsafe, raises: [].} =

# capture user configuration via closure
var opts = DeflateOpts(
Expand Down
2 changes: 1 addition & 1 deletion websock/frame.nim
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
## This file may not be copied, modified, or distributed except according to
## those terms.

{.push raises: [Defect].}
{.push gcsafe, raises: [].}

import pkg/[
chronos,
Expand Down
9 changes: 7 additions & 2 deletions websock/http/client.nim
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
## This file may not be copied, modified, or distributed except according to
## those terms.

{.push raises: [Defect].}
{.push gcsafe, raises: [].}

import std/[uri, strutils]
import pkg/[
Expand Down Expand Up @@ -170,13 +170,17 @@ proc connect*(
tlsMinVersion = TLSVersion.TLS12,
tlsMaxVersion = TLSVersion.TLS12,
hostName = ""): Future[T]
{.async, raises: [Defect, HttpError].} =
{.async: (raises: [CatchableError, HttpError]).} =

let wantedHostName = if hostName.len > 0:
hostName
else:
host.split(":")[0]

template used(x: typed) =
# silence unused warning
discard

let addrs = resolveTAddress(host)
for a in addrs:
try:
Expand All @@ -190,6 +194,7 @@ proc connect*(

return conn
except TransportError as exc:
used(exc)
trace "Error connecting to address", address = $a, exc = exc.msg

raise newException(HttpError,
Expand Down
2 changes: 1 addition & 1 deletion websock/http/common.nim
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
## This file may not be copied, modified, or distributed except according to
## those terms.

{.push raises: [Defect].}
{.push gcsafe, raises: [].}

import std/[uri]
import pkg/[
Expand Down
23 changes: 17 additions & 6 deletions websock/http/server.nim
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
## This file may not be copied, modified, or distributed except according to
## those terms.

{.push raises: [Defect].}
{.push gcsafe, raises: [].}

import std/uri
import pkg/[
Expand Down Expand Up @@ -43,6 +43,10 @@ type

TlsHttpServer* = HttpServer

template used(x: typed) =
# silence unused warning
discard

proc validateRequest(
stream: AsyncStreamWriter,
header: HttpRequestHeader): Future[ReqStatus] {.async.} =
Expand All @@ -69,7 +73,7 @@ proc parseRequest(
##

var buffer = newSeq[byte](MaxHttpHeadersSize)
let remoteAddr = stream.reader.tsource.remoteAddress()
let remoteAddr {.used.} = stream.reader.tsource.remoteAddress()
trace "Received connection", address = $remoteAddr
try:
let hlenfut = stream.reader.readUntil(
Expand Down Expand Up @@ -115,11 +119,12 @@ proc parseRequest(
# remote peer disconnected
trace "Remote peer disconnected", address = $remoteAddr
except TransportOsError as exc:
used(exc)
trace "Problems with networking", address = $remoteAddr, error = exc.msg

proc handleConnCb(
server: StreamServer,
transp: StreamTransport) {.async.} =
transp: StreamTransport) {.async: (raises: []).} =
var stream: AsyncStream
try:
stream = AsyncStream(
Expand All @@ -131,10 +136,15 @@ proc handleConnCb(

await httpServer.handler(request)
except CatchableError as exc:
used(exc)
debug "Exception in HttpHandler", exc = exc.msg
finally:
await stream.closeWait()

try:
await stream.closeWait()
except CatchableError as exc:
used(exc)
debug "Exception in HttpHandler closewait", exc = exc.msg

proc handleTlsConnCb(
server: StreamServer,
transp: StreamTransport) {.async.} =
Expand All @@ -158,6 +168,7 @@ proc handleTlsConnCb(

await httpServer.handler(request)
except CatchableError as exc:
used(exc)
debug "Exception in HttpHandler", exc = exc.msg
finally:
await stream.closeWait()
Expand Down Expand Up @@ -208,7 +219,7 @@ proc create*(
headersTimeout = HttpHeadersTimeout,
handshakeTimeout = 0.seconds
): HttpServer
{.raises: [Defect, CatchableError].} = # TODO: remove CatchableError
{.raises: [CatchableError].} = # TODO: remove CatchableError
## Make a new HTTP Server
##

Expand Down
Loading

0 comments on commit 0a6a4cd

Please sign in to comment.