nim-libp2p/tests/testdaemon.nim

46 lines
1.4 KiB
Nim
Raw Normal View History

2018-11-19 04:34:05 +00:00
import unittest
import asyncdispatch2
import ../libp2p/daemon/daemonapi
proc identitySpawnTest(): Future[bool] {.async.} =
var api = await newDaemonApi(sockpath = "/tmp/p2pd-1.sock")
var data = await api.identity()
await api.close()
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)
2018-11-19 20:53:20 +00:00
await api1.close()
await api2.close()
result = true
2018-11-19 04:34:05 +00:00
when isMainModule:
suite "libp2p-daemon test suite":
test "Simple spawn and get identity test":
2018-11-19 04:42:50 +00:00
check:
waitFor(identitySpawnTest()) == true
test "Connect/Accept peer/stream test":
check:
2018-11-19 20:53:20 +00:00
waitFor(connectStreamTest()) == true