Skip to content

Commit 165c0c1

Browse files
committed
Update tests
1 parent 15d0d1a commit 165c0c1

File tree

3 files changed

+64
-19
lines changed

3 files changed

+64
-19
lines changed

lib/live_debugger/services/garbage_collector/actions/garbage_collecting.ex

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ defmodule LiveDebugger.Services.GarbageCollector.Actions.GarbageCollecting do
55

66
alias LiveDebugger.API.StatesStorage
77
alias LiveDebugger.API.TracesStorage
8+
alias LiveDebugger.Services.GarbageCollector.GenServers.GarbageCollector
89

910
alias LiveDebugger.Bus
1011
alias LiveDebugger.Services.GarbageCollector.Events.TableTrimmed
@@ -14,7 +15,12 @@ defmodule LiveDebugger.Services.GarbageCollector.Actions.GarbageCollecting do
1415
@watched_table_size 50 * @megabyte_unit
1516
@non_watched_table_size 5 * @megabyte_unit
1617

17-
@spec garbage_collect_traces!(map(), MapSet.t(pid()), MapSet.t(pid())) :: MapSet.t(pid())
18+
@doc """
19+
Performs garbage collection on traces based on `to_remove`, `watched_pids`, and `alive_pids` sets.
20+
Returns a set of PIDs marked for removal in next cycle.
21+
"""
22+
@spec garbage_collect_traces!(GarbageCollector.state(), MapSet.t(pid()), MapSet.t(pid())) ::
23+
to_remove :: MapSet.t(pid())
1824
def garbage_collect_traces!(%{to_remove: to_remove}, watched_pids, alive_pids) do
1925
TracesStorage.get_all_tables()
2026
|> Enum.map(fn {pid, table} ->
@@ -31,7 +37,12 @@ defmodule LiveDebugger.Services.GarbageCollector.Actions.GarbageCollecting do
3137
|> aggregate_results()
3238
end
3339

34-
@spec garbage_collect_states!(map(), MapSet.t(pid()), MapSet.t(pid())) :: MapSet.t(pid())
40+
@doc """
41+
Performs garbage collection on states based on `to_remove`, `watched_pids`, and `alive_pids` sets.
42+
Returns a set of PIDs marked for removal in next cycle.
43+
"""
44+
@spec garbage_collect_states!(GarbageCollector.state(), MapSet.t(pid()), MapSet.t(pid())) ::
45+
to_remove :: MapSet.t(pid())
3546
def garbage_collect_states!(%{to_remove: to_remove}, watched_pids, alive_pids) do
3647
StatesStorage.get_all_states()
3748
|> Enum.map(fn {pid, _} ->

lib/live_debugger/services/garbage_collector/gen_servers/garbage_collector.ex

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ defmodule LiveDebugger.Services.GarbageCollector.GenServers.GarbageCollector do
1616

1717
@garbage_collect_interval 2000
1818

19+
@type state :: %{
20+
garbage_collection_enabled?: boolean(),
21+
to_remove: MapSet.t(pid())
22+
}
23+
1924
def start_link(opts \\ []) do
2025
GenServer.start_link(__MODULE__, opts, name: __MODULE__)
2126
end
@@ -46,26 +51,20 @@ defmodule LiveDebugger.Services.GarbageCollector.GenServers.GarbageCollector do
4651
end
4752

4853
# Handle messages related to ETS table transfers from TracesStorage
49-
@impl true
5054
def handle_info({:"ETS-TRANSFER", _ref, _from, _}, state) do
5155
{:noreply, state}
5256
end
5357

54-
@impl true
5558
def handle_info(%UserChangedSettings{key: :garbage_collection, value: true}, state) do
5659
resume_garbage_collection()
5760
{:noreply, Map.put(state, :garbage_collection_enabled?, true)}
5861
end
5962

60-
@impl true
6163
def handle_info(%UserChangedSettings{key: :garbage_collection, value: false}, state) do
6264
{:noreply, Map.put(state, :garbage_collection_enabled?, false)}
6365
end
6466

65-
@impl true
66-
def handle_info(_, state) do
67-
{:noreply, state}
68-
end
67+
def handle_info(_, state), do: {:noreply, state}
6968

7069
defp loop_garbage_collection() do
7170
Process.send_after(

test/services/garbage_collector/actions/garbage_collecting_test.exs

Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ defmodule LiveDebugger.Services.GarbageCollector.Actions.GarbageCollectingTest d
2525
alive_pids = MapSet.new([pid2])
2626
table1 = make_ref()
2727
table2 = make_ref()
28+
state = %{to_remove: MapSet.new()}
2829

2930
max_table_size_watched = 50 * @megabyte_unit
3031
max_table_size_non_watched = 5 * @megabyte_unit
@@ -39,7 +40,8 @@ defmodule LiveDebugger.Services.GarbageCollector.Actions.GarbageCollectingTest d
3940
MockBus
4041
|> expect(:broadcast_event!, 2, fn %TableTrimmed{} -> :ok end)
4142

42-
assert true == GarbageCollectingActions.garbage_collect_traces!(watched_pids, alive_pids)
43+
assert MapSet.new() ==
44+
GarbageCollectingActions.garbage_collect_traces!(state, watched_pids, alive_pids)
4345
end
4446

4547
test "does not collect garbage if max size not exceeded" do
@@ -49,6 +51,7 @@ defmodule LiveDebugger.Services.GarbageCollector.Actions.GarbageCollectingTest d
4951
alive_pids = MapSet.new([pid2])
5052
table1 = make_ref()
5153
table2 = make_ref()
54+
state = %{to_remove: MapSet.new()}
5255

5356
MockAPITracesStorage
5457
|> expect(:get_all_tables, fn -> [{pid1, table1}, {pid2, table2}] end)
@@ -59,44 +62,75 @@ defmodule LiveDebugger.Services.GarbageCollector.Actions.GarbageCollectingTest d
5962
MockBus
6063
|> deny(:broadcast_event!, 2)
6164

62-
assert false == GarbageCollectingActions.garbage_collect_traces!(watched_pids, alive_pids)
65+
assert MapSet.new() ==
66+
GarbageCollectingActions.garbage_collect_traces!(state, watched_pids, alive_pids)
6367
end
6468

65-
test "deletes table if not watched and no alive" do
69+
test "marks for removal if not watched and not alive" do
6670
watched_pids = MapSet.new()
6771
alive_pids = MapSet.new()
6872
pid1 = :c.pid(0, 11, 0)
6973
table1 = make_ref()
74+
state = %{to_remove: MapSet.new([])}
75+
76+
expect(MockAPITracesStorage, :get_all_tables, fn -> [{pid1, table1}] end)
77+
78+
assert MapSet.new([pid1]) ==
79+
GarbageCollectingActions.garbage_collect_traces!(state, watched_pids, alive_pids)
80+
end
81+
82+
test "deletes table if not watched, not alive and marked to remove" do
83+
watched_pids = MapSet.new()
84+
alive_pids = MapSet.new()
85+
pid1 = :c.pid(0, 11, 0)
86+
table1 = make_ref()
87+
state = %{to_remove: MapSet.new([pid1])}
7088

7189
expect(MockAPITracesStorage, :get_all_tables, fn -> [{pid1, table1}] end)
7290
expect(MockAPITracesStorage, :delete_table!, fn ^table1 -> :ok end)
7391
expect(MockBus, :broadcast_event!, fn %TableDeleted{} -> :ok end)
7492

75-
assert true == GarbageCollectingActions.garbage_collect_traces!(watched_pids, alive_pids)
93+
assert MapSet.new() ==
94+
GarbageCollectingActions.garbage_collect_traces!(state, watched_pids, alive_pids)
7695
end
7796
end
7897

7998
describe "garbage_collect_states!/1" do
80-
test "collects garbage for states if pids are not watched and not alive" do
99+
test "marks for removal if not watched and not alive" do
81100
pid1 = :c.pid(0, 12, 0)
82101
watched_pids = MapSet.new([:c.pid(0, 11, 0)])
83102
alive_pids = MapSet.new([:c.pid(0, 13, 0)])
103+
state = %{to_remove: MapSet.new()}
104+
105+
MockAPIStatesStorage
106+
|> expect(:get_all_states, fn -> [{pid1, :some_state}] end)
107+
108+
assert MapSet.new([pid1]) ==
109+
GarbageCollectingActions.garbage_collect_states!(state, watched_pids, alive_pids)
110+
end
111+
112+
test "deletes states if not watched, not alive and marked for removal " do
113+
pid1 = :c.pid(0, 12, 0)
114+
watched_pids = MapSet.new([:c.pid(0, 11, 0)])
115+
alive_pids = MapSet.new([:c.pid(0, 13, 0)])
116+
state = %{to_remove: MapSet.new([pid1])}
84117

85118
MockAPIStatesStorage
86119
|> expect(:get_all_states, fn -> [{pid1, :some_state}] end)
87120
|> expect(:delete!, fn ^pid1 -> :ok end)
88121

89-
MockBus
90-
|> expect(:broadcast_event!, fn %TableTrimmed{} -> :ok end)
122+
expect(MockBus, :broadcast_event!, fn %TableTrimmed{} -> :ok end)
91123

92-
assert true == GarbageCollectingActions.garbage_collect_states!(watched_pids, alive_pids)
124+
assert MapSet.new() ==
125+
GarbageCollectingActions.garbage_collect_states!(state, watched_pids, alive_pids)
93126
end
94127

95-
test "does not collect garbage for states if pids are watched or alive" do
128+
test "do nothing if pids are watched or alive" do
96129
pid1 = :c.pid(0, 12, 0)
97130
pid2 = :c.pid(0, 13, 0)
98131
watched_pids = MapSet.new([pid1])
99132
alive_pids = MapSet.new([pid2])
133+
state = %{to_remove: MapSet.new()}
100134

101135
MockAPIStatesStorage
102136
|> expect(:get_all_states, fn -> [{pid1, :some_state}, {pid2, :some_other_state}] end)
@@ -105,7 +139,8 @@ defmodule LiveDebugger.Services.GarbageCollector.Actions.GarbageCollectingTest d
105139
MockBus
106140
|> deny(:broadcast_event!, 1)
107141

108-
assert false == GarbageCollectingActions.garbage_collect_states!(watched_pids, alive_pids)
142+
assert MapSet.new() ==
143+
GarbageCollectingActions.garbage_collect_states!(state, watched_pids, alive_pids)
109144
end
110145
end
111146
end

0 commit comments

Comments
 (0)