Skip to content

Commit

Permalink
Upgrade trackers (#53)
Browse files Browse the repository at this point in the history
* Update presto to follow recent changes in chronos.

* Bump to reset CI testing.

* Bump requirements.

* Use chronos@head instead of specific version.

* Use proper delimiter to specify HEAD.
  • Loading branch information
cheatfate authored Jul 17, 2023
1 parent 35652ed commit 4255261
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 101 deletions.
2 changes: 1 addition & 1 deletion presto.nimble
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ license = "MIT"
skipDirs = @["tests", "examples"]

requires "nim >= 1.6.0",
"chronos >= 3.0.9",
"chronos#head",
"chronicles",
"stew"

Expand Down
3 changes: 0 additions & 3 deletions presto/route.nim
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
# Licensed under either of
# Apache License, version 2.0, (LICENSE-APACHEv2)
# MIT license (LICENSE-MIT)

import std/[macros, options]
import chronos, chronos/apps/http/[httpcommon, httptable, httpclient]
import httputils
Expand Down Expand Up @@ -84,8 +83,6 @@ proc addRoute*(rr: var RestRouter, request: HttpMethod, path: string,
let
optionsPath = SegmentedPath.init(
MethodOptions, path, rr.patternCallback)
optionsRoute = rr.routes.getOrDefault(
optionsPath, RestRouteItem(kind: RestRouteKind.None))
case route.kind
of RestRouteKind.None:
let optionsHandler = RestRouteItem(kind: RestRouteKind.Handler,
Expand Down
6 changes: 3 additions & 3 deletions presto/serverprivate.nim
Original file line number Diff line number Diff line change
Expand Up @@ -209,13 +209,13 @@ proc processRestRequest*[T](server: T,
debug "Critical error occured while sending response",
meth = $request.meth, peer = $request.remoteAddress(),
uri = $request.uri, code = exc.code, error_msg = $exc.msg
return dumbResponse()
return defaultResponse()
except CatchableError as exc:
warn "Unexpected error occured while sending response",
meth = $request.meth, peer = $request.remoteAddress(),
uri = $request.uri, error_msg = $exc.msg,
error_name = $exc.name
return dumbResponse()
return defaultResponse()
else:
debug "Request is not part of API", peer = $request.remoteAddress(),
meth = $request.meth, uri = $request.uri
Expand All @@ -233,4 +233,4 @@ proc processRestRequest*[T](server: T,
debug "Remote peer dropped connection", peer = $httpErr.remote,
reason = $httpErr.error, code = $httpErr.code

return dumbResponse()
return defaultResponse()
18 changes: 3 additions & 15 deletions tests/testclient.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1318,7 +1318,8 @@ suite "REST API client test suite":
contentBody: Option[ContentBody]) -> RestApiResponse:
return RestApiResponse.response("ok")

var sres = RestServerRef.new(router, serverAddress)
var sres = RestServerRef.new(router, serverAddress,
serverFlags = {HttpServerFlags.Http11Pipeline})
let server = sres.get()
server.start()

Expand Down Expand Up @@ -1398,17 +1399,4 @@ suite "REST API client test suite":
await server.closeWait()

test "Leaks test":
proc getTrackerLeaks(tracker: string): bool =
let tracker = getTracker(tracker)
if isNil(tracker): false else: tracker.isLeaked()

check:
getTrackerLeaks("http.body.reader") == false
getTrackerLeaks("http.body.writer") == false
getTrackerLeaks("httpclient.connection") == false
getTrackerLeaks("httpclient.request") == false
getTrackerLeaks("httpclient.response") == false
getTrackerLeaks("async.stream.reader") == false
getTrackerLeaks("async.stream.writer") == false
getTrackerLeaks("stream.server") == false
getTrackerLeaks("stream.transport") == false
checkLeaks()
64 changes: 32 additions & 32 deletions tests/testroute.nim
Original file line number Diff line number Diff line change
Expand Up @@ -409,83 +409,83 @@ suite "REST API router & macro tests":
router.addRedirect(MethodPost, "/redirect/path/{pattern2}",
"/unique/path/{pattern2")

expect AssertionError:
expect Defect:
router.addRoute(MethodGet, "/unique/path/1", apiCallback)
expect AssertionError:
expect Defect:
router.addRoute(MethodGet, "/unique/path/2", apiCallback)
expect AssertionError:
expect Defect:
router.addRoute(MethodGet, "/unique/path/{pattern1}", apiCallback)
expect AssertionError:
expect Defect:
router.addRoute(MethodGet, "/unique/path/{pattern2}", apiCallback)

expect AssertionError:
expect Defect:
router.addRoute(MethodGet, "/redirect/path/1", apiCallback)
expect AssertionError:
expect Defect:
router.addRoute(MethodGet, "/redirect/path/2", apiCallback)
expect AssertionError:
expect Defect:
router.addRoute(MethodGet, "/redirect/path/{pattern1}", apiCallback)
expect AssertionError:
expect Defect:
router.addRoute(MethodGet, "/redirect/path/{pattern2}", apiCallback)

expect AssertionError:
expect Defect:
router.addRoute(MethodPost, "/unique/path/1", apiCallback)
expect AssertionError:
expect Defect:
router.addRoute(MethodPost, "/unique/path/2", apiCallback)
expect AssertionError:
expect Defect:
router.addRoute(MethodPost, "/unique/path/{pattern1}", apiCallback)
expect AssertionError:
expect Defect:
router.addRoute(MethodPost, "/unique/path/{pattern2}", apiCallback)

expect AssertionError:
expect Defect:
router.addRoute(MethodPost, "/redirect/path/1", apiCallback)
expect AssertionError:
expect Defect:
router.addRoute(MethodPost, "/redirect/path/2", apiCallback)
expect AssertionError:
expect Defect:
router.addRoute(MethodPost, "/redirect/path/{pattern1}", apiCallback)
expect AssertionError:
expect Defect:
router.addRoute(MethodPost, "/redirect/path/{pattern2}", apiCallback)

expect AssertionError:
expect Defect:
router.addRedirect(MethodGet, "/unique/path/1", "/unique/1")
expect AssertionError:
expect Defect:
router.addRedirect(MethodGet, "/unique/path/2", "/unique/2")
expect AssertionError:
expect Defect:
router.addRedirect(MethodGet, "/unique/path/{pattern1}",
"/unique/{pattern1}")
expect AssertionError:
expect Defect:
router.addRedirect(MethodGet, "/unique/path/{pattern2}",
"/unique/{pattern2}")

expect AssertionError:
expect Defect:
router.addRedirect(MethodGet, "/redirect/path/1", "/another/1")
expect AssertionError:
expect Defect:
router.addRedirect(MethodGet, "/redirect/path/2", "/another/2")
expect AssertionError:
expect Defect:
router.addRedirect(MethodGet, "/redirect/path/{pattern1}",
"/another/{pattern1}")
expect AssertionError:
expect Defect:
router.addRedirect(MethodGet, "/redirect/path/{pattern2}",
"/another/{pattern2}")

expect AssertionError:
expect Defect:
router.addRedirect(MethodPost, "/unique/path/1", "/unique/1")
expect AssertionError:
expect Defect:
router.addRedirect(MethodPost, "/unique/path/2", "/unique/2")
expect AssertionError:
expect Defect:
router.addRedirect(MethodPost, "/unique/path/{pattern1}",
"/unique/{pattern1}")
expect AssertionError:
expect Defect:
router.addRedirect(MethodPost, "/unique/path/{pattern2}",
"/unique/{pattern2}")

expect AssertionError:
expect Defect:
router.addRedirect(MethodPost, "/redirect/path/1", "/another/1")
expect AssertionError:
expect Defect:
router.addRedirect(MethodPost, "/redirect/path/2", "/another/2")
expect AssertionError:
expect Defect:
router.addRedirect(MethodPost, "/redirect/path/{pattern1}",
"/another/{pattern1}")
expect AssertionError:
expect Defect:
router.addRedirect(MethodPost, "/redirect/path/{pattern2}",
"/another/{pattern2}")

Expand Down
58 changes: 24 additions & 34 deletions tests/testsecureserver.nim
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import std/[unittest, strutils]
import std/strutils
import helpers
import chronos, chronos/apps
import chronos, chronos/apps, chronos/unittest2/asynctests
import stew/byteutils
import ../presto/route, ../presto/segpath, ../presto/secureserver

Expand Down Expand Up @@ -108,7 +108,7 @@ proc httpsClient(server: TransportAddress, meth: HttpMethod, url: string,
ClientResponse(status: resp.code, data: dataBuf)
else:
ClientResponse(status: resp.code, data: "")
return cresp
cresp
finally:
if not(isNil(tlsstream)):
await allFutures(tlsstream.reader.closeWait(),
Expand All @@ -125,13 +125,6 @@ proc createServer(address: TransportAddress,
secureCert)
sres.get()

template asyncTest*(name: string, body: untyped): untyped =
test name:
waitFor((
proc() {.async, gcsafe.} =
body
)())

suite "Secure REST API server test suite":
let serverAddress = initTAddress("127.0.0.1:30180")
asyncTest "Responses test":
Expand All @@ -152,25 +145,26 @@ suite "Secure REST API server test suite":
let server = createServer(serverAddress, router)
server.start()
try:
# Handler returned empty response.
let res1 = await httpsClient(serverAddress, MethodGet, "/test/simple/1",
"")
# Handler returned good response.
let res2 = await httpsClient(serverAddress, MethodGet, "/test/simple/2",
"")
# Handler returned via RestApiResponse.
let res3 = await httpsClient(serverAddress, MethodGet, "/test/simple/3",
"")
# Exception generated by handler.
let res4 = await httpsClient(serverAddress, MethodGet, "/test/simple/4",
"")
# Missing handler response
let res5 = await httpsClient(serverAddress, MethodGet, "/test/simple/5",
"")
# URI with more than 64 segments response
let res6 = await httpsClient(serverAddress, MethodGet,
"//////////////////////////////////////////" &
"//////////////////////////test", "")
let
# Handler returned empty response.
res1 = await httpsClient(serverAddress, MethodGet, "/test/simple/1",
"")
# Handler returned good response.
res2 = await httpsClient(serverAddress, MethodGet, "/test/simple/2",
"")
# Handler returned via RestApiResponse.
res3 = await httpsClient(serverAddress, MethodGet, "/test/simple/3",
"")
# Exception generated by handler.
res4 = await httpsClient(serverAddress, MethodGet, "/test/simple/4",
"")
# Missing handler response
res5 = await httpsClient(serverAddress, MethodGet, "/test/simple/5",
"")
# URI with more than 64 segments response
res6 = await httpsClient(serverAddress, MethodGet,
"//////////////////////////////////////////" &
"//////////////////////////test", "")
check:
res1 == ClientResponse(status: 410)
res2 == ClientResponse(status: 200, data: "ok-1")
Expand Down Expand Up @@ -607,8 +601,4 @@ suite "Secure REST API server test suite":
await server.closeWait()

test "Leaks test":
check:
getTracker("async.stream.reader").isLeaked() == false
getTracker("async.stream.writer").isLeaked() == false
getTracker("stream.server").isLeaked() == false
getTracker("stream.transport").isLeaked() == false
checkLeaks()
10 changes: 5 additions & 5 deletions tests/testsegpath.nim
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ suite "SegmentedPath test suite":
"/path1/path2/path3/{}/"
]
for item in EmptyVectors:
expect AssertionError:
expect Defect:
let path {.used.} = SegmentedPath.init(HttpMethod.MethodGet, item,
validate)
test "Too many segments path test":
for i in 63 .. 128:
expect AssertionError:
expect Defect:
let path {.used.} = SegmentedPath.init(createPatternsOnly(i), validate)
expect AssertionError:
expect Defect:
let path {.used.} = SegmentedPath.init(createPathOnly(i), validate)

let rpath1 = SegmentedPath.init(createPathOnly(i))
Expand All @@ -61,7 +61,7 @@ suite "SegmentedPath test suite":
"/{a}/{b}/{a}"
]
for item in NonUniqueVectors:
expect AssertionError:
expect Defect:
let path {.used.} = SegmentedPath.init(HttpMethod.MethodGet, item,
validate)
test "Url-encoded path test":
Expand Down Expand Up @@ -113,5 +113,5 @@ suite "SegmentedPath test suite":
check createPath(item[0], item[1]) == item[2]

for item in BadVectors:
expect AssertionError:
expect Defect:
let path {.used.} = createPath(item[0], item[1])
11 changes: 3 additions & 8 deletions tests/testserver.nim
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import std/[unittest, strutils, algorithm]
import std/[strutils, algorithm]
import helpers
import chronos, chronos/apps
import chronos, chronos/apps, chronos/unittest2/asynctests
import stew/byteutils
import ../presto/route, ../presto/segpath, ../presto/server

Expand Down Expand Up @@ -884,7 +884,6 @@ suite "REST API server test suite":
var router = RestRouter.init(testValidate)
router.rawApi(MethodPost, "/test/post") do () -> RestApiResponse:
let contentType = request.headers.getString(ContentTypeHeader)
let acceptType = request.headers.getString(AcceptHeaderName)
let body = await request.getBody()
return
RestApiResponse.response(
Expand All @@ -903,8 +902,4 @@ suite "REST API server test suite":
await server.closeWait()

test "Leaks test":
check:
getTracker("async.stream.reader").isLeaked() == false
getTracker("async.stream.writer").isLeaked() == false
getTracker("stream.server").isLeaked() == false
getTracker("stream.transport").isLeaked() == false
checkLeaks()

0 comments on commit 4255261

Please sign in to comment.