Skip to content

Commit 1427896

Browse files
alcoclaude
andcommitted
Use on_exit callbacks for reliable ETS table cleanup in tests
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 0146496 commit 1427896

1 file changed

Lines changed: 28 additions & 44 deletions

File tree

packages/electric-telemetry/test/electric/telemetry/ets_tables_test.exs

Lines changed: 28 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@ defmodule ElectricTelemetry.EtsTablesTest do
77
test "returns top N tables by memory usage" do
88
# Create some test tables
99
table1 = :ets.new(:test_table_1, [:public, :named_table])
10+
on_exit(fn -> if :ets.info(table1) != :undefined, do: :ets.delete(table1) end)
1011
table2 = :ets.new(:test_table_2, [:public, :named_table])
12+
on_exit(fn -> if :ets.info(table2) != :undefined, do: :ets.delete(table2) end)
1113
table3 = :ets.new(:test_table_3, [:public, :named_table])
14+
on_exit(fn -> if :ets.info(table3) != :undefined, do: :ets.delete(table3) end)
1215

1316
# Insert data to create memory usage differences
1417
for i <- 1..100, do: :ets.insert(table1, {i, :binary.copy(<<0>>, 1000)})
@@ -42,11 +45,6 @@ defmodule ElectricTelemetry.EtsTablesTest do
4245
# Check that results are sorted by memory (descending)
4346
memories = Enum.map(results, & &1.memory)
4447
assert memories == Enum.sort(memories, :desc)
45-
46-
# Cleanup
47-
:ets.delete(table1)
48-
:ets.delete(table2)
49-
:ets.delete(table3)
5048
end
5149

5250
test "handles different table counts" do
@@ -62,9 +60,13 @@ defmodule ElectricTelemetry.EtsTablesTest do
6260

6361
test "correctly calculates type statistics" do
6462
# Create tables with same type but different sizes
65-
table1 = :ets.new(:"TestType:stack_aaa", [:public])
66-
table2 = :ets.new(:"TestType:stack_bbb", [:public])
67-
table3 = :ets.new(:"TestType:stack_ccc", [:public])
63+
# Use UUID-like suffixes so type extraction groups them under "TestType"
64+
table1 = :ets.new(:"TestType:aaa11111-1111", [:public])
65+
on_exit(fn -> if :ets.info(table1) != :undefined, do: :ets.delete(table1) end)
66+
table2 = :ets.new(:"TestType:bbb22222-2222", [:public])
67+
on_exit(fn -> if :ets.info(table2) != :undefined, do: :ets.delete(table2) end)
68+
table3 = :ets.new(:"TestType:ccc33333-3333", [:public])
69+
on_exit(fn -> if :ets.info(table3) != :undefined, do: :ets.delete(table3) end)
6870

6971
# Insert different amounts of data to get different sizes
7072
# Using more data to ensure these tables appear in top results
@@ -90,20 +92,18 @@ defmodule ElectricTelemetry.EtsTablesTest do
9092
# Verify individual sizes
9193
sizes = Enum.map(test_tables, & &1.size) |> Enum.sort()
9294
assert sizes == [1000, 2000, 3000]
93-
94-
# Cleanup
95-
:ets.delete(table1)
96-
:ets.delete(table2)
97-
:ets.delete(table3)
9895
end
9996
end
10097

10198
describe "top_by_type/1" do
10299
test "groups tables by type and sums memory" do
103100
# Create tables with patterns that should be grouped
104101
table1 = :ets.new(:"Electric.Test:6dd7c00b-8e31-4cfa", [:public])
102+
on_exit(fn -> if :ets.info(table1) != :undefined, do: :ets.delete(table1) end)
105103
table2 = :ets.new(:"Electric.Test:61fec704-7dbf-49a5", [:public])
104+
on_exit(fn -> if :ets.info(table2) != :undefined, do: :ets.delete(table2) end)
106105
table3 = :ets.new(:"Another.Module:abcd1234-5678-9abc", [:public])
106+
on_exit(fn -> if :ets.info(table3) != :undefined, do: :ets.delete(table3) end)
107107

108108
# Insert some data
109109
for i <- 1..10, do: :ets.insert(table1, {i, :binary.copy(<<0>>, 100)})
@@ -135,17 +135,14 @@ defmodule ElectricTelemetry.EtsTablesTest do
135135
# Check that results are sorted by memory (descending)
136136
memories = Enum.map(results, & &1.memory)
137137
assert memories == Enum.sort(memories, :desc)
138-
139-
# Cleanup
140-
:ets.delete(table1)
141-
:ets.delete(table2)
142-
:ets.delete(table3)
143138
end
144139

145140
test "handles unnamed tables with same name" do
146141
# Create multiple unnamed tables
147142
table1 = :ets.new(:unnamed_test, [:public, :named_table])
143+
on_exit(fn -> if :ets.info(table1) != :undefined, do: :ets.delete(table1) end)
148144
table2 = :ets.new(:unnamed_test_2, [:public])
145+
on_exit(fn -> if :ets.info(table2) != :undefined, do: :ets.delete(table2) end)
149146

