You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

45 lines
1.0KB

  1. require 'test/unit'
  2. require 'user'
  3. class ConccurentAccesTest < Test::Unit::TestCase
  4. def setup
  5. ActiveRecord::Base.establish_connection(:adapter => 'mysql', :database => 'test', :user => 'root', :password => 'm3rl1p', :socket => '/var/run/mysql/mysql.sock')
  6. ActiveRecord::Base.logger = Logger.new(File.open('test_db.log', 'a'))
  7. end
  8. def teardown
  9. joe = User.find 1
  10. joe.name = "Joe"
  11. joe.visit = 0
  12. joe.save
  13. billy = User.find 2
  14. billy.name = "Billy"
  15. billy.visit = 0
  16. billy.save
  17. end
  18. def test_reading
  19. joe = User.find(1)
  20. assert_equal "Joe", joe.name
  21. assert_equal 0, joe.visit
  22. end
  23. def test_concurrent_access_withoutlock
  24. joe = User.find(1)
  25. billy = User.find(1)
  26. joe.visit += 1
  27. billy.visit += 1
  28. joe.save
  29. billy.save
  30. assert_equal 2, User.find(1).visit
  31. end
  32. def test_concurrent_access
  33. joe = User.find(1, :lock => true)
  34. billy = User.find(1, :lock => true)
  35. joe.visit += 1
  36. billy.visit += 1
  37. joe.save
  38. billy.save
  39. assert_equal 2, User.find(1).visit
  40. end
  41. end