Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions test/non_thread_safe_integer_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# frozen_string_literal: true

require "test_helper"

class TestNonThreadSafeInteger < Minitest::Test
def setup
@integer = ::Semian::Simple::Integer.new # Using non-thread-safe implementation
end

def teardown
@integer.destroy
end

# The concurrent test that should expose race conditions
# Race condition:
# Thread A calls increment(1)
# Thread B calls @atom.value = 0
# Thread A's increment can complete after the assignment, overwriting the reset
def test_concurrent_reset
threads = []

assert_equal(0, @integer.value, "Integer should be initialized to 0")

4.times do
threads << Thread.new do
10.times { @integer.increment(1) }
end
end

threads << Thread.new do
@integer.reset
end

threads.each(&:join)

assert_equal(0, @integer.value, "Integer should be 0 after reset")

@integer.increment(5)

assert_equal(5, @integer.value, "Integer should work normally after reset")
end
end