title: Rails 5 ActiveStorage How to wait for all threads to finish
body: <p>First, let me describe the problem. I'm using RSpec to run a script to generate some test data.</p>
  
  <pre><code>RAILS_ENV=development rspec spec/dev/food/food_upload.rb
  </code></pre>
  
  <p>The script looks like this.</p>
  
  <pre><code>    image = fixture_file_upload(Rails.root.join('spec', 'resources', 'test2.png'), 'image/png')
  
      (0..200).each { 
          food = FactoryBot.build :food
          p "Loading #{food.name}"
          expect {
              post :upload, params: {
                  "foodUpload_foodName"=&gt; food.name,
                  "foodUpload_image"=&gt; image
              }
          }.to change(Food, :count).by(1)
          .and change(Image, :count).by(1)
  </code></pre>
  
  <p>This script will send data to FoodController which then add more food items. Every food item uses ActiveStorage to save an image.</p>
  
  <p>The problem is this script finishes so quickly and as I found out, ActiveStorage spawns new threads to process images. As the result, I have many unfinished images and it shows 'minimagick::invalid error with message improper image header' when trying to load the images.</p>
  
  <p>At the moment, I'm using sleep(5.minutes) to handle this problem. Just want to know if there is another way to do it properly.</p>