Add one more test for self-connect-open-accept-stream.

This commit is contained in:
cheatfate 2018-11-19 19:30:40 +02:00
parent deedc37fb3
commit f49d67bc0d
2 changed files with 29 additions and 1 deletions

View File

@ -96,7 +96,7 @@ type
PeerInfo* = object PeerInfo* = object
peer*: PeerID peer*: PeerID
addresses: seq[MultiAddress] addresses*: seq[MultiAddress]
P2PStreamCallback* = proc(api: DaemonAPI, P2PStreamCallback* = proc(api: DaemonAPI,
stream: P2PStream): Future[void] {.gcsafe.} stream: P2PStream): Future[void] {.gcsafe.}

View File

@ -8,8 +8,36 @@ proc identitySpawnTest(): Future[bool] {.async.} =
await api.close() await api.close()
result = true result = true
proc connectStreamTest(): Future[bool] {.async.} =
var api1 = await newDaemonApi(sockpath = "/tmp/p2pd-1.sock")
var api2 = await newDaemonApi(sockpath = "/tmp/p2pd-2.sock")
var id1 = await api1.identity()
var id2 = await api2.identity()
var protos = @["/test-stream"]
var test = "TEST STRING"
var testFuture = newFuture[string]("test.future")
proc streamHandler(api: DaemonAPI, stream: P2PStream) {.async.} =
var line = await stream.transp.readLine()
testFuture.complete(line)
await api2.addHandler(protos, streamHandler)
await api1.connect(id2.peer, id2.addresses)
var stream = await api1.openStream(id2.peer, protos)
let sent = await stream.transp.write(test & "\r\n")
doAssert(sent == len(test) + 2)
var check = await wait(testFuture, 10000)
doAssert(check == test)
result = true
when isMainModule: when isMainModule:
suite "libp2p-daemon test suite": suite "libp2p-daemon test suite":
test "Simple spawn and get identity test": test "Simple spawn and get identity test":
check: check:
waitFor(identitySpawnTest()) == true waitFor(identitySpawnTest()) == true
test "Connect/Accept peer/stream test":
check:
waitFor(connectStreamTest()) == true