Skip to content

Commit e31fec7

Browse files
committed
test: Improve concurrency to achieve 100% coverage
Increased the iteration count in concurrent tests and added explicit background contention (continuous toggling) to guarantee that :atomics.compare_exchange/4 hits its retry condition, stabilizing code coverage at 100%.
1 parent d6d2295 commit e31fec7

2 files changed

Lines changed: 17 additions & 8 deletions

File tree

test/abit/counter_test.exs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,9 @@ defmodule Abit.CounterTest do
132132

133133
test "add/3 concurrently to the same counter to trigger retry" do
134134
counter = Counter.new(10, 32)
135-
tasks = for _ <- 1..50, do: Task.async(fn -> Counter.add(counter, 1, 1) end)
135+
tasks = for _ <- 1..500, do: Task.async(fn -> Counter.add(counter, 1, 1) end)
136136
Enum.each(tasks, &Task.await/1)
137-
assert Counter.get(counter, 1) == 50
137+
assert Counter.get(counter, 1) == 500
138138
end
139139

140140
test "add/3 concurrently for different bits in the same atomics integer" do

test/abit_test.exs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -241,20 +241,29 @@ defmodule AbitTest do
241241

242242
test "set_bit_at/3 concurrently for different bits" do
243243
ref = :atomics.new(1, signed: false)
244+
245+
# Spawn tasks that toggle bit 0 concurrently to create contention on the atomics integer
246+
toggle_tasks = for _ <- 1..500, do: Task.async(fn -> Abit.toggle_bit_at(ref, 63) end)
247+
244248
tasks = for i <- 0..50, do: Task.async(fn -> Abit.set_bit_at(ref, i, 1) end)
245-
Enum.each(tasks, &Task.await/1)
246-
assert Abit.set_bits_count(ref) == 51
249+
250+
Enum.each(toggle_tasks ++ tasks, &Task.await/1)
251+
252+
# All bits 0..50 should be set
253+
for i <- 0..50 do
254+
assert Abit.bit_at(ref, i) == 1
255+
end
247256
end
248257

249258
test "toggle_bit_at/2 concurrently for the same bit" do
250259
ref = :atomics.new(1, signed: false)
251-
# Toggling 50 times should result in the bit being set to 0.
252-
tasks = for _ <- 1..50, do: Task.async(fn -> Abit.toggle_bit_at(ref, 0) end)
260+
# Toggling 500 times should result in the bit being set to 0.
261+
tasks = for _ <- 1..500, do: Task.async(fn -> Abit.toggle_bit_at(ref, 0) end)
253262
Enum.each(tasks, &Task.await/1)
254263
assert :atomics.get(ref, 1) == 0
255264

256-
# Toggling 51 times should result in the bit being set to 1.
257-
tasks = for _ <- 1..51, do: Task.async(fn -> Abit.toggle_bit_at(ref, 0) end)
265+
# Toggling 501 times should result in the bit being set to 1.
266+
tasks = for _ <- 1..501, do: Task.async(fn -> Abit.toggle_bit_at(ref, 0) end)
258267
Enum.each(tasks, &Task.await/1)
259268
assert :atomics.get(ref, 1) == 1
260269
end

0 commit comments

Comments
 (0)