mirror of
https://github.com/vacp2p/nim-libp2p.git
synced 2025-03-04 01:50:36 +00:00
listenError async, add tcp/ws trasnport tests for listenError
- listenError async - add tcp/ws trasnport tests for listenError - wstransport: in start, remove unhandled addresses from self.addrs (may need to be refactored)
This commit is contained in:
parent
1bad6eeda1
commit
59aaee8354
@ -61,7 +61,7 @@ type
|
|||||||
transport*: Transport
|
transport*: Transport
|
||||||
ListenErrorCallback = proc (
|
ListenErrorCallback = proc (
|
||||||
t: Transport,
|
t: Transport,
|
||||||
err: ref TransportListenError): ref SwitchListenError
|
err: ref TransportListenError): Future[ref SwitchListenError]
|
||||||
{.gcsafe, raises: [Defect].}
|
{.gcsafe, raises: [Defect].}
|
||||||
|
|
||||||
Switch* = ref object of Dial
|
Switch* = ref object of Dial
|
||||||
@ -258,7 +258,7 @@ proc start*(s: Switch) {.async, gcsafe.} =
|
|||||||
try:
|
try:
|
||||||
await t.start(addrs)
|
await t.start(addrs)
|
||||||
except TransportListenError as e:
|
except TransportListenError as e:
|
||||||
let err = s.listenError(t, e)
|
let err = await s.listenError(t, e)
|
||||||
if not err.isNil:
|
if not err.isNil:
|
||||||
raise err
|
raise err
|
||||||
s.acceptFuts.add(s.accept(t))
|
s.acceptFuts.add(s.accept(t))
|
||||||
@ -275,7 +275,10 @@ proc newSwitchListenError*(
|
|||||||
parent: parent)
|
parent: parent)
|
||||||
|
|
||||||
const ListenErrorDefault =
|
const ListenErrorDefault =
|
||||||
proc(t: Transport, e: ref TransportListenError): ref SwitchListenError =
|
proc(
|
||||||
|
t: Transport,
|
||||||
|
e: ref TransportListenError): Future[ref SwitchListenError] {.async.}=
|
||||||
|
|
||||||
error "Failed to start one transport", error = e.msg
|
error "Failed to start one transport", error = e.msg
|
||||||
return newSwitchListenError(t, e)
|
return newSwitchListenError(t, e)
|
||||||
|
|
||||||
@ -287,7 +290,7 @@ proc newSwitch*(peerInfo: PeerInfo,
|
|||||||
connManager: ConnManager,
|
connManager: ConnManager,
|
||||||
ms: MultistreamSelect,
|
ms: MultistreamSelect,
|
||||||
nameResolver: NameResolver = nil,
|
nameResolver: NameResolver = nil,
|
||||||
listenError: ListenErrorCallback = nil): Switch
|
listenError: ListenErrorCallback = ListenErrorDefault): Switch
|
||||||
{.raises: [Defect, LPError].} =
|
{.raises: [Defect, LPError].} =
|
||||||
|
|
||||||
if secureManagers.len == 0:
|
if secureManagers.len == 0:
|
||||||
@ -303,12 +306,6 @@ proc newSwitch*(peerInfo: PeerInfo,
|
|||||||
nameResolver: nameResolver,
|
nameResolver: nameResolver,
|
||||||
listenError: listenError)
|
listenError: listenError)
|
||||||
|
|
||||||
if switch.listenError.isNil:
|
|
||||||
switch.listenError = ListenErrorDefault
|
|
||||||
# switch.listenError = proc(ma: MultiAddress, e: ref TransportListenError): ref SwitchError =
|
|
||||||
# error "Failed to start one transport", error = e.msg
|
|
||||||
# return nil
|
|
||||||
|
|
||||||
switch.connManager.peerStore = switch.peerStore
|
switch.connManager.peerStore = switch.peerStore
|
||||||
switch.mount(identity)
|
switch.mount(identity)
|
||||||
return switch
|
return switch
|
||||||
|
@ -119,16 +119,13 @@ proc new*(
|
|||||||
T: typedesc[TcpTransport],
|
T: typedesc[TcpTransport],
|
||||||
flags: set[ServerFlags] = {},
|
flags: set[ServerFlags] = {},
|
||||||
upgrade: Upgrade,
|
upgrade: Upgrade,
|
||||||
listenError: ListenErrorCallback = nil): T =
|
listenError: ListenErrorCallback = ListenErrorDefault): T =
|
||||||
|
|
||||||
let transport = T(
|
let transport = T(
|
||||||
flags: flags,
|
flags: flags,
|
||||||
upgrader: upgrade,
|
upgrader: upgrade,
|
||||||
listenError: listenError)
|
listenError: listenError)
|
||||||
|
|
||||||
if transport.listenError.isNil:
|
|
||||||
transport.listenError = ListenErrorDefault
|
|
||||||
|
|
||||||
inc getTcpTransportTracker().opened
|
inc getTcpTransportTracker().opened
|
||||||
return transport
|
return transport
|
||||||
|
|
||||||
@ -164,7 +161,7 @@ method start*(
|
|||||||
|
|
||||||
self.servers &= server
|
self.servers &= server
|
||||||
except CatchableError as ex:
|
except CatchableError as ex:
|
||||||
let err = self.listenError(ma, ex)
|
let err = await self.listenError(ma, ex)
|
||||||
if not err.isNil:
|
if not err.isNil:
|
||||||
raise err
|
raise err
|
||||||
|
|
||||||
|
@ -23,7 +23,7 @@ logScope:
|
|||||||
type
|
type
|
||||||
ListenErrorCallback* = proc (
|
ListenErrorCallback* = proc (
|
||||||
ma: MultiAddress,
|
ma: MultiAddress,
|
||||||
err: ref CatchableError): ref TransportListenError
|
err: ref CatchableError): Future[ref TransportListenError]
|
||||||
{.gcsafe, raises: [Defect].}
|
{.gcsafe, raises: [Defect].}
|
||||||
TransportError* = object of LPError
|
TransportError* = object of LPError
|
||||||
TransportInvalidAddrError* = object of TransportError
|
TransportInvalidAddrError* = object of TransportError
|
||||||
@ -41,12 +41,18 @@ proc newTransportClosedError*(parent: ref Exception = nil): ref LPError =
|
|||||||
newException(TransportClosedError,
|
newException(TransportClosedError,
|
||||||
"Transport closed, no more connections!", parent)
|
"Transport closed, no more connections!", parent)
|
||||||
|
|
||||||
proc newTransportListenError*(ma: MultiAddress, parent: ref Exception = nil): ref TransportListenError =
|
proc newTransportListenError*(
|
||||||
(ref TransportListenError)(msg: "Transport failed to start", parent: parent, ma: ma)
|
ma: MultiAddress,
|
||||||
|
parent: ref Exception = nil): ref TransportListenError =
|
||||||
|
|
||||||
|
return (ref TransportListenError)(msg: "Transport failed to start", parent: parent, ma: ma)
|
||||||
|
|
||||||
const ListenErrorDefault* =
|
const ListenErrorDefault* =
|
||||||
proc(ma: MultiAddress, err: ref CatchableError): ref TransportListenError =
|
proc(
|
||||||
newTransportListenError(ma, err)
|
ma: MultiAddress,
|
||||||
|
err: ref CatchableError): Future[ref TransportListenError] {.async.} =
|
||||||
|
|
||||||
|
return newTransportListenError(ma, err)
|
||||||
|
|
||||||
method start*(
|
method start*(
|
||||||
self: Transport,
|
self: Transport,
|
||||||
|
@ -107,6 +107,11 @@ method start*(
|
|||||||
|
|
||||||
|
|
||||||
for i, ma in addrs:
|
for i, ma in addrs:
|
||||||
|
if not self.handles(ma):
|
||||||
|
trace "Invalid address detected, skipping!", address = ma
|
||||||
|
self.addrs.del i
|
||||||
|
continue
|
||||||
|
|
||||||
let isWss =
|
let isWss =
|
||||||
if WSS.match(ma):
|
if WSS.match(ma):
|
||||||
if self.secure: true
|
if self.secure: true
|
||||||
@ -115,26 +120,32 @@ method start*(
|
|||||||
false
|
false
|
||||||
else: false
|
else: false
|
||||||
|
|
||||||
let httpserver =
|
try:
|
||||||
if isWss:
|
let httpserver =
|
||||||
TlsHttpServer.create(
|
if isWss:
|
||||||
address = ma.initTAddress().tryGet(),
|
TlsHttpServer.create(
|
||||||
tlsPrivateKey = self.tlsPrivateKey,
|
address = ma.initTAddress().tryGet(),
|
||||||
tlsCertificate = self.tlsCertificate,
|
tlsPrivateKey = self.tlsPrivateKey,
|
||||||
flags = self.flags)
|
tlsCertificate = self.tlsCertificate,
|
||||||
else:
|
flags = self.flags)
|
||||||
HttpServer.create(ma.initTAddress().tryGet())
|
else:
|
||||||
|
HttpServer.create(ma.initTAddress().tryGet())
|
||||||
|
|
||||||
self.httpservers &= httpserver
|
self.httpservers &= httpserver
|
||||||
|
|
||||||
let codec = if isWss:
|
let codec = if isWss:
|
||||||
MultiAddress.init("/wss")
|
MultiAddress.init("/wss")
|
||||||
else:
|
else:
|
||||||
MultiAddress.init("/ws")
|
MultiAddress.init("/ws")
|
||||||
|
|
||||||
# always get the resolved address in case we're bound to 0.0.0.0:0
|
# always get the resolved address in case we're bound to 0.0.0.0:0
|
||||||
self.addrs[i] = MultiAddress.init(
|
self.addrs[i] = MultiAddress.init(
|
||||||
httpserver.localAddress()).tryGet() & codec.tryGet()
|
httpserver.localAddress()).tryGet() & codec.tryGet()
|
||||||
|
|
||||||
|
except CatchableError as ex:
|
||||||
|
let err = await self.listenError(ma, ex)
|
||||||
|
if not err.isNil:
|
||||||
|
raise err
|
||||||
|
|
||||||
trace "Listening on", addresses = self.addrs
|
trace "Listening on", addresses = self.addrs
|
||||||
|
|
||||||
@ -291,7 +302,8 @@ proc new*(
|
|||||||
tlsFlags: set[TLSFlags] = {},
|
tlsFlags: set[TLSFlags] = {},
|
||||||
flags: set[ServerFlags] = {},
|
flags: set[ServerFlags] = {},
|
||||||
factories: openArray[ExtFactory] = [],
|
factories: openArray[ExtFactory] = [],
|
||||||
rng: Rng = nil): T =
|
rng: Rng = nil,
|
||||||
|
listenError: ListenErrorCallback = ListenErrorDefault): T =
|
||||||
|
|
||||||
T(
|
T(
|
||||||
upgrader: upgrade,
|
upgrader: upgrade,
|
||||||
@ -300,14 +312,16 @@ proc new*(
|
|||||||
tlsFlags: tlsFlags,
|
tlsFlags: tlsFlags,
|
||||||
flags: flags,
|
flags: flags,
|
||||||
factories: @factories,
|
factories: @factories,
|
||||||
rng: rng)
|
rng: rng,
|
||||||
|
listenError: listenError)
|
||||||
|
|
||||||
proc new*(
|
proc new*(
|
||||||
T: typedesc[WsTransport],
|
T: typedesc[WsTransport],
|
||||||
upgrade: Upgrade,
|
upgrade: Upgrade,
|
||||||
flags: set[ServerFlags] = {},
|
flags: set[ServerFlags] = {},
|
||||||
factories: openArray[ExtFactory] = [],
|
factories: openArray[ExtFactory] = [],
|
||||||
rng: Rng = nil): T =
|
rng: Rng = nil,
|
||||||
|
listenError: ListenErrorCallback = ListenErrorDefault): T =
|
||||||
|
|
||||||
T.new(
|
T.new(
|
||||||
upgrade = upgrade,
|
upgrade = upgrade,
|
||||||
@ -315,4 +329,5 @@ proc new*(
|
|||||||
tlsCertificate = nil,
|
tlsCertificate = nil,
|
||||||
flags = flags,
|
flags = flags,
|
||||||
factories = @factories,
|
factories = @factories,
|
||||||
rng = rng)
|
rng = rng,
|
||||||
|
listenError = listenError)
|
||||||
|
@ -976,19 +976,24 @@ suite "Switch":
|
|||||||
await srcWsSwitch.stop()
|
await srcWsSwitch.stop()
|
||||||
await srcTcpSwitch.stop()
|
await srcTcpSwitch.stop()
|
||||||
|
|
||||||
asyncTest "listenError callback default returns TransportListenError (pessimistic)":
|
asyncTest "pessimistic: default listenError callback returns SwitchListenError":
|
||||||
let
|
let
|
||||||
switch = newStandardSwitch()
|
switch = newStandardSwitch()
|
||||||
transport = Transport()
|
transport = Transport()
|
||||||
|
|
||||||
check switch.listenError.isNil.not
|
check switch.listenError.isNil.not
|
||||||
|
|
||||||
let exc = newException(TransportListenError, "test")
|
let
|
||||||
check not switch.listenError(transport, exc).isNil
|
exc = newException(TransportListenError, "test")
|
||||||
|
listenErrResult = await switch.listenError(transport, exc)
|
||||||
|
|
||||||
|
check:
|
||||||
|
not listenErrResult.isNil
|
||||||
|
listenErrResult is (ref SwitchListenError)
|
||||||
|
|
||||||
await switch.stop()
|
await switch.stop()
|
||||||
|
|
||||||
asyncTest "listenError callback assignable and callable":
|
asyncTest "switch listenError callback assignable and callable":
|
||||||
let
|
let
|
||||||
switch = newStandardSwitch()
|
switch = newStandardSwitch()
|
||||||
transportListenError = newException(TransportListenError, "test1")
|
transportListenError = newException(TransportListenError, "test1")
|
||||||
@ -996,7 +1001,8 @@ suite "Switch":
|
|||||||
transport = Transport()
|
transport = Transport()
|
||||||
|
|
||||||
switch.listenError = proc(
|
switch.listenError = proc(
|
||||||
t: Transport, exc: ref TransportListenError): ref SwitchListenError =
|
t: Transport,
|
||||||
|
exc: ref TransportListenError): Future[ref SwitchListenError] {.async.} =
|
||||||
|
|
||||||
check:
|
check:
|
||||||
exc == transportListenError
|
exc == transportListenError
|
||||||
@ -1004,8 +1010,8 @@ suite "Switch":
|
|||||||
|
|
||||||
return switchListenError
|
return switchListenError
|
||||||
|
|
||||||
check:
|
let listenErrResult = await switch.listenError(transport, transportListenError)
|
||||||
switch.listenError(transport, transportListenError) == switchListenError
|
check listenErrResult == switchListenError
|
||||||
|
|
||||||
await switch.stop()
|
await switch.stop()
|
||||||
|
|
||||||
@ -1016,7 +1022,7 @@ suite "Switch":
|
|||||||
exc2 = newException(CatchableError, "test2")
|
exc2 = newException(CatchableError, "test2")
|
||||||
|
|
||||||
transportStartMock =
|
transportStartMock =
|
||||||
proc(self: MockTransport, addrs: seq[MultiAddress]): Future[void] =
|
proc(self: MockTransport, addrs: seq[MultiAddress]): Future[void] {.async.} =
|
||||||
for i, ma in addrs:
|
for i, ma in addrs:
|
||||||
try:
|
try:
|
||||||
if i == 0:
|
if i == 0:
|
||||||
@ -1031,9 +1037,10 @@ suite "Switch":
|
|||||||
echo "[test.startMock] raising exc2"
|
echo "[test.startMock] raising exc2"
|
||||||
raise exc2
|
raise exc2
|
||||||
except CatchableError as e:
|
except CatchableError as e:
|
||||||
let err = self.listenError(ma, e)
|
let err = await self.listenError(ma, e)
|
||||||
if not err.isNil:
|
if not err.isNil:
|
||||||
raise err
|
raise err
|
||||||
|
# return
|
||||||
fail() # should not get this far
|
fail() # should not get this far
|
||||||
|
|
||||||
mockTransport =
|
mockTransport =
|
||||||
@ -1067,8 +1074,9 @@ suite "Switch":
|
|||||||
exc = TransportListenError(e.parent[])
|
exc = TransportListenError(e.parent[])
|
||||||
ma = exc.ma
|
ma = exc.ma
|
||||||
|
|
||||||
check ma == ma0
|
check:
|
||||||
check exc.parent == exc0
|
ma == ma0
|
||||||
|
exc.parent == exc0
|
||||||
|
|
||||||
await switch.stop()
|
await switch.stop()
|
||||||
|
|
||||||
@ -1080,7 +1088,10 @@ suite "Switch":
|
|||||||
exc2 = newException(CatchableError, "test2")
|
exc2 = newException(CatchableError, "test2")
|
||||||
|
|
||||||
transportListenError =
|
transportListenError =
|
||||||
proc(ma: MultiAddress, ex: ref CatchableError): ref TransportListenError =
|
proc(
|
||||||
|
ma: MultiAddress,
|
||||||
|
ex: ref CatchableError): Future[ref TransportListenError] {.async.} =
|
||||||
|
|
||||||
handledTransportErrs[ma] = ex
|
handledTransportErrs[ma] = ex
|
||||||
return nil # optimistic transport multiaddress failure
|
return nil # optimistic transport multiaddress failure
|
||||||
|
|
||||||
@ -1098,7 +1109,7 @@ suite "Switch":
|
|||||||
echo "[test.startMock] raising exc2"
|
echo "[test.startMock] raising exc2"
|
||||||
raise exc2
|
raise exc2
|
||||||
except CatchableError as e:
|
except CatchableError as e:
|
||||||
let err = self.listenError(ma, e)
|
let err = await self.listenError(ma, e)
|
||||||
# check err == nil
|
# check err == nil
|
||||||
if not err.isNil:
|
if not err.isNil:
|
||||||
raise err
|
raise err
|
||||||
@ -1129,7 +1140,8 @@ suite "Switch":
|
|||||||
|
|
||||||
|
|
||||||
switch.listenError = proc(
|
switch.listenError = proc(
|
||||||
t: Transport, exc: ref TransportListenError): ref SwitchListenError =
|
t: Transport,
|
||||||
|
exc: ref TransportListenError): Future[ref SwitchListenError] {.async.} =
|
||||||
|
|
||||||
let ma = exc.ma
|
let ma = exc.ma
|
||||||
if ma == ma0:
|
if ma == ma0:
|
||||||
@ -1160,7 +1172,10 @@ suite "Switch":
|
|||||||
exc2 = newException(CatchableError, "test2")
|
exc2 = newException(CatchableError, "test2")
|
||||||
|
|
||||||
transportListenError =
|
transportListenError =
|
||||||
proc(ma: MultiAddress, ex: ref CatchableError): ref TransportListenError =
|
proc(
|
||||||
|
ma: MultiAddress,
|
||||||
|
ex: ref CatchableError): Future[ref TransportListenError] {.async.} =
|
||||||
|
|
||||||
handledTransportErrs[ma] = ex
|
handledTransportErrs[ma] = ex
|
||||||
return nil # optimistic transport multiaddress failure
|
return nil # optimistic transport multiaddress failure
|
||||||
|
|
||||||
@ -1177,7 +1192,7 @@ suite "Switch":
|
|||||||
echo "[test.startMock] raising exc2"
|
echo "[test.startMock] raising exc2"
|
||||||
raise exc2
|
raise exc2
|
||||||
except CatchableError as e:
|
except CatchableError as e:
|
||||||
let err = self.listenError(ma, e)
|
let err = await self.listenError(ma, e)
|
||||||
# check err == nil
|
# check err == nil
|
||||||
if not err.isNil:
|
if not err.isNil:
|
||||||
raise err
|
raise err
|
||||||
@ -1208,7 +1223,8 @@ suite "Switch":
|
|||||||
|
|
||||||
|
|
||||||
switch.listenError = proc(
|
switch.listenError = proc(
|
||||||
t: Transport, exc: ref TransportListenError): ref SwitchListenError =
|
t: Transport,
|
||||||
|
exc: ref TransportListenError): Future[ref SwitchListenError] {.async.} =
|
||||||
fail()
|
fail()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@ -1235,13 +1251,19 @@ suite "Switch":
|
|||||||
|
|
||||||
|
|
||||||
transportListenError0 =
|
transportListenError0 =
|
||||||
proc(ma: MultiAddress, ex: ref CatchableError): ref TransportListenError =
|
proc(
|
||||||
|
ma: MultiAddress,
|
||||||
|
ex: ref CatchableError): Future[ref TransportListenError] {.async.} =
|
||||||
|
|
||||||
handledTransportErrs0[ma] = ex
|
handledTransportErrs0[ma] = ex
|
||||||
# pessimistic transport multiaddress failure
|
# pessimistic transport multiaddress failure
|
||||||
return newTransportListenError(ma, ex)
|
return newTransportListenError(ma, ex)
|
||||||
|
|
||||||
transportListenError1 =
|
transportListenError1 =
|
||||||
proc(ma: MultiAddress, ex: ref CatchableError): ref TransportListenError =
|
proc(
|
||||||
|
ma: MultiAddress,
|
||||||
|
ex: ref CatchableError): Future[ref TransportListenError] {.async.} =
|
||||||
|
|
||||||
handledTransportErrs1[ma] = ex
|
handledTransportErrs1[ma] = ex
|
||||||
# pessimistic transport multiaddress failure
|
# pessimistic transport multiaddress failure
|
||||||
return newTransportListenError(ma, ex)
|
return newTransportListenError(ma, ex)
|
||||||
@ -1258,7 +1280,7 @@ suite "Switch":
|
|||||||
else:
|
else:
|
||||||
fail()
|
fail()
|
||||||
except CatchableError as e:
|
except CatchableError as e:
|
||||||
let err = self.listenError(ma, e)
|
let err = await self.listenError(ma, e)
|
||||||
if not err.isNil:
|
if not err.isNil:
|
||||||
raise err
|
raise err
|
||||||
|
|
||||||
@ -1271,7 +1293,7 @@ suite "Switch":
|
|||||||
else:
|
else:
|
||||||
fail()
|
fail()
|
||||||
except CatchableError as e:
|
except CatchableError as e:
|
||||||
let err = self.listenError(ma, e)
|
let err = await self.listenError(ma, e)
|
||||||
if not err.isNil:
|
if not err.isNil:
|
||||||
raise err
|
raise err
|
||||||
|
|
||||||
@ -1314,15 +1336,17 @@ suite "Switch":
|
|||||||
|
|
||||||
switch.listenError = proc(
|
switch.listenError = proc(
|
||||||
t: Transport,
|
t: Transport,
|
||||||
exc: ref TransportListenError): ref SwitchListenError =
|
exc: ref TransportListenError): Future[ref SwitchListenError] {.async.} =
|
||||||
|
|
||||||
let ma = exc.ma
|
let ma = exc.ma
|
||||||
if ma == ma0:
|
if ma == ma0:
|
||||||
check exc.parent == exc10
|
check:
|
||||||
check t == switch.transports[1]
|
exc.parent == exc10
|
||||||
|
t == switch.transports[1]
|
||||||
elif ma == ma1:
|
elif ma == ma1:
|
||||||
check exc.parent == exc01
|
check:
|
||||||
check t == switch.transports[0]
|
exc.parent == exc01
|
||||||
|
t == switch.transports[0]
|
||||||
else:
|
else:
|
||||||
fail()
|
fail()
|
||||||
# switch optimistic, continue with all transports
|
# switch optimistic, continue with all transports
|
||||||
@ -1343,8 +1367,9 @@ suite "Switch":
|
|||||||
for ma, ex in handledTransportErrs1:
|
for ma, ex in handledTransportErrs1:
|
||||||
echo "ma: ", $ma, ", ex: ", ex.msg
|
echo "ma: ", $ma, ", ex: ", ex.msg
|
||||||
|
|
||||||
check handledTransportErrs0 == [(ma1, exc01)].toTable()
|
check:
|
||||||
check handledTransportErrs1 == [(ma0, exc10)].toTable()
|
handledTransportErrs0 == [(ma1, exc01)].toTable()
|
||||||
|
handledTransportErrs1 == [(ma0, exc10)].toTable()
|
||||||
|
|
||||||
await switch.stop()
|
await switch.stop()
|
||||||
|
|
||||||
@ -1358,18 +1383,27 @@ suite "Switch":
|
|||||||
exc10 = newException(CatchableError, "test10")
|
exc10 = newException(CatchableError, "test10")
|
||||||
|
|
||||||
transportListenError0 =
|
transportListenError0 =
|
||||||
proc(ma: MultiAddress, ex: ref CatchableError): ref TransportListenError =
|
proc(
|
||||||
|
ma: MultiAddress,
|
||||||
|
ex: ref CatchableError): Future[ref TransportListenError] {.async.} =
|
||||||
|
|
||||||
handledTransportErrs0[ma] = ex
|
handledTransportErrs0[ma] = ex
|
||||||
# optimistic transport multiaddress failure
|
# optimistic transport multiaddress failure
|
||||||
return nil
|
return nil
|
||||||
|
|
||||||
transportListenError1 =
|
transportListenError1 =
|
||||||
proc(ma: MultiAddress, ex: ref CatchableError): ref TransportListenError =
|
proc(
|
||||||
|
ma: MultiAddress,
|
||||||
|
ex: ref CatchableError): Future[ref TransportListenError] {.async.} =
|
||||||
|
|
||||||
handledTransportErrs1[ma] = ex
|
handledTransportErrs1[ma] = ex
|
||||||
return newTransportListenError(ma, ex)
|
return newTransportListenError(ma, ex)
|
||||||
|
|
||||||
transportListenError2 =
|
transportListenError2 =
|
||||||
proc(ma: MultiAddress, ex: ref CatchableError): ref TransportListenError =
|
proc(
|
||||||
|
ma: MultiAddress,
|
||||||
|
ex: ref CatchableError): Future[ref TransportListenError] {.async.} =
|
||||||
|
|
||||||
# should not get here as switch is pessimistic so will stop at first
|
# should not get here as switch is pessimistic so will stop at first
|
||||||
# failed transport (transpor1)
|
# failed transport (transpor1)
|
||||||
fail()
|
fail()
|
||||||
@ -1383,7 +1417,7 @@ suite "Switch":
|
|||||||
else:
|
else:
|
||||||
continue
|
continue
|
||||||
except CatchableError as e:
|
except CatchableError as e:
|
||||||
let err = self.listenError(ma, e)
|
let err = await self.listenError(ma, e)
|
||||||
if not err.isNil:
|
if not err.isNil:
|
||||||
raise err
|
raise err
|
||||||
|
|
||||||
@ -1396,7 +1430,7 @@ suite "Switch":
|
|||||||
else:
|
else:
|
||||||
continue
|
continue
|
||||||
except CatchableError as e:
|
except CatchableError as e:
|
||||||
let err = self.listenError(ma, e)
|
let err = await self.listenError(ma, e)
|
||||||
if not err.isNil:
|
if not err.isNil:
|
||||||
raise err
|
raise err
|
||||||
|
|
||||||
@ -1457,10 +1491,11 @@ suite "Switch":
|
|||||||
|
|
||||||
switch.listenError = proc(
|
switch.listenError = proc(
|
||||||
t: Transport,
|
t: Transport,
|
||||||
exc: ref TransportListenError): ref SwitchListenError =
|
exc: ref TransportListenError): Future[ref SwitchListenError] {.async.} =
|
||||||
|
|
||||||
check t == switch.transports[1]
|
check:
|
||||||
check exc.ma == ma0
|
t == switch.transports[1]
|
||||||
|
exc.ma == ma0
|
||||||
|
|
||||||
# pessimistic
|
# pessimistic
|
||||||
return newSwitchListenError(t, exc)
|
return newSwitchListenError(t, exc)
|
||||||
@ -1469,9 +1504,10 @@ suite "Switch":
|
|||||||
await switch.start()
|
await switch.start()
|
||||||
except SwitchListenError as e:
|
except SwitchListenError as e:
|
||||||
let tListenEx = (ref TransportListenError)(e.parent)
|
let tListenEx = (ref TransportListenError)(e.parent)
|
||||||
check tListenEx.ma == ma0
|
check:
|
||||||
check tListenEx.parent == exc10
|
tListenEx.ma == ma0
|
||||||
check e.transport == switch.transports[1]
|
tListenEx.parent == exc10
|
||||||
|
e.transport == switch.transports[1]
|
||||||
|
|
||||||
echo "handledTransportErrs0:"
|
echo "handledTransportErrs0:"
|
||||||
for ma, ex in handledTransportErrs0:
|
for ma, ex in handledTransportErrs0:
|
||||||
@ -1481,19 +1517,24 @@ suite "Switch":
|
|||||||
for ma, ex in handledTransportErrs1:
|
for ma, ex in handledTransportErrs1:
|
||||||
echo "ma: ", $ma, ", ex: ", ex.msg
|
echo "ma: ", $ma, ", ex: ", ex.msg
|
||||||
|
|
||||||
check handledTransportErrs0 == [(ma1, exc01)].toTable()
|
check:
|
||||||
check handledTransportErrs1 == [(ma0, exc10)].toTable()
|
handledTransportErrs0 == [(ma1, exc01)].toTable()
|
||||||
|
handledTransportErrs1 == [(ma0, exc10)].toTable()
|
||||||
|
|
||||||
await switch.stop()
|
await switch.stop()
|
||||||
|
|
||||||
asyncTest "no exceptions raised, listenError should not be called":
|
asyncTest "no exceptions raised, listenError should not be called":
|
||||||
let
|
let
|
||||||
transportListenError0 =
|
transportListenError0 =
|
||||||
proc(ma: MultiAddress, ex: ref CatchableError): ref TransportListenError =
|
proc(
|
||||||
|
ma: MultiAddress,
|
||||||
|
ex: ref CatchableError): Future[ref TransportListenError] {.async.} =
|
||||||
fail()
|
fail()
|
||||||
|
|
||||||
transportListenError1 =
|
transportListenError1 =
|
||||||
proc(ma: MultiAddress, ex: ref CatchableError): ref TransportListenError =
|
proc(
|
||||||
|
ma: MultiAddress,
|
||||||
|
ex: ref CatchableError): Future[ref TransportListenError] {.async.} =
|
||||||
fail()
|
fail()
|
||||||
|
|
||||||
transportStartMock0 =
|
transportStartMock0 =
|
||||||
@ -1546,7 +1587,7 @@ suite "Switch":
|
|||||||
|
|
||||||
switch.listenError = proc(
|
switch.listenError = proc(
|
||||||
t: Transport,
|
t: Transport,
|
||||||
exc: ref TransportListenError): ref SwitchListenError =
|
exc: ref TransportListenError): Future[ref SwitchListenError] {.async.} =
|
||||||
|
|
||||||
fail()
|
fail()
|
||||||
|
|
||||||
|
@ -125,6 +125,115 @@ suite "TCP transport":
|
|||||||
server.close()
|
server.close()
|
||||||
await server.join()
|
await server.join()
|
||||||
|
|
||||||
|
asyncTest "pessimistic: default listenError callback returns TransportListenError":
|
||||||
|
let
|
||||||
|
transport = TcpTransport.new(upgrade = Upgrade())
|
||||||
|
|
||||||
|
check not transport.listenError.isNil
|
||||||
|
|
||||||
|
let
|
||||||
|
exc = newException(CatchableError, "test")
|
||||||
|
ma = Multiaddress.init("/ip4/0.0.0.0/tcp/0").tryGet()
|
||||||
|
listenErrResult = await transport.listenError(ma, exc)
|
||||||
|
|
||||||
|
check:
|
||||||
|
not listenErrResult.isNil
|
||||||
|
listenErrResult is (ref TransportListenError)
|
||||||
|
|
||||||
|
await transport.stop()
|
||||||
|
|
||||||
|
asyncTest "listenError callback assignable and callable":
|
||||||
|
let
|
||||||
|
failListenErr = proc(
|
||||||
|
maErr: MultiAddress,
|
||||||
|
exc: ref CatchableError): Future[ref TransportListenError] {.async.} =
|
||||||
|
fail()
|
||||||
|
transport = TcpTransport.new(
|
||||||
|
upgrade = Upgrade(),
|
||||||
|
listenError = failListenErr)
|
||||||
|
ma = Multiaddress.init("/ip4/0.0.0.0/tcp/0").tryGet()
|
||||||
|
catchableError = newException(CatchableError, "test1")
|
||||||
|
transportListenError = newTransportListenError(ma, catchableError)
|
||||||
|
|
||||||
|
transport.listenError = proc(
|
||||||
|
maErr: MultiAddress,
|
||||||
|
exc: ref CatchableError): Future[ref TransportListenError] {.async.} =
|
||||||
|
|
||||||
|
check:
|
||||||
|
exc == catchableError
|
||||||
|
maErr == ma
|
||||||
|
|
||||||
|
return transportListenError
|
||||||
|
|
||||||
|
let listenErrResult = await transport.listenError(ma, catchableError)
|
||||||
|
|
||||||
|
check:
|
||||||
|
listenErrResult == transportListenError
|
||||||
|
|
||||||
|
await transport.stop()
|
||||||
|
|
||||||
|
asyncTest "pessimistic: default listenError re-raises exception":
|
||||||
|
let
|
||||||
|
# use a bad MultiAddress to throw an error during transport.start
|
||||||
|
ma = Multiaddress.init("/ip4/1.0.0.0/tcp/0").tryGet()
|
||||||
|
|
||||||
|
transport = TcpTransport.new(upgrade = Upgrade())
|
||||||
|
|
||||||
|
expect TransportListenError:
|
||||||
|
await transport.start(@[ma])
|
||||||
|
|
||||||
|
await transport.stop()
|
||||||
|
|
||||||
|
asyncTest "pessimistic: overridden listenError re-raises exception":
|
||||||
|
var transportListenErr: ref TransportListenError
|
||||||
|
|
||||||
|
let
|
||||||
|
# use a bad MultiAddress to throw an error during transport.start
|
||||||
|
ma = Multiaddress.init("/ip4/1.0.0.0/tcp/0").tryGet()
|
||||||
|
listenError = proc(
|
||||||
|
maErr: MultiAddress,
|
||||||
|
exc: ref CatchableError): Future[ref TransportListenError] {.async.} =
|
||||||
|
|
||||||
|
transportListenErr = newTransportListenError(maErr, exc)
|
||||||
|
check maErr == ma
|
||||||
|
return transportListenErr
|
||||||
|
|
||||||
|
transport = TcpTransport.new(
|
||||||
|
upgrade = Upgrade(),
|
||||||
|
listenError = listenError)
|
||||||
|
|
||||||
|
try:
|
||||||
|
await transport.start(@[ma])
|
||||||
|
fail()
|
||||||
|
except TransportListenError as e:
|
||||||
|
check e == transportListenErr
|
||||||
|
|
||||||
|
await transport.stop()
|
||||||
|
|
||||||
|
asyncTest "optimistic: overridden listenError does not re-raise exception":
|
||||||
|
var transportListenErr: ref TransportListenError
|
||||||
|
|
||||||
|
let
|
||||||
|
# use a bad MultiAddress to throw an error during transport.start
|
||||||
|
ma = Multiaddress.init("/ip4/1.0.0.0/tcp/0").tryGet()
|
||||||
|
listenError = proc(
|
||||||
|
maErr: MultiAddress,
|
||||||
|
exc: ref CatchableError): Future[ref TransportListenError] {.async.} =
|
||||||
|
|
||||||
|
check maErr == ma
|
||||||
|
return nil
|
||||||
|
|
||||||
|
transport = TcpTransport.new(
|
||||||
|
upgrade = Upgrade(),
|
||||||
|
listenError = listenError)
|
||||||
|
|
||||||
|
try:
|
||||||
|
await transport.start(@[ma])
|
||||||
|
except TransportListenError as e:
|
||||||
|
fail()
|
||||||
|
|
||||||
|
await transport.stop()
|
||||||
|
|
||||||
commonTransportTest(
|
commonTransportTest(
|
||||||
"TcpTransport",
|
"TcpTransport",
|
||||||
proc (): Transport = TcpTransport.new(upgrade = Upgrade()),
|
proc (): Transport = TcpTransport.new(upgrade = Upgrade()),
|
||||||
|
@ -55,6 +55,115 @@ suite "WebSocket transport":
|
|||||||
teardown:
|
teardown:
|
||||||
checkTrackers()
|
checkTrackers()
|
||||||
|
|
||||||
|
asyncTest "pessimistic: default listenError callback returns TransportListenError":
|
||||||
|
let
|
||||||
|
transport = WsTransport.new(upgrade = Upgrade())
|
||||||
|
|
||||||
|
check not transport.listenError.isNil
|
||||||
|
|
||||||
|
let
|
||||||
|
exc = newException(CatchableError, "test")
|
||||||
|
ma = Multiaddress.init("/ip4/0.0.0.0/tcp/0/ws").tryGet()
|
||||||
|
listenErrResult = await transport.listenError(ma, exc)
|
||||||
|
|
||||||
|
check:
|
||||||
|
not listenErrResult.isNil
|
||||||
|
listenErrResult is (ref TransportListenError)
|
||||||
|
|
||||||
|
await transport.stop()
|
||||||
|
|
||||||
|
asyncTest "listenError callback assignable and callable":
|
||||||
|
let
|
||||||
|
failListenErr = proc(
|
||||||
|
maErr: MultiAddress,
|
||||||
|
exc: ref CatchableError): Future[ref TransportListenError] {.async.} =
|
||||||
|
fail()
|
||||||
|
transport = WsTransport.new(
|
||||||
|
upgrade = Upgrade(),
|
||||||
|
listenError = failListenErr)
|
||||||
|
ma = Multiaddress.init("/ip4/0.0.0.0/tcp/0/ws").tryGet()
|
||||||
|
catchableError = newException(CatchableError, "test1")
|
||||||
|
transportListenError = newTransportListenError(ma, catchableError)
|
||||||
|
|
||||||
|
transport.listenError = proc(
|
||||||
|
maErr: MultiAddress,
|
||||||
|
exc: ref CatchableError): Future[ref TransportListenError] {.async.} =
|
||||||
|
|
||||||
|
check:
|
||||||
|
exc == catchableError
|
||||||
|
maErr == ma
|
||||||
|
|
||||||
|
return transportListenError
|
||||||
|
|
||||||
|
let listenErrResult = await transport.listenError(ma, catchableError)
|
||||||
|
|
||||||
|
check:
|
||||||
|
listenErrResult == transportListenError
|
||||||
|
|
||||||
|
await transport.stop()
|
||||||
|
|
||||||
|
asyncTest "pessimistic: default listenError re-raises exception":
|
||||||
|
let
|
||||||
|
# use a bad MultiAddress to throw an error during transport.start
|
||||||
|
ma = Multiaddress.init("/ip4/1.0.0.0/tcp/0/ws").tryGet()
|
||||||
|
|
||||||
|
transport = WsTransport.new(upgrade = Upgrade())
|
||||||
|
|
||||||
|
expect TransportListenError:
|
||||||
|
await transport.start(@[ma])
|
||||||
|
|
||||||
|
await transport.stop()
|
||||||
|
|
||||||
|
asyncTest "pessimistic: overridden listenError re-raises exception":
|
||||||
|
var transportListenErr: ref TransportListenError
|
||||||
|
|
||||||
|
let
|
||||||
|
# use a bad MultiAddress to throw an error during transport.start
|
||||||
|
ma = Multiaddress.init("/ip4/1.0.0.0/tcp/0/ws").tryGet()
|
||||||
|
listenError = proc(
|
||||||
|
maErr: MultiAddress,
|
||||||
|
exc: ref CatchableError): Future[ref TransportListenError] {.async.} =
|
||||||
|
|
||||||
|
transportListenErr = newTransportListenError(maErr, exc)
|
||||||
|
check maErr == ma
|
||||||
|
return transportListenErr
|
||||||
|
|
||||||
|
transport = WsTransport.new(
|
||||||
|
upgrade = Upgrade(),
|
||||||
|
listenError = listenError)
|
||||||
|
|
||||||
|
try:
|
||||||
|
await transport.start(@[ma])
|
||||||
|
fail()
|
||||||
|
except TransportListenError as e:
|
||||||
|
check e == transportListenErr
|
||||||
|
|
||||||
|
await transport.stop()
|
||||||
|
|
||||||
|
asyncTest "optimistic: overridden listenError does not re-raise exception":
|
||||||
|
var transportListenErr: ref TransportListenError
|
||||||
|
|
||||||
|
let
|
||||||
|
# use a bad MultiAddress to throw an error during transport.start
|
||||||
|
ma = Multiaddress.init("/ip4/1.0.0.0/tcp/0/ws").tryGet()
|
||||||
|
listenError = proc(
|
||||||
|
maErr: MultiAddress,
|
||||||
|
exc: ref CatchableError): Future[ref TransportListenError] {.async.} =
|
||||||
|
|
||||||
|
check maErr == ma
|
||||||
|
return nil
|
||||||
|
|
||||||
|
transport = WsTransport.new(
|
||||||
|
upgrade = Upgrade(),
|
||||||
|
listenError = listenError)
|
||||||
|
|
||||||
|
try:
|
||||||
|
await transport.start(@[ma])
|
||||||
|
except TransportListenError as e:
|
||||||
|
fail()
|
||||||
|
|
||||||
|
await transport.stop()
|
||||||
|
|
||||||
commonTransportTest(
|
commonTransportTest(
|
||||||
"WebSocket",
|
"WebSocket",
|
||||||
proc (): Transport = WsTransport.new(Upgrade()),
|
proc (): Transport = WsTransport.new(Upgrade()),
|
||||||
|
Loading…
x
Reference in New Issue
Block a user