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.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2017 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 File.expand_path('../../test_helper', __FILE__)
  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? || mysql?
  28. User.current = nil
  29. CustomField.delete_all
  30. end
  31. def teardown
  32. Issue.delete_all
  33. end
  34. def test_concurrency
  35. # Generates an issue and destroys it in order
  36. # to load all needed classes before starting threads
  37. i = Issue.generate!
  38. i.destroy
  39. root = Issue.generate!
  40. assert_difference 'Issue.count', 60 do
  41. threaded(3) do
  42. 10.times do
  43. i = Issue.generate! :parent_issue_id => root.id
  44. c1 = Issue.generate! :parent_issue_id => i.id
  45. c2 = Issue.generate! :parent_issue_id => i.id
  46. c3 = Issue.generate! :parent_issue_id => i.id
  47. c2.reload.destroy
  48. c1.reload.destroy
  49. end
  50. end
  51. end
  52. end
  53. def test_concurrent_subtasks_creation
  54. root = Issue.generate!
  55. assert_difference 'Issue.count', 30 do
  56. threaded(3) do
  57. 10.times do
  58. Issue.generate! :parent_issue_id => root.id
  59. end
  60. end
  61. end
  62. root.reload
  63. assert_equal [1, 62], [root.lft, root.rgt]
  64. children_bounds = root.children.sort_by(&:lft).map {|c| [c.lft, c.rgt]}.flatten
  65. assert_equal (2..61).to_a, children_bounds
  66. end
  67. private
  68. def threaded(count, &block)
  69. with_settings :notified_events => [] do
  70. threads = []
  71. count.times do |i|
  72. threads << Thread.new(i) do
  73. ActiveRecord::Base.connection_pool.with_connection do
  74. begin
  75. yield
  76. rescue => e
  77. Thread.current[:exception] = e.message
  78. end
  79. end
  80. end
  81. end
  82. threads.each do |thread|
  83. thread.join
  84. assert_nil thread[:exception]
  85. end
  86. end
  87. end
  88. end