toptal / chewy

High-level Elasticsearch Ruby framework based on the official elasticsearch-ruby client
MIT License
1.89k stars 368 forks source link

Support rails parallel testing #902

Open quorak opened 11 months ago

quorak commented 11 months ago

Make index_name thread save to support parallel-test feature from rails

It looks like setting the index_name per test can lead to cross pollination of the index_name, leading to situation where the index changes during test, because another elasticsearch test just started.

I checked #342 but don't think naming will solve everything

quorak commented 3 months ago

I got it quite stable by using this approach:

    parallelize_setup do |worker|
      Chewy.settings[:prefix] = "chewy-test-parallel-#{worker}"
      SearchIndex.purge!
    end

    teardown do
      # Loop until the document count is zero
      loop do
        SearchIndex.purge
        sleep 1 # Sleep for 1 second before checking again
        document_count = SearchIndex.client.count(index: SearchIndex.index_name)['count']
        puts "Waiting for deletion... Current document count: #{document_count}"
        break if document_count == 0
      rescue Elasticsearch::Transport::Transport::Errors::ServiceUnavailable
        puts 'Waiting for sever available'
        sleep 3
      end
    end