wip: adding test
This commit is contained in:
parent
5b7e8d99bd
commit
47106a6a7d
|
@ -22,7 +22,7 @@ proc newConnection*(reader: AsyncStreamReader, writter: AsyncStreamWriter): Conn
|
|||
result.reader = reader
|
||||
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
|
||||
result = await c.reader.read(size)
|
||||
|
||||
|
|
|
@ -39,8 +39,13 @@ method listen*(t: TcpTransport): Future[void] {.async.} =
|
|||
let listenFuture: Future[void] = newFuture[void]()
|
||||
result = listenFuture
|
||||
|
||||
proc initTransport(server: StreamServer, fd: AsyncFD): StreamTransport {.gcsafe.} =
|
||||
t.server = server
|
||||
t.fd = fd
|
||||
listenFuture.complete()
|
||||
|
||||
## listen on the transport
|
||||
discard createStreamServer(t.ma,
|
||||
let server = createStreamServer(t.ma,
|
||||
connCb,
|
||||
{},
|
||||
t,
|
||||
|
@ -48,12 +53,8 @@ method listen*(t: TcpTransport): Future[void] {.async.} =
|
|||
100,
|
||||
DefaultStreamBufferSize,
|
||||
nil,
|
||||
proc (server: StreamServer,
|
||||
fd: AsyncFD): StreamTransport {.gcsafe.} =
|
||||
t.server = server
|
||||
t.fd = fd
|
||||
listenFuture.complete()
|
||||
)
|
||||
initTransport)
|
||||
server.start()
|
||||
|
||||
method dial*(t: TcpTransport,
|
||||
address: MultiAddress): Future[Connection] {.async.} =
|
||||
|
|
|
@ -27,7 +27,7 @@ method init*(t: Transport) {.base.} =
|
|||
## perform protocol initialization
|
||||
discard
|
||||
|
||||
proc new*(t: typedesc[Transport],
|
||||
proc newTransport*(t: typedesc[Transport],
|
||||
ma: MultiAddress,
|
||||
handler: ConnHandler): t =
|
||||
new result
|
||||
|
|
|
@ -361,3 +361,7 @@ suite "MultiAddress test suite":
|
|||
for bitem in item.bad:
|
||||
var a = MultiAddress.init(bitem)
|
||||
check item.pattern.match(a) == false
|
||||
|
||||
test "MultiAddress port":
|
||||
var maStr = "/ip4/127.0.0.1/tcp/55555"
|
||||
var ma = MultiAddress.init(maStr)
|
||||
|
|
Binary file not shown.
|
@ -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
|
Loading…
Reference in New Issue