Skip to content

Commit 5e1db04

Browse files
committed
More test coverage.
1 parent 6b4b980 commit 5e1db04

File tree

5 files changed

+37
-2
lines changed

5 files changed

+37
-2
lines changed

lib/memory/leak/cluster.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ def sample!
9494
#
9595
# @yields {|process_id, monitor| ...} each process ID and monitor that is leaking or exceeds the memory limit.
9696
def check!(&block)
97+
return to_enum(__method__) unless block_given?
98+
9799
self.sample!
98100

99101
leaking = []

lib/memory/leak/monitor.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# Copyright, 2025, by Samuel Williams.
55

66
require "console"
7+
require_relative "system"
78

89
module Memory
910
module Leak

lib/memory/leak/system.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ def self.total_memory_size
1515
return total.to_i * 1024
1616
end
1717
end
18-
19-
return nil
2018
end
2119
elsif RUBY_PLATFORM =~ /darwin/
2220
def self.total_memory_size

test/memory/leak/cluster.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,20 @@
1313

1414
let(:cluster) {subject.new}
1515

16+
with "#as_json" do
17+
it "generates a JSON representation" do
18+
expect(cluster.as_json).to have_keys(
19+
processes: be_a(Hash),
20+
total_size: be_nil,
21+
total_size_limit: be_nil,
22+
)
23+
end
24+
25+
it "generates a JSON string" do
26+
expect(JSON.dump(cluster)).to be == cluster.to_json
27+
end
28+
end
29+
1630
with "a leaking child process" do
1731
before do
1832
@children = 3.times.map do
@@ -65,6 +79,8 @@
6579
# 100 MiB limit:
6680
cluster.total_size_limit = 1024*1024*100
6781

82+
expect(cluster.check!.to_a).to be(:empty?)
83+
6884
big_child = children.values.first
6985
big_monitor = cluster.processes[big_child.process_id]
7086
small_child = children.values.last

test/memory/leak/monitor.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,24 @@
99
describe Memory::Leak::Monitor do
1010
let(:monitor) {subject.new}
1111

12+
with "#as_json" do
13+
it "generates a JSON representation" do
14+
expect(monitor.as_json).to have_keys(
15+
process_id: be_a(Integer),
16+
current_size: be_nil,
17+
maximum_size: be_nil,
18+
maximum_size_limit: be_nil,
19+
threshold_size: be_a(Integer),
20+
increase_count: be == 0,
21+
increase_limit: be_a(Integer)
22+
)
23+
end
24+
25+
it "generates a JSON string" do
26+
expect(JSON.dump(monitor)).to be == monitor.to_json
27+
end
28+
end
29+
1230
with "#sample!" do
1331
it "can capture samples" do
1432
3.times do

0 commit comments

Comments
 (0)