2018-11-19 04:34:05 +00:00
|
|
|
import unittest
|
|
|
|
import asyncdispatch2
|
|
|
|
import ../libp2p/daemon/daemonapi
|
|
|
|
|
|
|
|
proc identitySpawnTest(): Future[bool] {.async.} =
|
2018-12-10 20:55:06 +00:00
|
|
|
var api = await newDaemonApi()
|
2018-11-19 04:34:05 +00:00
|
|
|
var data = await api.identity()
|
|
|
|
await api.close()
|
|
|
|
result = true
|
|
|
|
|
2018-11-19 17:30:40 +00:00
|
|
|
proc connectStreamTest(): Future[bool] {.async.} =
|
2018-12-10 20:55:06 +00:00
|
|
|
var api1 = await newDaemonApi()
|
|
|
|
var api2 = await newDaemonApi()
|
2018-11-19 17:30:40 +00:00
|
|
|
|
|
|
|
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)
|
2018-12-10 20:55:06 +00:00
|
|
|
echo await api1.listPeers()
|
2018-11-19 17:30:40 +00:00
|
|
|
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-12-10 10:38:12 +00:00
|
|
|
await stream.close()
|
2018-11-19 20:53:20 +00:00
|
|
|
await api1.close()
|
|
|
|
await api2.close()
|
2018-11-19 17:30:40 +00:00
|
|
|
result = true
|
|
|
|
|
2018-11-22 15:35:44 +00:00
|
|
|
proc provideBadCidTest(): Future[bool] {.async.} =
|
|
|
|
var cid = newSeq[byte](10)
|
|
|
|
var api = await newDaemonApi({DHTFull})
|
|
|
|
try:
|
|
|
|
await api.dhtProvide(cid)
|
|
|
|
result = false
|
|
|
|
except DaemonRemoteError:
|
|
|
|
result = true
|
2018-12-09 16:44:20 +00:00
|
|
|
finally:
|
|
|
|
await api.close()
|
|
|
|
|
|
|
|
proc pubsubTest(f: set[P2PDaemonFlags]): Future[bool] {.async.} =
|
|
|
|
var pubsubData = "TEST MESSAGE"
|
|
|
|
var msgData = cast[seq[byte]](pubsubData)
|
2018-12-10 20:55:06 +00:00
|
|
|
var api1, api2: DaemonAPI
|
|
|
|
if PSGossipSub in f:
|
|
|
|
api1 = await newDaemonApi(f, gossipsubHeartbeatInterval = 100,
|
|
|
|
gossipsubHeartbeatDelay = 100)
|
|
|
|
api2 = await newDaemonApi(f, gossipsubHeartbeatInterval = 100,
|
|
|
|
gossipsubHeartbeatDelay = 100)
|
|
|
|
else:
|
|
|
|
api1 = await newDaemonApi(f)
|
|
|
|
api2 = await newDaemonApi(f)
|
2018-12-09 16:44:20 +00:00
|
|
|
|
|
|
|
var id1 = await api1.identity()
|
|
|
|
var id2 = await api2.identity()
|
|
|
|
|
|
|
|
var resultsCount = 0
|
|
|
|
|
|
|
|
var topics10 = await api1.pubsubGetTopics()
|
|
|
|
var peers10 = await api1.pubsubListPeers("test-topic")
|
2018-12-10 20:55:06 +00:00
|
|
|
var topics20 = await api2.pubsubGetTopics()
|
|
|
|
var peers20 = await api2.pubsubListPeers("test-topic")
|
|
|
|
|
|
|
|
var handlerFuture1 = newFuture[void]()
|
|
|
|
var handlerFuture2 = newFuture[void]()
|
2018-12-09 16:44:20 +00:00
|
|
|
|
|
|
|
proc pubsubHandler1(api: DaemonAPI,
|
|
|
|
ticket: PubsubTicket,
|
|
|
|
message: PubSubMessage): Future[bool] {.async.} =
|
2018-12-10 20:55:06 +00:00
|
|
|
echo "handler1"
|
2018-12-09 16:44:20 +00:00
|
|
|
let smsg = cast[string](message.data)
|
|
|
|
if smsg == pubsubData:
|
|
|
|
inc(resultsCount)
|
2018-12-10 20:55:06 +00:00
|
|
|
handlerFuture1.complete()
|
2018-12-09 16:44:20 +00:00
|
|
|
# Callback must return `false` to close subscription channel.
|
2018-12-10 20:55:06 +00:00
|
|
|
result = false
|
2018-12-09 16:44:20 +00:00
|
|
|
|
|
|
|
proc pubsubHandler2(api: DaemonAPI,
|
|
|
|
ticket: PubsubTicket,
|
|
|
|
message: PubSubMessage): Future[bool] {.async.} =
|
2018-12-10 20:55:06 +00:00
|
|
|
echo "handler2"
|
2018-12-09 16:44:20 +00:00
|
|
|
let smsg = cast[string](message.data)
|
|
|
|
if smsg == pubsubData:
|
|
|
|
inc(resultsCount)
|
2018-12-10 20:55:06 +00:00
|
|
|
handlerFuture2.complete()
|
2018-12-09 16:44:20 +00:00
|
|
|
# Callback must return `false` to close subscription channel.
|
|
|
|
result = false
|
|
|
|
|
|
|
|
if len(topics10) == 0 and len(peers10) == 0 and
|
|
|
|
len(topics20) == 0 and len(peers20) == 0:
|
|
|
|
# Not subscribed to any topics everything must be 0.
|
|
|
|
|
2018-12-10 20:55:06 +00:00
|
|
|
await api1.connect(id2.peer, id2.addresses)
|
|
|
|
await api2.connect(id1.peer, id1.addresses)
|
|
|
|
|
|
|
|
var gpeers1 = await api1.listPeers()
|
|
|
|
var gpeers2 = await api2.listPeers()
|
|
|
|
echo "globalPeers1 = ", gpeers1
|
|
|
|
echo "globalPeers2 = ", gpeers2
|
|
|
|
|
2018-12-09 16:44:20 +00:00
|
|
|
var ticket1 = await api1.pubsubSubscribe("test-topic", pubsubHandler1)
|
|
|
|
var ticket2 = await api2.pubsubSubscribe("test-topic", pubsubHandler2)
|
|
|
|
|
2018-12-10 20:55:06 +00:00
|
|
|
var topics1 = await api1.pubsubGetTopics()
|
|
|
|
var topics2 = await api2.pubsubGetTopics()
|
|
|
|
|
|
|
|
if len(topics1) == 1 and len(topics2) == 1:
|
|
|
|
echo "connecting"
|
|
|
|
|
|
|
|
while true:
|
|
|
|
var peers1 = await api1.pubsubListPeers("test-topic")
|
|
|
|
var peers2 = await api2.pubsubListPeers("test-topic")
|
|
|
|
if len(peers1) == 1 and len(peers2) == 1:
|
|
|
|
break
|
|
|
|
echo "pubsubPeers1 = ", peers1
|
|
|
|
echo "pubsubPeers2 = ", peers2
|
|
|
|
# var gpeers1 = await api1.listPeers()
|
|
|
|
# var gpeers2 = await api2.listPeers()
|
|
|
|
# echo "globalPeers1 = ", gpeers1
|
|
|
|
# echo "globalPeers2 = ", gpeers2
|
|
|
|
await sleepAsync(500)
|
|
|
|
|
|
|
|
|
|
|
|
# if len(topics12) == 1 and len(peers12) == 1 and
|
|
|
|
# len(topics22) == 1 and len(peers22) == 1:
|
|
|
|
echo "Publishing"
|
|
|
|
# Publish test data via api1.
|
|
|
|
await sleepAsync(500)
|
|
|
|
await api1.pubsubPublish("test-topic", msgData)
|
|
|
|
var andfut = handlerFuture1 and handlerFuture2
|
|
|
|
await andfut or sleepAsync(10000)
|
|
|
|
|
2018-12-09 16:44:20 +00:00
|
|
|
await api1.close()
|
|
|
|
await api2.close()
|
|
|
|
if resultsCount == 2:
|
|
|
|
result = true
|
2018-11-22 15:35:44 +00:00
|
|
|
|
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
|
2018-11-19 17:30:40 +00:00
|
|
|
test "Connect/Accept peer/stream test":
|
|
|
|
check:
|
2018-11-19 20:53:20 +00:00
|
|
|
waitFor(connectStreamTest()) == true
|
2018-11-22 15:35:44 +00:00
|
|
|
test "DHT provide bad CID test":
|
|
|
|
check:
|
2018-11-23 10:20:53 +00:00
|
|
|
waitFor(provideBadCidTest()) == true
|
2018-12-09 16:44:20 +00:00
|
|
|
test "GossipSub test":
|
|
|
|
check:
|
|
|
|
waitFor(pubsubTest({PSGossipSub})) == true
|
|
|
|
test "FloodSub test":
|
|
|
|
check:
|
|
|
|
waitFor(pubsubTest({PSFloodSub})) == true
|
|
|
|
|