spec_helper.rb 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. end
  30. config.after :suite do
  31. FileUtils.rm_rf(Dir[Rails.root.join('spec', 'test_files')])
  32. end
  33. end
  34. def body_as_json
  35. json_str_to_hash(response.body)
  36. end
  37. def json_str_to_hash(str)
  38. JSON.parse(str, symbolize_names: true)
  39. end
  40. def expect_push_bulk_to_match(klass, matcher)
  41. expect(Sidekiq::Client).to receive(:push_bulk).with(hash_including({
  42. 'class' => klass,
  43. 'args' => matcher,
  44. }))
  45. end
  46. class StreamingServerManager
  47. @running_thread = nil
  48. def initialize
  49. at_exit { stop }
  50. end
  51. def start(port: 4020)
  52. return if @running_thread
  53. queue = Queue.new
  54. @queue = queue
  55. @running_thread = Thread.new do
  56. Open3.popen2e(
  57. {
  58. 'REDIS_NAMESPACE' => ENV.fetch('REDIS_NAMESPACE'),
  59. 'DB_NAME' => "#{ENV.fetch('DB_NAME', 'mastodon')}_test#{ENV.fetch('TEST_ENV_NUMBER', '')}",
  60. 'RAILS_ENV' => ENV.fetch('RAILS_ENV', 'test'),
  61. 'NODE_ENV' => ENV.fetch('STREAMING_NODE_ENV', 'development'),
  62. 'PORT' => port.to_s,
  63. },
  64. 'node index.js', # must not call yarn here, otherwise it will fail because yarn does not send signals to its child process
  65. chdir: Rails.root.join('streaming')
  66. ) do |_stdin, stdout_err, process_thread|
  67. status = :starting
  68. # Spawn a thread to listen on streaming server output
  69. output_thread = Thread.new do
  70. stdout_err.each_line do |line|
  71. Rails.logger.info "Streaming server: #{line}"
  72. if status == :starting && line.match('Streaming API now listening on')
  73. status = :started
  74. @queue.enq 'started'
  75. end
  76. end
  77. end
  78. # And another thread to listen on commands from the main thread
  79. loop do
  80. msg = queue.pop
  81. case msg
  82. when 'stop'
  83. # we need to properly stop the reading thread
  84. output_thread.kill
  85. # Then stop the node process
  86. Process.kill('KILL', process_thread.pid)
  87. # And we stop ourselves
  88. @running_thread.kill
  89. end
  90. end
  91. end
  92. end
  93. # wait for 10 seconds for the streaming server to start
  94. Timeout.timeout(10) do
  95. loop do
  96. break if @queue.pop == 'started'
  97. end
  98. end
  99. end
  100. def stop
  101. return unless @running_thread
  102. @queue.enq 'stop'
  103. # Wait for the thread to end
  104. @running_thread.join
  105. end
  106. end
  107. class SearchDataManager
  108. def prepare_test_data
  109. 4.times do |i|
  110. username = "search_test_account_#{i}"
  111. account = Fabricate.create(:account, username: username, indexable: i.even?, discoverable: i.even?, note: "Lover of #{i}.")
  112. 2.times do |j|
  113. Fabricate.create(:status, account: account, text: "#{username}'s #{j} post", visibility: j.even? ? :public : :private)
  114. end
  115. end
  116. 3.times do |i|
  117. Fabricate.create(:tag, name: "search_test_tag_#{i}")
  118. end
  119. end
  120. def indexes
  121. [
  122. AccountsIndex,
  123. PublicStatusesIndex,
  124. StatusesIndex,
  125. TagsIndex,
  126. ]
  127. end
  128. def populate_indexes
  129. indexes.each do |index_class|
  130. index_class.purge!
  131. index_class.import!
  132. end
  133. end
  134. def remove_indexes
  135. indexes.each(&:delete!)
  136. end
  137. def cleanup_test_data
  138. Status.destroy_all
  139. Account.destroy_all
  140. Tag.destroy_all
  141. end
  142. end