spec_helper.rb 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. # frozen_string_literal: true
  2. if ENV['DISABLE_SIMPLECOV'] != 'true'
  3. require 'simplecov'
  4. SimpleCov.start 'rails' do
  5. add_filter 'lib/linter'
  6. add_group 'Policies', 'app/policies'
  7. add_group 'Presenters', 'app/presenters'
  8. add_group 'Serializers', 'app/serializers'
  9. add_group 'Services', 'app/services'
  10. add_group 'Validators', 'app/validators'
  11. end
  12. end
  13. RSpec.configure do |config|
  14. config.example_status_persistence_file_path = 'tmp/rspec/examples.txt'
  15. config.expect_with :rspec do |expectations|
  16. expectations.include_chain_clauses_in_custom_matcher_descriptions = true
  17. end
  18. config.mock_with :rspec do |mocks|
  19. mocks.verify_partial_doubles = true
  20. config.around(:example, :without_verify_partial_doubles) do |example|
  21. mocks.verify_partial_doubles = false
  22. example.call
  23. mocks.verify_partial_doubles = true
  24. end
  25. end
  26. config.before :suite do
  27. Rails.application.load_seed
  28. Chewy.strategy(:bypass)
  29. # NOTE: we switched registrations mode to closed by default, but the specs
  30. # very heavily rely on having it enabled by default, as it relies on users
  31. # being approved by default except in select cases where explicitly testing
  32. # other registration modes
  33. Setting.registrations_mode = 'open'
  34. end
  35. config.after :suite do
  36. FileUtils.rm_rf(Dir[Rails.root.join('spec', 'test_files')])
  37. end
  38. end
  39. def body_as_json
  40. json_str_to_hash(response.body)
  41. end
  42. def json_str_to_hash(str)
  43. JSON.parse(str, symbolize_names: true)
  44. end
  45. def expect_push_bulk_to_match(klass, matcher)
  46. expect(Sidekiq::Client).to receive(:push_bulk).with(hash_including({
  47. 'class' => klass,
  48. 'args' => matcher,
  49. }))
  50. end
  51. class StreamingServerManager
  52. @running_thread = nil
  53. def initialize
  54. at_exit { stop }
  55. end
  56. def start(port: 4020)
  57. return if @running_thread
  58. queue = Queue.new
  59. @queue = queue
  60. @running_thread = Thread.new do
  61. Open3.popen2e(
  62. {
  63. 'REDIS_NAMESPACE' => ENV.fetch('REDIS_NAMESPACE'),
  64. 'DB_NAME' => "#{ENV.fetch('DB_NAME', 'mastodon')}_test#{ENV.fetch('TEST_ENV_NUMBER', '')}",
  65. 'RAILS_ENV' => ENV.fetch('RAILS_ENV', 'test'),
  66. 'NODE_ENV' => ENV.fetch('STREAMING_NODE_ENV', 'development'),
  67. 'PORT' => port.to_s,
  68. },
  69. 'node index.js', # must not call yarn here, otherwise it will fail because yarn does not send signals to its child process
  70. chdir: Rails.root.join('streaming')
  71. ) do |_stdin, stdout_err, process_thread|
  72. status = :starting
  73. # Spawn a thread to listen on streaming server output
  74. output_thread = Thread.new do
  75. stdout_err.each_line do |line|
  76. Rails.logger.info "Streaming server: #{line}"
  77. if status == :starting && line.match('Streaming API now listening on')
  78. status = :started
  79. @queue.enq 'started'
  80. end
  81. end
  82. end
  83. # And another thread to listen on commands from the main thread
  84. loop do
  85. msg = queue.pop
  86. case msg
  87. when 'stop'
  88. # we need to properly stop the reading thread
  89. output_thread.kill
  90. # Then stop the node process
  91. Process.kill('KILL', process_thread.pid)
  92. # And we stop ourselves
  93. @running_thread.kill
  94. end
  95. end
  96. end
  97. end
  98. # wait for 10 seconds for the streaming server to start
  99. Timeout.timeout(10) do
  100. loop do
  101. break if @queue.pop == 'started'
  102. end
  103. end
  104. end
  105. def stop
  106. return unless @running_thread
  107. @queue.enq 'stop'
  108. # Wait for the thread to end
  109. @running_thread.join
  110. end
  111. end
  112. class SearchDataManager
  113. def prepare_test_data
  114. 4.times do |i|
  115. username = "search_test_account_#{i}"
  116. account = Fabricate.create(:account, username: username, indexable: i.even?, discoverable: i.even?, note: "Lover of #{i}.")
  117. 2.times do |j|
  118. Fabricate.create(:status, account: account, text: "#{username}'s #{j} post", visibility: j.even? ? :public : :private)
  119. end
  120. end
  121. 3.times do |i|
  122. Fabricate.create(:tag, name: "search_test_tag_#{i}")
  123. end
  124. end
  125. def indexes
  126. [
  127. AccountsIndex,
  128. PublicStatusesIndex,
  129. StatusesIndex,
  130. TagsIndex,
  131. ]
  132. end
  133. def populate_indexes
  134. indexes.each do |index_class|
  135. index_class.purge!
  136. index_class.import!
  137. end
  138. end
  139. def remove_indexes
  140. indexes.each(&:delete!)
  141. end
  142. def cleanup_test_data
  143. Status.destroy_all
  144. Account.destroy_all
  145. Tag.destroy_all
  146. end
  147. end