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 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006- Jean-Philippe Lang
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; either version 2
  8. # of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. require_relative '../test_helper'
  19. class IssueNestedSetConcurrencyTest < ActiveSupport::TestCase
  20. fixtures :projects, :users,
  21. :trackers, :projects_trackers,
  22. :enabled_modules,
  23. :issue_statuses,
  24. :enumerations
  25. self.use_transactional_tests = false
  26. def setup
  27. skip if sqlite?
  28. if mysql?
  29. connection = ActiveRecord::Base.connection_db_config.configuration_hash.deep_dup
  30. connection[:variables] = mysql8? ? { transaction_isolation: "READ-COMMITTED" } : { tx_isolation: "READ-COMMITTED" }
  31. ActiveRecord::Base.establish_connection connection
  32. end
  33. User.current = nil
  34. CustomField.delete_all
  35. end
  36. def teardown
  37. Issue.delete_all
  38. end
  39. def test_concurrency
  40. # Generates an issue and destroys it in order
  41. # to load all needed classes before starting threads
  42. i = Issue.generate!
  43. i.destroy
  44. root = Issue.generate!
  45. assert_difference 'Issue.count', 60 do
  46. threaded(3) do
  47. 10.times do
  48. i = Issue.generate! :parent_issue_id => root.id
  49. c1 = Issue.generate! :parent_issue_id => i.id
  50. c2 = Issue.generate! :parent_issue_id => i.id
  51. c3 = Issue.generate! :parent_issue_id => i.id
  52. c2.reload.destroy
  53. c1.reload.destroy
  54. end
  55. end
  56. end
  57. end
  58. def test_concurrent_subtasks_creation
  59. root = Issue.generate!
  60. assert_difference 'Issue.count', 30 do
  61. threaded(3) do
  62. 10.times do
  63. Issue.generate! :parent_issue_id => root.id
  64. end
  65. end
  66. end
  67. root.reload
  68. assert_equal [1, 62], [root.lft, root.rgt]
  69. children_bounds = root.children.sort_by(&:lft).map {|c| [c.lft, c.rgt]}.flatten
  70. assert_equal (2..61).to_a, children_bounds
  71. end
  72. def test_concurrent_subtask_removal
  73. with_settings :notified_events => [] do
  74. root = Issue.generate!
  75. 60.times do
  76. Issue.generate! :parent_issue_id => root.id
  77. end
  78. # pick 40 random subtask ids
  79. child_ids = Issue.where(root_id: root.id, parent_id: root.id).pluck(:id)
  80. ids_to_remove = child_ids.sample(40).shuffle
  81. ids_to_keep = child_ids - ids_to_remove
  82. # remove these from the set, using four parallel threads
  83. threads = []
  84. ids_to_remove.each_slice(10) do |ids|
  85. threads << Thread.new do
  86. ActiveRecord::Base.connection_pool.with_connection do
  87. begin
  88. ids.each do |id|
  89. Issue.find(id).update(parent_id: nil)
  90. end
  91. rescue => e
  92. Thread.current[:exception] = e.message
  93. end
  94. end
  95. end
  96. end
  97. threads.each do |thread|
  98. thread.join
  99. assert_nil thread[:exception]
  100. end
  101. assert_equal 20, Issue.where(parent_id: root.id).count
  102. Issue.where(id: ids_to_remove).each do |issue|
  103. assert_nil issue.parent_id
  104. assert_equal issue.id, issue.root_id
  105. assert_equal 1, issue.lft
  106. assert_equal 2, issue.rgt
  107. end
  108. root.reload
  109. assert_equal [1, 42], [root.lft, root.rgt]
  110. children_bounds = root.children.sort_by(&:lft).map {|c| [c.lft, c.rgt]}.flatten
  111. assert_equal (2..41).to_a, children_bounds
  112. end
  113. end
  114. private
  115. def threaded(count, &block)
  116. with_settings :notified_events => [] do
  117. threads = []
  118. count.times do |i|
  119. threads << Thread.new(i) do
  120. ActiveRecord::Base.connection_pool.with_connection do
  121. begin
  122. yield
  123. rescue => e
  124. Thread.current[:exception] = e.message
  125. end
  126. end
  127. end
  128. end
  129. threads.each do |thread|
  130. thread.join
  131. assert_nil thread[:exception]
  132. end
  133. end
  134. end
  135. end