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.

issue_nested_set_concurrency_test.rb 2.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2017 Jean-Philippe Lang
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License
  6. # as published by the Free Software Foundation; either version 2
  7. # of the License, or (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. require File.expand_path('../../test_helper', __FILE__)
  18. class IssueNestedSetConcurrencyTest < ActiveSupport::TestCase
  19. fixtures :projects, :users,
  20. :trackers, :projects_trackers,
  21. :enabled_modules,
  22. :issue_statuses,
  23. :enumerations
  24. self.use_transactional_tests = false
  25. def setup
  26. skip if sqlite? || mysql?
  27. User.current = nil
  28. CustomField.delete_all
  29. end
  30. def teardown
  31. Issue.delete_all
  32. end
  33. def test_concurrency
  34. # Generates an issue and destroys it in order
  35. # to load all needed classes before starting threads
  36. i = Issue.generate!
  37. i.destroy
  38. root = Issue.generate!
  39. assert_difference 'Issue.count', 60 do
  40. threaded(3) do
  41. 10.times do
  42. i = Issue.generate! :parent_issue_id => root.id
  43. c1 = Issue.generate! :parent_issue_id => i.id
  44. c2 = Issue.generate! :parent_issue_id => i.id
  45. c3 = Issue.generate! :parent_issue_id => i.id
  46. c2.reload.destroy
  47. c1.reload.destroy
  48. end
  49. end
  50. end
  51. end
  52. def test_concurrent_subtasks_creation
  53. root = Issue.generate!
  54. assert_difference 'Issue.count', 30 do
  55. threaded(3) do
  56. 10.times do
  57. Issue.generate! :parent_issue_id => root.id
  58. end
  59. end
  60. end
  61. root.reload
  62. assert_equal [1, 62], [root.lft, root.rgt]
  63. children_bounds = root.children.sort_by(&:lft).map {|c| [c.lft, c.rgt]}.flatten
  64. assert_equal (2..61).to_a, children_bounds
  65. end
  66. private
  67. def threaded(count, &block)
  68. with_settings :notified_events => [] do
  69. threads = []
  70. count.times do |i|
  71. threads << Thread.new(i) do
  72. ActiveRecord::Base.connection_pool.with_connection do
  73. begin
  74. yield
  75. rescue Exception => e
  76. Thread.current[:exception] = e.message
  77. end
  78. end
  79. end
  80. end
  81. threads.each do |thread|
  82. thread.join
  83. assert_nil thread[:exception]
  84. end
  85. end
  86. end
  87. end