nim-libp2p/tests/testswitch.nim

106 lines
3.3 KiB
Nim
Raw Normal View History

import unittest, tables, options
import chronos
import ../libp2p/[switch,
2019-09-30 15:48:40 +00:00
multistream,
protocols/identify,
2019-09-30 15:48:40 +00:00
connection,
transports/transport,
transports/tcptransport,
multiaddress,
2019-09-30 15:48:40 +00:00
peerinfo,
crypto/crypto,
2019-09-30 15:48:40 +00:00
peer,
protocols/protocol,
2019-09-30 15:48:40 +00:00
muxers/muxer,
muxers/mplex/mplex,
2019-09-30 15:48:40 +00:00
muxers/mplex/types,
protocols/secure/secio,
protocols/secure/secure]
2019-08-31 17:58:49 +00:00
when defined(nimHasUsed): {.used.}
2019-08-31 17:58:49 +00:00
const TestCodec = "/test/proto/1.0.0"
type
TestProto = ref object of LPProtocol
method init(p: TestProto) {.gcsafe.} =
proc handle(conn: Connection, proto: string) {.async, gcsafe.} =
2019-08-31 17:58:49 +00:00
let msg = cast[string](await conn.readLp())
check "Hello!" == msg
await conn.writeLp("Hello!")
2019-09-08 07:43:33 +00:00
await conn.close()
2019-08-31 17:58:49 +00:00
2019-08-31 18:52:56 +00:00
p.codec = TestCodec
2019-08-31 17:58:49 +00:00
p.handler = handle
2019-12-23 18:44:51 +00:00
proc createSwitch(ma: MultiAddress): (Switch, PeerInfo) =
let seckey = PrivateKey.random(RSA)
var peerInfo: PeerInfo = PeerInfo.init(PrivateKey.random(RSA))
peerInfo.addrs.add(ma)
let identify = newIdentify(peerInfo)
proc createMplex(conn: Connection): Muxer =
result = newMplex(conn)
let mplexProvider = newMuxerProvider(createMplex, MplexCodec)
let transports = @[Transport(newTransport(TcpTransport))]
let muxers = [(MplexCodec, mplexProvider)].toTable()
let secureManagers = [(SecioCodec, Secure(newSecio(peerInfo.privateKey)))].toTable()
let switch = newSwitch(peerInfo,
transports,
identify,
muxers,
secureManagers)
result = (switch, peerInfo)
2019-09-04 22:00:39 +00:00
2019-12-23 18:44:51 +00:00
suite "Switch":
test "e2e use switch dial proto string":
2019-08-31 17:58:49 +00:00
proc testSwitch(): Future[bool] {.async, gcsafe.} =
2019-09-30 15:48:40 +00:00
let ma1: MultiAddress = Multiaddress.init("/ip4/0.0.0.0/tcp/0")
let ma2: MultiAddress = Multiaddress.init("/ip4/0.0.0.0/tcp/0")
2019-08-31 17:58:49 +00:00
var peerInfo1, peerInfo2: PeerInfo
var switch1, switch2: Switch
(switch1, peerInfo1) = createSwitch(ma1)
2019-09-05 15:19:39 +00:00
2019-09-04 22:00:39 +00:00
let testProto = new TestProto
2019-09-05 15:19:39 +00:00
testProto.init()
2019-09-04 22:00:39 +00:00
testProto.codec = TestCodec
2019-08-31 17:58:49 +00:00
switch1.mount(testProto)
PubSub (Gossip & Flood) Implementation (#36) This adds gossipsub and floodsub, as well as basic interop testing with the go libp2p daemon. * add close event * wip: gossipsub * splitting rpc message * making message handling more consistent * initial gossipsub implementation * feat: nim 1.0 cleanup * wip: gossipsub protobuf * adding encoding/decoding of gossipsub messages * add disconnect handler * add proper gossipsub msg handling * misc: cleanup for nim 1.0 * splitting floodsub and gossipsub tests * feat: add mesh rebalansing * test pubsub * add mesh rebalansing tests * testing mesh maintenance * finishing mcache implementatin * wip: commenting out broken tests * wip: don't run heartbeat for now * switchout debug for trace logging * testing gossip peer selection algorithm * test stream piping * more work around message amplification * get the peerid from message * use timed cache as backing store * allow setting timeout in constructor * several changes to improve performance * more through testing of msg amplification * prevent gc issues * allow piping to self and prevent deadlocks * improove floodsub * allow running hook on cache eviction * prevent race conditions * prevent race conditions and improove tests * use hashes as cache keys * removing useless file * don't create a new seq * re-enable pubsub tests * fix imports * reduce number of runs to speed up tests * break out control message processing * normalize sleeps between steps * implement proper transport filtering * initial interop testing * clean up floodsub publish logic * allow dialing without a protocol * adding multiple reads/writes * use protobuf varint in mplex * don't loose conn's peerInfo * initial interop pubsub tests * don't duplicate connections/peers * bring back interop tests * wip: interop * re-enable interop and daemon tests * add multiple read write tests from handlers * don't cleanup channel prematurely * use correct channel to send/receive msgs * adjust tests with latest changes * include interop tests * remove temp logging output * fix ci * use correct public key serialization * additional tests for pubsub interop
2019-12-06 02:16:18 +00:00
asyncCheck switch1.start()
2019-08-31 17:58:49 +00:00
(switch2, peerInfo2) = createSwitch(ma2)
PubSub (Gossip & Flood) Implementation (#36) This adds gossipsub and floodsub, as well as basic interop testing with the go libp2p daemon. * add close event * wip: gossipsub * splitting rpc message * making message handling more consistent * initial gossipsub implementation * feat: nim 1.0 cleanup * wip: gossipsub protobuf * adding encoding/decoding of gossipsub messages * add disconnect handler * add proper gossipsub msg handling * misc: cleanup for nim 1.0 * splitting floodsub and gossipsub tests * feat: add mesh rebalansing * test pubsub * add mesh rebalansing tests * testing mesh maintenance * finishing mcache implementatin * wip: commenting out broken tests * wip: don't run heartbeat for now * switchout debug for trace logging * testing gossip peer selection algorithm * test stream piping * more work around message amplification * get the peerid from message * use timed cache as backing store * allow setting timeout in constructor * several changes to improve performance * more through testing of msg amplification * prevent gc issues * allow piping to self and prevent deadlocks * improove floodsub * allow running hook on cache eviction * prevent race conditions * prevent race conditions and improove tests * use hashes as cache keys * removing useless file * don't create a new seq * re-enable pubsub tests * fix imports * reduce number of runs to speed up tests * break out control message processing * normalize sleeps between steps * implement proper transport filtering * initial interop testing * clean up floodsub publish logic * allow dialing without a protocol * adding multiple reads/writes * use protobuf varint in mplex * don't loose conn's peerInfo * initial interop pubsub tests * don't duplicate connections/peers * bring back interop tests * wip: interop * re-enable interop and daemon tests * add multiple read write tests from handlers * don't cleanup channel prematurely * use correct channel to send/receive msgs * adjust tests with latest changes * include interop tests * remove temp logging output * fix ci * use correct public key serialization * additional tests for pubsub interop
2019-12-06 02:16:18 +00:00
asyncCheck switch2.start()
2019-09-30 15:48:40 +00:00
let conn = await switch2.dial(switch1.peerInfo, TestCodec)
2019-08-31 17:58:49 +00:00
await conn.writeLp("Hello!")
let msg = cast[string](await conn.readLp())
check "Hello!" == msg
2019-09-13 00:57:11 +00:00
discard allFutures(switch1.stop(), switch2.stop())
2019-08-31 17:58:49 +00:00
result = true
check:
waitFor(testSwitch()) == true
2019-12-23 18:44:51 +00:00
test "e2e use switch no proto string":
proc testSwitch(): Future[bool] {.async, gcsafe.} =
let ma1: MultiAddress = Multiaddress.init("/ip4/0.0.0.0/tcp/0")
let ma2: MultiAddress = Multiaddress.init("/ip4/0.0.0.0/tcp/0")
var peerInfo1, peerInfo2: PeerInfo
var switch1, switch2: Switch
(switch1, peerInfo1) = createSwitch(ma1)
asyncCheck switch1.start()
(switch2, peerInfo2) = createSwitch(ma2)
asyncCheck switch2.start()
var conn = await switch2.dial(switch1.peerInfo)
check isNil(conn)
discard allFutures(switch1.stop(), switch2.stop())
result = true
check:
waitFor(testSwitch()) == true