mirror of https://github.com/vacp2p/nim-libp2p.git
adding switch e2e test
This commit is contained in:
parent
79129ea6d1
commit
db1aad3df0
|
@ -33,31 +33,31 @@ proc secure(s: Switch, conn: Connection) {.async, gcsafe.} =
|
||||||
## secure the incoming connection
|
## secure the incoming connection
|
||||||
discard
|
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.} =
|
proc dial*(s: Switch, peer: PeerInfo, proto: string = ""): Future[Connection] {.async.} =
|
||||||
for t in s.transports: # for each transport
|
for t in s.transports: # for each transport
|
||||||
for a in peer.addrs: # for each address
|
for a in peer.addrs: # for each address
|
||||||
if t.handles(a): # check if it can dial it
|
if t.handles(a): # check if it can dial it
|
||||||
result = await t.dial(a)
|
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) =
|
proc mount*[T: LPProtocol](s: Switch, proto: T) =
|
||||||
if isNil(proto.handler):
|
if isNil(proto.handler):
|
||||||
raise newException(CatchableError,
|
raise newException(CatchableError,
|
||||||
"Protocol has to define a handle method or proc")
|
"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)
|
s.ms.addHandler(proto.codec, proto)
|
||||||
|
|
||||||
proc start*(s: Switch) {.async.} =
|
proc start*(s: Switch) {.async.} =
|
||||||
proc handle(conn: Connection): Future[void] {.closure, gcsafe.} =
|
proc handle(conn: Connection): Future[void] {.async, closure, gcsafe.} =
|
||||||
s.processConn(conn)
|
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 t in s.transports: # for each transport
|
||||||
for a in s.peerInfo.addrs:
|
for a in s.peerInfo.addrs:
|
||||||
|
|
|
@ -177,7 +177,8 @@ suite "Multistream select":
|
||||||
peerInfo.peerId = PeerID.init(seckey)
|
peerInfo.peerId = PeerID.init(seckey)
|
||||||
var protocol: LPProtocol = newProtocol(LPProtocol, peerInfo)
|
var protocol: LPProtocol = newProtocol(LPProtocol, peerInfo)
|
||||||
proc testHandler(conn: Connection,
|
proc testHandler(conn: Connection,
|
||||||
proto: string): Future[void] {.async, gcsafe.} = discard
|
proto: string):
|
||||||
|
Future[void] {.async, gcsafe.} = discard
|
||||||
|
|
||||||
protocol.handler = testHandler
|
protocol.handler = testHandler
|
||||||
ms.addHandler("/test/proto1/1.0.0", protocol)
|
ms.addHandler("/test/proto1/1.0.0", protocol)
|
||||||
|
@ -204,7 +205,8 @@ suite "Multistream select":
|
||||||
peerInfo.peerId = PeerID.init(seckey)
|
peerInfo.peerId = PeerID.init(seckey)
|
||||||
var protocol: LPProtocol = newProtocol(LPProtocol, peerInfo)
|
var protocol: LPProtocol = newProtocol(LPProtocol, peerInfo)
|
||||||
proc testHandler(conn: Connection,
|
proc testHandler(conn: Connection,
|
||||||
proto: string): Future[void] {.async, gcsafe.} = discard
|
proto: string):
|
||||||
|
Future[void] {.async, gcsafe.} = discard
|
||||||
protocol.handler = testHandler
|
protocol.handler = testHandler
|
||||||
ms.addHandler("/unabvailable/proto/1.0.0", protocol)
|
ms.addHandler("/unabvailable/proto/1.0.0", protocol)
|
||||||
|
|
||||||
|
@ -223,7 +225,8 @@ suite "Multistream select":
|
||||||
peerInfo.peerId = PeerID.init(seckey)
|
peerInfo.peerId = PeerID.init(seckey)
|
||||||
var protocol: LPProtocol = newProtocol(LPProtocol, peerInfo)
|
var protocol: LPProtocol = newProtocol(LPProtocol, peerInfo)
|
||||||
proc testHandler(conn: Connection,
|
proc testHandler(conn: Connection,
|
||||||
proto: string): Future[void] {.async, gcsafe.} =
|
proto: string):
|
||||||
|
Future[void] {.async, gcsafe.} =
|
||||||
check proto == "/test/proto/1.0.0"
|
check proto == "/test/proto/1.0.0"
|
||||||
await conn.writeLp("Hello!")
|
await conn.writeLp("Hello!")
|
||||||
await conn.close()
|
await conn.close()
|
||||||
|
@ -262,7 +265,8 @@ suite "Multistream select":
|
||||||
peerInfo.peerId = PeerID.init(seckey)
|
peerInfo.peerId = PeerID.init(seckey)
|
||||||
var protocol: LPProtocol = newProtocol(LPProtocol, peerInfo)
|
var protocol: LPProtocol = newProtocol(LPProtocol, peerInfo)
|
||||||
proc testHandler(conn: Connection,
|
proc testHandler(conn: Connection,
|
||||||
proto: string): Future[void] {.async.} = discard
|
proto: string):
|
||||||
|
Future[void] {.async.} = discard
|
||||||
protocol.handler = testHandler
|
protocol.handler = testHandler
|
||||||
msListen.addHandler("/test/proto1/1.0.0", protocol)
|
msListen.addHandler("/test/proto1/1.0.0", protocol)
|
||||||
msListen.addHandler("/test/proto2/1.0.0", protocol)
|
msListen.addHandler("/test/proto2/1.0.0", protocol)
|
||||||
|
|
|
@ -18,6 +18,7 @@ method init(p: TestProto) {.gcsafe.} =
|
||||||
check "Hello!" == msg
|
check "Hello!" == msg
|
||||||
await conn.writeLp("Hello!")
|
await conn.writeLp("Hello!")
|
||||||
|
|
||||||
|
p.codec = TestCodec
|
||||||
p.handler = handle
|
p.handler = handle
|
||||||
|
|
||||||
suite "Switch":
|
suite "Switch":
|
||||||
|
@ -42,7 +43,6 @@ suite "Switch":
|
||||||
await switch1.start()
|
await switch1.start()
|
||||||
|
|
||||||
(switch2, peerInfo2) = createSwitch(ma2)
|
(switch2, peerInfo2) = createSwitch(ma2)
|
||||||
await switch2.start()
|
|
||||||
let conn = await switch2.dial(peerInfo1, TestCodec)
|
let conn = await switch2.dial(peerInfo1, TestCodec)
|
||||||
await conn.writeLp("Hello!")
|
await conn.writeLp("Hello!")
|
||||||
let msg = cast[string](await conn.readLp())
|
let msg = cast[string](await conn.readLp())
|
||||||
|
|
Loading…
Reference in New Issue