adding switch e2e test

This commit is contained in:
Dmitriy Ryajov 2019-08-31 12:52:56 -06:00
parent 79129ea6d1
commit db1aad3df0
3 changed files with 20 additions and 16 deletions

View File

@ -33,31 +33,31 @@ proc secure(s: Switch, conn: Connection) {.async, gcsafe.} =
## secure the incoming connection
discard
proc processConn(s: Switch, conn: Connection, proto: string = "") {.async, gcsafe.} =
defer: await s.ms.handle(conn) # fire up the handler
await s.secure(conn) # secure the connection
if proto.len > 0:
echo "SELECTING"
if not await s.ms.select(conn, proto):
raise newException(CatchableError, "Unable to select protocol: " & proto)
proc dial*(s: Switch, peer: PeerInfo, proto: string = ""): Future[Connection] {.async.} =
for t in s.transports: # for each transport
for a in peer.addrs: # for each address
if t.handles(a): # check if it can dial it
result = await t.dial(a)
await s.processConn(result, proto)
await s.secure(result)
if not await s.ms.select(result, proto):
raise newException(CatchableError,
"Unable to select protocol: " & proto)
proc mount*[T: LPProtocol](s: Switch, proto: T) =
if isNil(proto.handler):
raise newException(CatchableError,
"Protocol has to define a handle method or proc")
if len(proto.codec) <= 0:
raise newException(CatchableError,
"Protocol has to define a codec string")
s.ms.addHandler(proto.codec, proto)
proc start*(s: Switch) {.async.} =
proc handle(conn: Connection): Future[void] {.closure, gcsafe.} =
s.processConn(conn)
proc handle(conn: Connection): Future[void] {.async, closure, gcsafe.} =
await s.secure(conn) # secure the connection
await s.ms.handle(conn) # fire up the ms handler
for t in s.transports: # for each transport
for a in s.peerInfo.addrs:

View File

@ -177,7 +177,8 @@ suite "Multistream select":
peerInfo.peerId = PeerID.init(seckey)
var protocol: LPProtocol = newProtocol(LPProtocol, peerInfo)
proc testHandler(conn: Connection,
proto: string): Future[void] {.async, gcsafe.} = discard
proto: string):
Future[void] {.async, gcsafe.} = discard
protocol.handler = testHandler
ms.addHandler("/test/proto1/1.0.0", protocol)
@ -204,7 +205,8 @@ suite "Multistream select":
peerInfo.peerId = PeerID.init(seckey)
var protocol: LPProtocol = newProtocol(LPProtocol, peerInfo)
proc testHandler(conn: Connection,
proto: string): Future[void] {.async, gcsafe.} = discard
proto: string):
Future[void] {.async, gcsafe.} = discard
protocol.handler = testHandler
ms.addHandler("/unabvailable/proto/1.0.0", protocol)
@ -223,7 +225,8 @@ suite "Multistream select":
peerInfo.peerId = PeerID.init(seckey)
var protocol: LPProtocol = newProtocol(LPProtocol, peerInfo)
proc testHandler(conn: Connection,
proto: string): Future[void] {.async, gcsafe.} =
proto: string):
Future[void] {.async, gcsafe.} =
check proto == "/test/proto/1.0.0"
await conn.writeLp("Hello!")
await conn.close()
@ -262,7 +265,8 @@ suite "Multistream select":
peerInfo.peerId = PeerID.init(seckey)
var protocol: LPProtocol = newProtocol(LPProtocol, peerInfo)
proc testHandler(conn: Connection,
proto: string): Future[void] {.async.} = discard
proto: string):
Future[void] {.async.} = discard
protocol.handler = testHandler
msListen.addHandler("/test/proto1/1.0.0", protocol)
msListen.addHandler("/test/proto2/1.0.0", protocol)

View File

@ -18,6 +18,7 @@ method init(p: TestProto) {.gcsafe.} =
check "Hello!" == msg
await conn.writeLp("Hello!")
p.codec = TestCodec
p.handler = handle
suite "Switch":
@ -42,7 +43,6 @@ suite "Switch":
await switch1.start()
(switch2, peerInfo2) = createSwitch(ma2)
await switch2.start()
let conn = await switch2.dial(peerInfo1, TestCodec)
await conn.writeLp("Hello!")
let msg = cast[string](await conn.readLp())