wip: adding test

This commit is contained in:
Dmitriy Ryajov 2019-08-20 22:58:12 -06:00
parent 5b7e8d99bd
commit 47106a6a7d
6 changed files with 41 additions and 14 deletions

View File

@ -22,7 +22,7 @@ proc newConnection*(reader: AsyncStreamReader, writter: AsyncStreamWriter): Conn
result.reader = reader result.reader = reader
result.writter = writter result.writter = writter
method read* (c: Connection, size: int = DefaultReadSize): Future[seq[byte]] {.base, async.} = method read* (c: Connection, size: int = DefaultReadSize): Future[seq[byte]] {.base, async, gcsafe.} =
## read DefaultReadSize (1024) bytes or `size` bytes if specified ## read DefaultReadSize (1024) bytes or `size` bytes if specified
result = await c.reader.read(size) result = await c.reader.read(size)

View File

@ -39,21 +39,22 @@ method listen*(t: TcpTransport): Future[void] {.async.} =
let listenFuture: Future[void] = newFuture[void]() let listenFuture: Future[void] = newFuture[void]()
result = listenFuture result = listenFuture
## listen on the transport proc initTransport(server: StreamServer, fd: AsyncFD): StreamTransport {.gcsafe.} =
discard createStreamServer(t.ma,
connCb,
{},
t,
asyncInvalidSocket,
100,
DefaultStreamBufferSize,
nil,
proc (server: StreamServer,
fd: AsyncFD): StreamTransport {.gcsafe.} =
t.server = server t.server = server
t.fd = fd t.fd = fd
listenFuture.complete() listenFuture.complete()
)
## listen on the transport
let server = createStreamServer(t.ma,
connCb,
{},
t,
asyncInvalidSocket,
100,
DefaultStreamBufferSize,
nil,
initTransport)
server.start()
method dial*(t: TcpTransport, method dial*(t: TcpTransport,
address: MultiAddress): Future[Connection] {.async.} = address: MultiAddress): Future[Connection] {.async.} =

View File

@ -27,7 +27,7 @@ method init*(t: Transport) {.base.} =
## perform protocol initialization ## perform protocol initialization
discard discard
proc new*(t: typedesc[Transport], proc newTransport*(t: typedesc[Transport],
ma: MultiAddress, ma: MultiAddress,
handler: ConnHandler): t = handler: ConnHandler): t =
new result new result

View File

@ -361,3 +361,7 @@ suite "MultiAddress test suite":
for bitem in item.bad: for bitem in item.bad:
var a = MultiAddress.init(bitem) var a = MultiAddress.init(bitem)
check item.pattern.match(a) == false check item.pattern.match(a) == false
test "MultiAddress port":
var maStr = "/ip4/127.0.0.1/tcp/55555"
var ma = MultiAddress.init(maStr)

BIN
tests/testtransport Executable file

Binary file not shown.

22
tests/testtransport.nim Normal file
View File

@ -0,0 +1,22 @@
import unittest
import chronos
import ../libp2p/connection, ../libp2p/transport, ../libp2p/tcptransport,
../libp2p/multiaddress, ../libp2p/wire
suite "TCP transport suite":
test "test listener":
proc testListener(): Future[bool] {.async.} =
let ma: MultiAddress = Multiaddress.init("/ip4/127.0.0.1/tcp/53335")
proc connHandler(conn: Connection): Future[void] {.gcsafe.} =
let msg = "Hello"
conn.write(msg.cstring, 6)
let transport: TcpTransport = newTransport(TcpTransport, ma, connHandler)
await transport.listen()
let streamTransport: StreamTransport = await connect(ma)
let msg = await streamTransport.read()
echo "HERE!!!!"
result = cast[string](msg) == "Hello!"
check:
waitFor(testListener()) == true