use new Chronos `trackCounter` APIs for leaks checks in tests (#1038)
This commit is contained in:
parent
6c873481ac
commit
44cada9c55
|
@ -1,5 +1,5 @@
|
||||||
# Nim-LibP2P
|
# Nim-LibP2P
|
||||||
# Copyright (c) 2023 Status Research & Development GmbH
|
# Copyright (c) 2023-2024 Status Research & Development GmbH
|
||||||
# Licensed under either of
|
# Licensed under either of
|
||||||
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
|
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
|
||||||
# * MIT license ([LICENSE-MIT](LICENSE-MIT))
|
# * MIT license ([LICENSE-MIT](LICENSE-MIT))
|
||||||
|
@ -77,30 +77,6 @@ type
|
||||||
opened*: uint64
|
opened*: uint64
|
||||||
closed*: uint64
|
closed*: uint64
|
||||||
|
|
||||||
proc setupStreamTracker*(name: string): StreamTracker =
|
|
||||||
let tracker = new StreamTracker
|
|
||||||
|
|
||||||
proc dumpTracking(): string {.gcsafe.} =
|
|
||||||
return "Opened " & tracker.id & ": " & $tracker.opened & "\n" &
|
|
||||||
"Closed " & tracker.id & ": " & $tracker.closed
|
|
||||||
|
|
||||||
proc leakTransport(): bool {.gcsafe.} =
|
|
||||||
return (tracker.opened != tracker.closed)
|
|
||||||
|
|
||||||
tracker.id = name
|
|
||||||
tracker.opened = 0
|
|
||||||
tracker.closed = 0
|
|
||||||
tracker.dump = dumpTracking
|
|
||||||
tracker.isLeaked = leakTransport
|
|
||||||
addTracker(name, tracker)
|
|
||||||
|
|
||||||
return tracker
|
|
||||||
|
|
||||||
proc getStreamTracker(name: string): StreamTracker {.gcsafe.} =
|
|
||||||
result = cast[StreamTracker](getTracker(name))
|
|
||||||
if isNil(result):
|
|
||||||
result = setupStreamTracker(name)
|
|
||||||
|
|
||||||
proc newLPStreamReadError*(p: ref CatchableError): ref LPStreamReadError =
|
proc newLPStreamReadError*(p: ref CatchableError): ref LPStreamReadError =
|
||||||
var w = newException(LPStreamReadError, "Read stream failed")
|
var w = newException(LPStreamReadError, "Read stream failed")
|
||||||
w.msg = w.msg & ", originated from [" & $p.name & "] " & p.msg
|
w.msg = w.msg & ", originated from [" & $p.name & "] " & p.msg
|
||||||
|
@ -157,7 +133,7 @@ method initStream*(s: LPStream) {.base.} =
|
||||||
s.oid = genOid()
|
s.oid = genOid()
|
||||||
|
|
||||||
libp2p_open_streams.inc(labelValues = [s.objName, $s.dir])
|
libp2p_open_streams.inc(labelValues = [s.objName, $s.dir])
|
||||||
inc getStreamTracker(s.objName).opened
|
trackCounter(s.objName)
|
||||||
trace "Stream created", s, objName = s.objName, dir = $s.dir
|
trace "Stream created", s, objName = s.objName, dir = $s.dir
|
||||||
|
|
||||||
proc join*(
|
proc join*(
|
||||||
|
@ -304,7 +280,7 @@ method closeImpl*(s: LPStream): Future[void] {.async, base.} =
|
||||||
## Implementation of close - called only once
|
## Implementation of close - called only once
|
||||||
trace "Closing stream", s, objName = s.objName, dir = $s.dir
|
trace "Closing stream", s, objName = s.objName, dir = $s.dir
|
||||||
libp2p_open_streams.dec(labelValues = [s.objName, $s.dir])
|
libp2p_open_streams.dec(labelValues = [s.objName, $s.dir])
|
||||||
inc getStreamTracker(s.objName).closed
|
untrackCounter(s.objName)
|
||||||
s.closeEvent.fire()
|
s.closeEvent.fire()
|
||||||
trace "Closed stream", s, objName = s.objName, dir = $s.dir
|
trace "Closed stream", s, objName = s.objName, dir = $s.dir
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
# Nim-LibP2P
|
# Nim-LibP2P
|
||||||
# Copyright (c) 2023 Status Research & Development GmbH
|
# Copyright (c) 2023-2024 Status Research & Development GmbH
|
||||||
# Licensed under either of
|
# Licensed under either of
|
||||||
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
|
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
|
||||||
# * MIT license ([LICENSE-MIT](LICENSE-MIT))
|
# * MIT license ([LICENSE-MIT](LICENSE-MIT))
|
||||||
|
@ -42,36 +42,8 @@ type
|
||||||
acceptFuts: seq[Future[StreamTransport]]
|
acceptFuts: seq[Future[StreamTransport]]
|
||||||
connectionsTimeout: Duration
|
connectionsTimeout: Duration
|
||||||
|
|
||||||
TcpTransportTracker* = ref object of TrackerBase
|
|
||||||
opened*: uint64
|
|
||||||
closed*: uint64
|
|
||||||
|
|
||||||
TcpTransportError* = object of transport.TransportError
|
TcpTransportError* = object of transport.TransportError
|
||||||
|
|
||||||
proc setupTcpTransportTracker(): TcpTransportTracker {.gcsafe, raises: [].}
|
|
||||||
|
|
||||||
proc getTcpTransportTracker(): TcpTransportTracker {.gcsafe.} =
|
|
||||||
result = cast[TcpTransportTracker](getTracker(TcpTransportTrackerName))
|
|
||||||
if isNil(result):
|
|
||||||
result = setupTcpTransportTracker()
|
|
||||||
|
|
||||||
proc dumpTracking(): string {.gcsafe.} =
|
|
||||||
var tracker = getTcpTransportTracker()
|
|
||||||
result = "Opened tcp transports: " & $tracker.opened & "\n" &
|
|
||||||
"Closed tcp transports: " & $tracker.closed
|
|
||||||
|
|
||||||
proc leakTransport(): bool {.gcsafe.} =
|
|
||||||
var tracker = getTcpTransportTracker()
|
|
||||||
result = (tracker.opened != tracker.closed)
|
|
||||||
|
|
||||||
proc setupTcpTransportTracker(): TcpTransportTracker =
|
|
||||||
result = new TcpTransportTracker
|
|
||||||
result.opened = 0
|
|
||||||
result.closed = 0
|
|
||||||
result.dump = dumpTracking
|
|
||||||
result.isLeaked = leakTransport
|
|
||||||
addTracker(TcpTransportTrackerName, result)
|
|
||||||
|
|
||||||
proc connHandler*(self: TcpTransport,
|
proc connHandler*(self: TcpTransport,
|
||||||
client: StreamTransport,
|
client: StreamTransport,
|
||||||
observedAddr: Opt[MultiAddress],
|
observedAddr: Opt[MultiAddress],
|
||||||
|
@ -163,7 +135,7 @@ method start*(
|
||||||
|
|
||||||
await procCall Transport(self).start(addrs)
|
await procCall Transport(self).start(addrs)
|
||||||
trace "Starting TCP transport"
|
trace "Starting TCP transport"
|
||||||
inc getTcpTransportTracker().opened
|
trackCounter(TcpTransportTrackerName)
|
||||||
|
|
||||||
for i, ma in addrs:
|
for i, ma in addrs:
|
||||||
if not self.handles(ma):
|
if not self.handles(ma):
|
||||||
|
@ -217,7 +189,7 @@ method stop*(self: TcpTransport) {.async.} =
|
||||||
self.servers = @[]
|
self.servers = @[]
|
||||||
|
|
||||||
trace "Transport stopped"
|
trace "Transport stopped"
|
||||||
inc getTcpTransportTracker().closed
|
untrackCounter(TcpTransportTrackerName)
|
||||||
except CatchableError as exc:
|
except CatchableError as exc:
|
||||||
trace "Error shutting down tcp transport", exc = exc.msg
|
trace "Error shutting down tcp transport", exc = exc.msg
|
||||||
|
|
||||||
|
|
|
@ -35,25 +35,19 @@ const
|
||||||
ChronosStreamTrackerName
|
ChronosStreamTrackerName
|
||||||
]
|
]
|
||||||
|
|
||||||
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
|
|
||||||
|
|
||||||
template checkTracker*(name: string) =
|
template checkTracker*(name: string) =
|
||||||
var tracker = getTracker(name)
|
if isCounterLeaked(name):
|
||||||
if tracker.isLeaked():
|
let
|
||||||
checkpoint tracker.dump()
|
tracker = getTrackerCounter(name)
|
||||||
|
trackerDescription =
|
||||||
|
"Opened " & name & ": " & $tracker.opened & "\n" &
|
||||||
|
"Closed " & name & ": " & $tracker.closed
|
||||||
|
checkpoint trackerDescription
|
||||||
fail()
|
fail()
|
||||||
|
|
||||||
template checkTrackers*() =
|
template checkTrackers*() =
|
||||||
for tracker in testTrackers():
|
for name in trackerNames:
|
||||||
if tracker.isLeaked():
|
checkTracker(name)
|
||||||
checkpoint tracker.dump()
|
|
||||||
fail()
|
|
||||||
# Also test the GC is not fooling with us
|
# Also test the GC is not fooling with us
|
||||||
when defined(nimHasWarnBareExcept):
|
when defined(nimHasWarnBareExcept):
|
||||||
{.push warning[BareExcept]:off.}
|
{.push warning[BareExcept]:off.}
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
{.used.}
|
{.used.}
|
||||||
|
|
||||||
# Nim-Libp2p
|
# Nim-Libp2p
|
||||||
# Copyright (c) 2023 Status Research & Development GmbH
|
# Copyright (c) 2023-2024 Status Research & Development GmbH
|
||||||
# Licensed under either of
|
# Licensed under either of
|
||||||
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
|
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
|
||||||
# * MIT license ([LICENSE-MIT](LICENSE-MIT))
|
# * MIT license ([LICENSE-MIT](LICENSE-MIT))
|
||||||
|
@ -18,8 +18,7 @@ import ./helpers
|
||||||
|
|
||||||
suite "BufferStream":
|
suite "BufferStream":
|
||||||
teardown:
|
teardown:
|
||||||
# echo getTracker(BufferStreamTrackerName).dump()
|
checkTrackers()
|
||||||
check getTracker(BufferStreamTrackerName).isLeaked() == false
|
|
||||||
|
|
||||||
asyncTest "push data to buffer":
|
asyncTest "push data to buffer":
|
||||||
let buff = BufferStream.new()
|
let buff = BufferStream.new()
|
||||||
|
|
Loading…
Reference in New Issue