From 0e97cd1a0d3e7dbe2b99eb111e005d7c9d7002b7 Mon Sep 17 00:00:00 2001 From: Matthew Draper Date: Wed, 30 Nov 2016 21:10:52 +1030 Subject: [PATCH] Avoid race condition in AJ integration tests Make sure the file doesn't exist until we've finished writing it. --- activejob/test/support/integration/dummy_app_template.rb | 3 ++- activejob/test/support/integration/helper.rb | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/activejob/test/support/integration/dummy_app_template.rb b/activejob/test/support/integration/dummy_app_template.rb index 62f6fa13f6..29a5691f30 100644 --- a/activejob/test/support/integration/dummy_app_template.rb +++ b/activejob/test/support/integration/dummy_app_template.rb @@ -18,12 +18,13 @@ class TestJob < ActiveJob::Base queue_as :integration_tests def perform(x) - File.open(Rails.root.join("tmp/\#{x}"), "wb+") do |f| + File.open(Rails.root.join("tmp/\#{x}.new"), "wb+") do |f| f.write Marshal.dump({ "locale" => I18n.locale.to_s || "en", "executed_at" => Time.now.to_r }) end + File.rename(Rails.root.join("tmp/\#{x}.new"), Rails.root.join("tmp/\#{x}")) end end CODE diff --git a/activejob/test/support/integration/helper.rb b/activejob/test/support/integration/helper.rb index 1aaee2c809..626b932cce 100644 --- a/activejob/test/support/integration/helper.rb +++ b/activejob/test/support/integration/helper.rb @@ -5,6 +5,7 @@ ActiveJob::Base.queue_name_prefix = nil require "rails/generators/rails/app/app_generator" +require "tmpdir" dummy_app_path = Dir.mktmpdir + "/dummy" dummy_app_template = File.expand_path("../dummy_app_template.rb", __FILE__) args = Rails::Generators::ARGVScrubber.new(["new", dummy_app_path, "--skip-gemfile", "--skip-bundle",