mirror of
https://github.com/logos-storage/nim-chronos.git
synced 2026-01-11 18:03:07 +00:00
* exception tracking This PR adds minimal exception tracking to chronos, moving the goalpost one step further. In particular, it becomes invalid to raise exceptions from `callSoon` callbacks: this is critical for writing correct error handling because there's no reasonable way that a user of chronos can possibly _reason_ about exceptions coming out of there: the event loop will be in an indeterminite state when the loop is executing an _random_ callback. As expected, there are several issues in the error handling of chronos: in particular, it will end up in an inconsistent internal state whenever the selector loop operations fail, because the internal state update functions are not written in an exception-safe way. This PR turns this into a Defect, which probably is not the optimal way of handling things - expect more work to be done here. Some API have no way of reporting back errors to callers - for example, when something fails in the accept loop, there's not much it can do, and no way to report it back to the user of the API - this has been fixed with the new accept flow - the old one should be deprecated. Finally, there is information loss in the API: in composite operations like `poll` and `waitFor` there's no way to differentiate internal errors from user-level errors originating from callbacks. * store `CatchableError` in future * annotate proc's with correct raises information * `selectors2` to avoid non-CatchableError IOSelectorsException * `$` should never raise * remove unnecessary gcsafe annotations * fix exceptions leaking out of timer waits * fix some imports * functions must signal raising the union of all exceptions across all platforms to enable cross-platform code * switch to unittest2 * add `selectors2` which supercedes the std library version and fixes several exception handling issues in there * fixes * docs, platform-independent eh specifiers for some functions * add feature flag for strict exception mode also bump version to 3.0.0 - _most_ existing code should be compatible with this version of exception handling but some things might need fixing - callbacks, existing raises specifications etc. * fix AsyncCheck for non-void T
197 lines
6.4 KiB
Nim
197 lines
6.4 KiB
Nim
# Chronos Test Suite
|
|
# (c) Copyright 2018-Present
|
|
# Status Research & Development GmbH
|
|
#
|
|
# Licensed under either of
|
|
# Apache License, version 2.0, (LICENSE-APACHEv2)
|
|
# MIT license (LICENSE-MIT)
|
|
import unittest2
|
|
import ../chronos
|
|
|
|
when defined(nimHasUsed): {.used.}
|
|
|
|
suite "TransportAddress test suite":
|
|
test "initTAddress(string)":
|
|
check $initTAddress("0.0.0.0:1") == "0.0.0.0:1"
|
|
check $initTAddress("255.255.255.255:65535") == "255.255.255.255:65535"
|
|
check $initTAddress("[::]:1") == "[::]:1"
|
|
check $initTAddress("[FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF]:65535") ==
|
|
"[ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff]:65535"
|
|
|
|
test "initTAddress(string, Port)":
|
|
check $initTAddress("0.0.0.0", Port(0)) == "0.0.0.0:0"
|
|
check $initTAddress("255.255.255.255", Port(65535)) ==
|
|
"255.255.255.255:65535"
|
|
check $initTAddress("::", Port(0)) == "[::]:0"
|
|
check $initTAddress("FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF",
|
|
Port(65535)) ==
|
|
"[ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff]:65535"
|
|
|
|
test "initTAddress(string, int)":
|
|
check $initTAddress("0.0.0.0", 1) == "0.0.0.0:1"
|
|
check $initTAddress("255.255.255.255", 65535) ==
|
|
"255.255.255.255:65535"
|
|
check $initTAddress("::", 0) == "[::]:0"
|
|
check $initTAddress("FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF", 65535) ==
|
|
"[ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff]:65535"
|
|
|
|
test "resolveTAddress(string, IPv4)":
|
|
var numeric = ["0.0.0.0:1", "255.0.0.255:54321", "128.128.128.128:12345",
|
|
"255.255.255.255:65535"]
|
|
var hostnames = ["www.google.com:443", "www.github.com:443"]
|
|
|
|
for item in numeric:
|
|
var taseq = resolveTAddress(item)
|
|
check len(taseq) == 1
|
|
check $taseq[0] == item
|
|
|
|
for item in hostnames:
|
|
var taseq = resolveTAddress(item)
|
|
check len(taseq) >= 1
|
|
|
|
# test "resolveTAddress(string, IPv6)":
|
|
# var numeric = [
|
|
# "[::]:1",
|
|
# "[ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff]:65535",
|
|
# "[aaaa:bbbb:cccc:dddd:eeee:ffff::1111]:12345",
|
|
# "[aaaa:bbbb:cccc:dddd:eeee:ffff::]:12345",
|
|
# "[a:b:c:d:e:f::]:12345",
|
|
# "[2222:3333:4444:5555:6666:7777:8888:9999]:56789"
|
|
# ]
|
|
# var hostnames = ["localhost:443"]
|
|
|
|
# for item in numeric:
|
|
# var taseq = resolveTAddress(item, IpAddressFamily.IPv6)
|
|
# check len(taseq) == 1
|
|
# check $taseq[0] == item
|
|
|
|
# for item in hostnames:
|
|
# var taseq = resolveTAddress(item, IpAddressFamily.IPv6)
|
|
# check len(taseq) >= 1
|
|
|
|
test "resolveTAddress(string, Port, IPv4)":
|
|
var numeric = ["0.0.0.0", "255.0.0.255", "128.128.128.128",
|
|
"255.255.255.255"]
|
|
var hostnames = ["www.google.com", "www.github.com", "localhost"]
|
|
|
|
for item in numeric:
|
|
var taseq = resolveTAddress(item, Port(443))
|
|
check len(taseq) == 1
|
|
check $taseq[0] == item & ":443"
|
|
|
|
for item in hostnames:
|
|
var taseq = resolveTAddress(item, Port(443))
|
|
check len(taseq) >= 1
|
|
|
|
# test "resolveTAddress(string, Port, IPv6)":
|
|
# var numeric = [
|
|
# "::",
|
|
# "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff",
|
|
# "aaaa:bbbb:cccc:dddd:eeee:ffff::1111",
|
|
# "aaaa:bbbb:cccc:dddd:eeee:ffff::",
|
|
# "a:b:c:d:e:f::",
|
|
# "2222:3333:4444:5555:6666:7777:8888:9999"
|
|
# ]
|
|
# var hostnames = ["localhost"]
|
|
# for item in numeric:
|
|
# var taseq = resolveTAddress(item, Port(443), IpAddressFamily.IPv6)
|
|
# check len(taseq) == 1
|
|
# check $taseq[0] == "[" & item & "]:443"
|
|
|
|
# for item in hostnames:
|
|
# var taseq = resolveTAddress(item, Port(443), IpAddressFamily.IPv6)
|
|
# check len(taseq) >= 1
|
|
|
|
test "Faulty initTAddress(string)":
|
|
var tests = [
|
|
"z:1",
|
|
"256.256.256.256:65534",
|
|
"127.0.0.1:65536"
|
|
]
|
|
var errcounter = 0
|
|
for item in tests:
|
|
try:
|
|
discard initTAddress(item)
|
|
except TransportAddressError:
|
|
inc(errcounter)
|
|
check errcounter == len(tests)
|
|
|
|
test "Faulty initTAddress(string, Port)":
|
|
var tests = [
|
|
":::",
|
|
"999.999.999.999",
|
|
"gggg:aaaa:bbbb:gggg:aaaa:bbbb:gggg:aaaa",
|
|
"hostname"
|
|
]
|
|
var errcounter = 0
|
|
for item in tests:
|
|
try:
|
|
discard initTAddress(item, Port(443))
|
|
except TransportAddressError:
|
|
inc(errcounter)
|
|
check errcounter == len(tests)
|
|
|
|
test "Faulty initTAddress(string, Port)":
|
|
var errcounter = 0
|
|
try:
|
|
discard initTAddress("127.0.0.1", 100000)
|
|
except TransportAddressError:
|
|
inc(errcounter)
|
|
check errcounter == 1
|
|
|
|
test "Faulty resolveTAddress(string, IPv4) for IPv6 address":
|
|
var numeric = [
|
|
"[::]:1",
|
|
"[ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff]:65535",
|
|
"[aaaa:bbbb:cccc:dddd:eeee:ffff::1111]:12345",
|
|
"[aaaa:bbbb:cccc:dddd:eeee:ffff::]:12345",
|
|
"[a:b:c:d:e:f::]:12345",
|
|
"[2222:3333:4444:5555:6666:7777:8888:9999]:56789"
|
|
]
|
|
var errcounter = 0
|
|
for item in numeric:
|
|
try:
|
|
discard resolveTAddress(item)
|
|
except TransportAddressError:
|
|
inc(errcounter)
|
|
check errcounter == len(numeric)
|
|
|
|
test "Faulty resolveTAddress(string, Port, IPv4) for IPv6 address":
|
|
var numeric = [
|
|
"::",
|
|
"ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff",
|
|
"aaaa:bbbb:cccc:dddd:eeee:ffff::1111",
|
|
"aaaa:bbbb:cccc:dddd:eeee:ffff::",
|
|
"a:b:c:d:e:f::",
|
|
"2222:3333:4444:5555:6666:7777:8888:9999"
|
|
]
|
|
var errcounter = 0
|
|
for item in numeric:
|
|
try:
|
|
discard resolveTAddress(item, Port(443))
|
|
except TransportAddressError:
|
|
inc(errcounter)
|
|
check errcounter == len(numeric)
|
|
|
|
# test "Faulty resolveTAddress(string, IPv6) for IPv4 address":
|
|
# var numeric = ["0.0.0.0:0", "255.0.0.255:54321", "128.128.128.128:12345",
|
|
# "255.255.255.255:65535"]
|
|
# var errcounter = 0
|
|
# for item in numeric:
|
|
# try:
|
|
# var taseq = resolveTAddress(item, IpAddressFamily.IPv6)
|
|
# except TransportAddressError:
|
|
# inc(errcounter)
|
|
# check errcounter == len(numeric)
|
|
|
|
# test "Faulty resolveTAddress(string, Port, IPv6) for IPv4 address":
|
|
# var numeric = ["0.0.0.0", "255.0.0.255", "128.128.128.128",
|
|
# "255.255.255.255"]
|
|
# var errcounter = 0
|
|
# for item in numeric:
|
|
# try:
|
|
# var taseq = resolveTAddress(item, Port(443), IpAddressFamily.IPv6)
|
|
# except TransportAddressError:
|
|
# inc(errcounter)
|
|
# check errcounter == len(numeric)
|