150147
# Insert data
151148
for i <- 1..5, do: :ets.insert(table1, {i, :data})
@@ -158,10 +155,6 @@ defmodule ElectricTelemetry.EtsTablesTest do
158155
assert unnamed_type != nil, "Expected to find unnamed_test type in results"
159156
assert unnamed_type.table_count >= 1
160157
assert is_integer(unnamed_type.memory)
161-
162-
# Cleanup
163-
:ets.delete(table1)
164-
:ets.delete(table2)
165158
end
166159

167160
test "respects the count parameter" do
@@ -172,9 +165,13 @@ defmodule ElectricTelemetry.EtsTablesTest do
172165

173166
test "correctly calculates average size for grouped types" do
174167
# Create tables with same type but different sizes
175-
table1 = :ets.new(:"GroupTest:id_111", [:public])
176-
table2 = :ets.new(:"GroupTest:id_222", [:public])
177-
table3 = :ets.new(:"GroupTest:id_333", [:public])
168+
# Use UUID-like suffixes so type extraction groups them under "GroupTest"
169+
table1 = :ets.new(:"GroupTest:11111111-1111", [:public])
170+
on_exit(fn -> if :ets.info(table1) != :undefined, do: :ets.delete(table1) end)
171+
table2 = :ets.new(:"GroupTest:22222222-2222", [:public])
172+
on_exit(fn -> if :ets.info(table2) != :undefined, do: :ets.delete(table2) end)
173+
table3 = :ets.new(:"GroupTest:33333333-3333", [:public])
174+
on_exit(fn -> if :ets.info(table3) != :undefined, do: :ets.delete(table3) end)
178175

179176
# Insert different amounts of data
180177
for i <- 1..1500, do: :ets.insert(table1, {i, :binary.copy(<<0>>, 100)})
@@ -192,11 +189,6 @@ defmodule ElectricTelemetry.EtsTablesTest do
192189
assert group_test_type.avg_size == 2500.0
193190
# Memory should be the sum of all 3 tables
194191
assert group_test_type.memory > 0
195-
196-
# Cleanup
197-
:ets.delete(table1)
198-
:ets.delete(table2)
199-
:ets.delete(table3)
200192
end
201193
end
202194

@@ -236,7 +228,9 @@ defmodule ElectricTelemetry.EtsTablesTest do
236228
describe "table type extraction" do
237229
test "extracts type from colon-separated stack_id pattern" do
238230
table1 = :ets.new(:"Electric.StatusMonitor:6dd7c00b-8e31", [:public])
231+
on_exit(fn -> if :ets.info(table1) != :undefined, do: :ets.delete(table1) end)
239232
table2 = :ets.new(:"shapedb:shape_lookup:61fec704-7dbf-49a5", [:public])
233+
on_exit(fn -> if :ets.info(table2) != :undefined, do: :ets.delete(table2) end)
240234

241235
results = EtsTables.top_tables(100)
242236

@@ -255,14 +249,11 @@ defmodule ElectricTelemetry.EtsTablesTest do
255249

256250
assert shapedb_result != nil, "Expected to find shapedb:shape_lookup table in results"
257251
assert shapedb_result.type == "shapedb:shape_lookup"
258-
259-
# Cleanup
260-
:ets.delete(table1)
261-
:ets.delete(table2)
262252
end
263253

264254
test "extracts type from underscore-separated stack_id pattern" do
265255
table1 = :ets.new(:stack_call_home_telemetry_6dd7c00b, [:public])
256+
on_exit(fn -> if :ets.info(table1) != :undefined, do: :ets.delete(table1) end)
266257

267258
results = EtsTables.top_tables(100)
268259

@@ -273,29 +264,26 @@ defmodule ElectricTelemetry.EtsTablesTest do
273264

274265
assert result != nil, "Expected to find stack_call_home_telemetry table in results"
275266
assert result.type == "stack_call_home_telemetry"
276-
277-
# Cleanup
278-
:ets.delete(table1)
279267
end
280268

281269
test "uses full name when no pattern is detected" do
282270
table1 = :ets.new(:simple_table_name, [:public, :named_table])
271+
on_exit(fn -> if :ets.info(table1) != :undefined, do: :ets.delete(table1) end)
283272

284273
results = EtsTables.top_tables(100)
285274

286275
result = Enum.find(results, fn %{name: name} -> name == :simple_table_name end)
287276

288277
assert result != nil, "Expected to find simple_table_name table in results"
289278
assert result.type == "simple_table_name"
290-
291-
# Cleanup
292-
:ets.delete(table1)
293279
end
294280

295281
test "handles partial UUID patterns correctly" do
296282
# Some production tables have truncated UUIDs
297283
table1 = :ets.new(:"Electric.Test:6dd7c00b", [:public])
284+
on_exit(fn -> if :ets.info(table1) != :undefined, do: :ets.delete(table1) end)
298285
table2 = :ets.new(:"Electric.Test:61fec704-7dbf", [:public])
286+
on_exit(fn -> if :ets.info(table2) != :undefined, do: :ets.delete(table2) end)
299287

300288
results = EtsTables.top_tables(100)
301289

@@ -307,10 +295,6 @@ defmodule ElectricTelemetry.EtsTablesTest do
307295

308296
assert result2 != nil, "Expected to find Electric.Test:61fec704-7dbf table in results"
309297
assert result2.type == "Electric.Test"
310-
311-
# Cleanup
312-
:ets.delete(table1)
313-
:ets.delete(table2)
314298
end
315299
end
316300
end

0 commit comments

Comments
 (0)