Skip to content

Commit

Permalink
Test parallel push doesn't lead mismatched sha
Browse files Browse the repository at this point in the history
transactional_test is set to false because general test flow doesn't
clear records created in Thread.new which use different db connection.
  • Loading branch information
sonalkr132 committed May 26, 2018
1 parent 38896c9 commit 2ead2fa
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 0 deletions.
Binary file added test/gems/hola-0.0.0.gem
Binary file not shown.
Binary file added test/gems/hola/hola-0.0.0.gem
Binary file not shown.
46 changes: 46 additions & 0 deletions test/unit/parallel_pusher_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
require 'test_helper'
require 'concurrent/atomics'

class ParallelPusherTest < ActiveSupport::TestCase
self.use_transactional_tests = false

context "when pushing gems in parallel" do
setup do
@fs = RubygemFs.mock!
@user = create(:user, email: "user@example.com")
end

teardown do
@user.destroy
@rubygem = Rubygem.find_by(name: 'hola')
@rubygem.versions.destroy_all
@rubygem.destroy
Delayed::Job.delete_all
GemDownload.delete_all
end

should "not lead to sha mismatch between gem file and db" do
@gem1 = gem_file("hola-0.0.0.gem")
@gem2 = gem_file("hola/hola-0.0.0.gem")
@cutter1 = Pusher.new(@user, @gem1)
@cutter2 = Pusher.new(@user, @gem2)
latch = Concurrent::CountDownLatch.new(2)

Thread.new do
@cutter1.process
ActiveRecord::Base.connection.close
latch.count_down
end

Thread.new do
@cutter2.process
ActiveRecord::Base.connection.close
latch.count_down
end

latch.wait
expected_sha = Digest::SHA2.base64digest(@fs.get('gems/hola-0.0.0.gem'))
assert_equal expected_sha, Version.last.sha256
end
end
end
18 changes: 18 additions & 0 deletions test/unit/pusher_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -338,4 +338,22 @@ class PusherTest < ActiveSupport::TestCase
assert_equal 'new summary', response['_source']['summary']
end
end

context "pushing to s3 fails" do
setup do
@gem = gem_file("test-1.0.0.gem")
@cutter = Pusher.new(@user, @gem)
@fs = RubygemFs.s3!('https://some.host')
s3_exception = Aws::S3::Errors::ServiceError.new("stub raises", "something went wrong")
Aws::S3::Client.any_instance.stubs(:put_object).with(any_parameters).raises(s3_exception)
@cutter.process
end

should "not create version" do
rubygem = Rubygem.find_by(name: 'test')
expected_message = "There was a problem saving your gem. Please try again."
assert_equal expected_message, @cutter.message
assert_equal 0, rubygem.versions.count
end
end
end

0 comments on commit 2ead2fa

Please sign in to comment.