Skip to content

Commit

Permalink
(experimental) ensure specs don't leak fibers or open ports
Browse files Browse the repository at this point in the history
  • Loading branch information
straight-shoota committed Dec 19, 2018
1 parent e4e0a5d commit fd782e2
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion src/spec/methods.cr
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@
module Spec::Methods
def ensure_no_leakage(file = __FILE__, line = __LINE__)
lsof_initial = `lsof -p #{Process.pid} -i TCP -a 2> /dev/null`
fiber_count_initial = Fiber.count

yield

lsof = `lsof -p #{Process.pid} -i TCP -a 2> /dev/null`
diff = Fiber.count - fiber_count_initial

if lsof != lsof_initial
fail "Leaking open file descriptor: #{lsof}", file, line
end
if diff != 0
fail "Leaking #{diff} fibers (#{Fiber.count} total)", file, line
end
end

# Defines an example group that describes a unit to be tested.
# Inside *&block* examples are defined by `#it` or `#pending`.
#
Expand Down Expand Up @@ -43,7 +60,9 @@ module Spec::Methods
start = Time.monotonic
begin
Spec.run_before_each_hooks
block.call

ensure_no_leakage(file, line) { block.call }

Spec::RootContext.report(:success, description, file, line, Time.monotonic - start)
rescue ex : Spec::AssertionFailed
Spec::RootContext.report(:fail, description, file, line, Time.monotonic - start, ex)
Expand Down

0 comments on commit fd782e2

Please sign in to comment.