From db1aad3df08f4b8c03d2697242b210b87867b4da Mon Sep 17 00:00:00 2001 From: Dmitriy Ryajov Date: Sat, 31 Aug 2019 12:52:56 -0600 Subject: [PATCH] adding switch e2e test --- libp2p/switch.nim | 22 +++++++++++----------- tests/testmultistream.nim | 12 ++++++++---- tests/testswitch.nim | 2 +- 3 files changed, 20 insertions(+), 16 deletions(-) diff --git a/libp2p/switch.nim b/libp2p/switch.nim index 7e86d5e48..d3d156fa7 100644 --- a/libp2p/switch.nim +++ b/libp2p/switch.nim @@ -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: diff --git a/tests/testmultistream.nim b/tests/testmultistream.nim index c25f4c70e..c74070230 100644 --- a/tests/testmultistream.nim +++ b/tests/testmultistream.nim @@ -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) diff --git a/tests/testswitch.nim b/tests/testswitch.nim index abeee1231..f64e89ec6 100644 --- a/tests/testswitch.nim +++ b/tests/testswitch.nim @@ -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())