readMsg should never return an empty buffer

This commit is contained in:
Dmitriy Ryajov 2019-09-25 18:11:20 -06:00
parent f7e5c8ee30
commit c7606ce2eb
2 changed files with 32 additions and 44 deletions

View File

@ -55,15 +55,7 @@ method handle*(m: Mplex) {.async, gcsafe.} =
trace "starting mplex main loop" trace "starting mplex main loop"
try: try:
while not m.connection.closed: while not m.connection.closed:
let msgRes = await m.connection.readMsg() let (id, msgType, data) = (await m.connection.readMsg()).get()
if msgRes.isNone:
# TODO: this should ideally be poll(timeout = 100)
# but chronos poll() doesnt take a timeout, so it
# might and does hang
await sleepAsync(100.millis) # yield to async loop
continue
let (id, msgType, data) = msgRes.get()
let initiator = bool(ord(msgType) and 1) let initiator = bool(ord(msgType) and 1)
var channel: LPChannel var channel: LPChannel
if MessageType(msgType) != MessageType.New: if MessageType(msgType) != MessageType.New:

View File

@ -14,52 +14,48 @@ import ../libp2p/protocols/identify,
suite "Identify": suite "Identify":
test "handle identify message": test "handle identify message":
proc testHandle(): Future[bool] {.async.} = proc testHandle(): Future[bool] {.async.} =
let ma: MultiAddress = Multiaddress.init("/ip4/127.0.0.1/tcp/53360") let ma: MultiAddress = Multiaddress.init("/ip4/0.0.0.0/tcp/0")
let remoteSeckey = PrivateKey.random(RSA) let remoteSeckey = PrivateKey.random(RSA)
var remotePeerInfo: PeerInfo var remotePeerInfo: PeerInfo
var serverFut: Future[void] var serverFut: Future[void]
var transport: TcpTransport remotePeerInfo.peerId = some(PeerID.init(remoteSeckey))
proc receiver() {.async.} = remotePeerInfo.addrs.add(ma)
remotePeerInfo.peerId = some(PeerID.init(remoteSeckey)) remotePeerInfo.protocols.add("/test/proto1/1.0.0")
remotePeerInfo.addrs.add(ma) remotePeerInfo.protocols.add("/test/proto2/1.0.0")
remotePeerInfo.protocols.add("/test/proto1/1.0.0")
remotePeerInfo.protocols.add("/test/proto2/1.0.0")
let identifyProto = newIdentify(remotePeerInfo) let identifyProto1 = newIdentify(remotePeerInfo)
let msListen = newMultistream() let msListen = newMultistream()
msListen.addHandler(IdentifyCodec, identifyProto) msListen.addHandler(IdentifyCodec, identifyProto1)
proc connHandler(conn: Connection): Future[void] {.async, gcsafe.} = proc connHandler(conn: Connection): Future[void] {.async, gcsafe.} =
await msListen.handle(conn) await msListen.handle(conn)
transport = newTransport(TcpTransport) var transport1 = newTransport(TcpTransport)
serverFut = await transport.listen(ma, connHandler) serverFut = await transport1.listen(ma, connHandler)
proc sender() {.async.} = let msDial = newMultistream()
let msDial = newMultistream() let transport2: TcpTransport = newTransport(TcpTransport)
let transport: TcpTransport = newTransport(TcpTransport) let conn = await transport2.dial(transport1.ma)
let conn = await transport.dial(ma)
let seckey = PrivateKey.random(RSA) let seckey = PrivateKey.random(RSA)
var peerInfo: PeerInfo var peerInfo: PeerInfo
peerInfo.peerId = some(PeerID.init(seckey)) peerInfo.peerId = some(PeerID.init(seckey))
peerInfo.addrs.add(ma) peerInfo.addrs.add(ma)
let identifyProto = newIdentify(peerInfo) let identifyProto2 = newIdentify(peerInfo)
let res = await msDial.select(conn, IdentifyCodec) let res = await msDial.select(conn, IdentifyCodec)
let id = await identifyProto.identify(conn, remotePeerInfo) let id = await identifyProto2.identify(conn, remotePeerInfo)
check id.pubKey.get() == remoteSeckey.getKey() check id.pubKey.get() == remoteSeckey.getKey()
check id.addrs[0] == ma check id.addrs[0] == ma
check id.protoVersion.get() == ProtoVersion check id.protoVersion.get() == ProtoVersion
# check id.agentVersion.get() == AgentVersion # check id.agentVersion.get() == AgentVersion
check id.protos == @["/test/proto1/1.0.0", "/test/proto2/1.0.0"] check id.protos == @["/test/proto1/1.0.0", "/test/proto2/1.0.0"]
await conn.close() await conn.close()
await allFutures(sender(), receiver()) await transport1.close()
await transport.close()
await serverFut await serverFut
result = true result = true
@ -68,7 +64,7 @@ suite "Identify":
test "handle failed identify": test "handle failed identify":
proc testHandleError() {.async.} = proc testHandleError() {.async.} =
let ma: MultiAddress = Multiaddress.init("/ip4/127.0.0.1/tcp/53361") let ma: MultiAddress = Multiaddress.init("/ip4/0.0.0.0/tcp/0")
let remoteSeckey = PrivateKey.random(RSA) let remoteSeckey = PrivateKey.random(RSA)
var remotePeerInfo: PeerInfo var remotePeerInfo: PeerInfo
@ -87,7 +83,7 @@ suite "Identify":
let msDial = newMultistream() let msDial = newMultistream()
let transport2: TcpTransport = newTransport(TcpTransport) let transport2: TcpTransport = newTransport(TcpTransport)
let conn = await transport2.dial(ma) let conn = await transport2.dial(transport1.ma)
let seckey = PrivateKey.random(RSA) let seckey = PrivateKey.random(RSA)
var localPeerInfo: PeerInfo var localPeerInfo: PeerInfo