2020-09-21 17:48:19 +00:00
|
|
|
import std/unittest
|
|
|
|
|
2020-07-07 11:14:11 +00:00
|
|
|
import chronos, bearssl
|
2020-05-08 20:10:06 +00:00
|
|
|
|
|
|
|
import ../libp2p/transports/tcptransport
|
|
|
|
import ../libp2p/stream/bufferstream
|
2020-07-07 11:14:11 +00:00
|
|
|
import ../libp2p/crypto/crypto
|
2020-06-19 17:29:43 +00:00
|
|
|
import ../libp2p/stream/lpstream
|
2020-11-05 03:52:54 +00:00
|
|
|
import ../libp2p/muxers/mplex/lpchannel
|
|
|
|
import ../libp2p/protocols/secure/secure
|
2020-05-08 20:10:06 +00:00
|
|
|
|
2021-01-04 18:59:05 +00:00
|
|
|
export unittest
|
|
|
|
|
2020-05-08 20:10:06 +00:00
|
|
|
const
|
|
|
|
StreamTransportTrackerName = "stream.transport"
|
|
|
|
StreamServerTrackerName = "stream.server"
|
|
|
|
|
|
|
|
trackerNames = [
|
2020-11-05 03:52:54 +00:00
|
|
|
LPStreamTrackerName,
|
2020-09-21 17:48:19 +00:00
|
|
|
ConnectionTrackerName,
|
2020-11-05 03:52:54 +00:00
|
|
|
LPChannelTrackerName,
|
|
|
|
SecureConnTrackerName,
|
2020-05-08 20:10:06 +00:00
|
|
|
BufferStreamTrackerName,
|
|
|
|
TcpTransportTrackerName,
|
|
|
|
StreamTransportTrackerName,
|
|
|
|
StreamServerTrackerName
|
|
|
|
]
|
|
|
|
|
|
|
|
iterator testTrackers*(extras: openArray[string] = []): TrackerBase =
|
|
|
|
for name in trackerNames:
|
|
|
|
let t = getTracker(name)
|
|
|
|
if not isNil(t): yield t
|
|
|
|
for name in extras:
|
|
|
|
let t = getTracker(name)
|
|
|
|
if not isNil(t): yield t
|
2020-07-07 11:14:11 +00:00
|
|
|
|
2020-11-06 15:24:24 +00:00
|
|
|
template checkTracker*(name: string) =
|
|
|
|
var tracker = getTracker(LPChannelTrackerName)
|
|
|
|
if tracker.isLeaked():
|
|
|
|
checkpoint tracker.dump()
|
|
|
|
fail()
|
|
|
|
|
2020-09-21 17:48:19 +00:00
|
|
|
template checkTrackers*() =
|
|
|
|
for tracker in testTrackers():
|
|
|
|
if tracker.isLeaked():
|
|
|
|
checkpoint tracker.dump()
|
|
|
|
fail()
|
2020-11-04 14:24:41 +00:00
|
|
|
# Also test the GC is not fooling with us
|
|
|
|
GC_fullCollect()
|
2020-09-21 17:48:19 +00:00
|
|
|
|
2020-11-13 03:44:02 +00:00
|
|
|
template asyncTeardown*(body: untyped): untyped =
|
|
|
|
teardown:
|
|
|
|
waitFor((
|
2020-11-25 13:42:02 +00:00
|
|
|
proc() {.async, gcsafe.} =
|
2020-11-13 03:44:02 +00:00
|
|
|
body
|
|
|
|
)())
|
|
|
|
|
|
|
|
template asyncSetup*(body: untyped): untyped =
|
|
|
|
setup:
|
|
|
|
waitFor((
|
2020-11-25 13:42:02 +00:00
|
|
|
proc() {.async, gcsafe.} =
|
2020-11-13 03:44:02 +00:00
|
|
|
body
|
|
|
|
)())
|
|
|
|
|
|
|
|
template asyncTest*(name: string, body: untyped): untyped =
|
|
|
|
test name:
|
|
|
|
waitFor((
|
2020-11-25 13:42:02 +00:00
|
|
|
proc() {.async, gcsafe.} =
|
2020-11-13 03:44:02 +00:00
|
|
|
body
|
|
|
|
)())
|
|
|
|
|
2020-07-07 11:14:11 +00:00
|
|
|
type RngWrap = object
|
|
|
|
rng: ref BrHmacDrbgContext
|
|
|
|
|
|
|
|
var rngVar: RngWrap
|
|
|
|
|
|
|
|
proc getRng(): ref BrHmacDrbgContext =
|
|
|
|
# TODO if `rngVar` is a threadvar like it should be, there are random and
|
|
|
|
# spurious compile failures on mac - this is not gcsafe but for the
|
|
|
|
# purpose of the tests, it's ok as long as we only use a single thread
|
|
|
|
{.gcsafe.}:
|
|
|
|
if rngVar.rng.isNil:
|
|
|
|
rngVar.rng = newRng()
|
|
|
|
rngVar.rng
|
|
|
|
|
|
|
|
template rng*(): ref BrHmacDrbgContext =
|
|
|
|
getRng()
|
refactor bufferstream to use a queue (#346)
This change modifies how the backpressure algorithm in bufferstream
works - in particular, instead of working byte-by-byte, it will now work
seq-by-seq.
When data arrives, it usually does so in packets - in the current
bufferstream, the packet is read then split into bytes which are fed one
by one to the bufferstream. On the reading side, the bytes are popped of
the bufferstream, again byte by byte, to satisfy `readOnce` requests -
this introduces a lot of synchronization traffic because the checks for
full buffer and for async event handling must be done for every byte.
In this PR, a queue of length 1 is used instead - this means there will
at most exist one "packet" in `pushTo`, one in the queue and one in the
slush buffer that is used to store incomplete reads.
* avoid byte-by-byte copy to buffer, with synchronization in-between
* reuse AsyncQueue synchronization logic instead of rolling own
* avoid writeHandler callback - implement `write` method instead
* simplify EOF signalling by only setting EOF flag in queue reader (and
reset)
* remove BufferStream pipes (unused)
* fixes drainBuffer deadlock when drain is called from within read loop
and thus blocks draining
* fix lpchannel init order
2020-09-10 06:19:13 +00:00
|
|
|
|
|
|
|
type
|
|
|
|
WriteHandler* = proc(data: seq[byte]): Future[void] {.gcsafe.}
|
|
|
|
TestBufferStream* = ref object of BufferStream
|
|
|
|
writeHandler*: WriteHandler
|
|
|
|
|
|
|
|
method write*(s: TestBufferStream, msg: seq[byte]): Future[void] =
|
|
|
|
s.writeHandler(msg)
|
|
|
|
|
|
|
|
proc newBufferStream*(writeHandler: WriteHandler): TestBufferStream =
|
|
|
|
new result
|
|
|
|
result.writeHandler = writeHandler
|
|
|
|
result.initStream()
|